From d26462ed8dd40db035c774a727efc329530e1b91 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Fri, 26 Jun 2026 05:13:58 -0400 Subject: [PATCH] feat(delmia-notifier): connect-failure-only failover loop --- .../IRecipeSender.cs | 21 ++++++ .../Notifier.cs | 47 +++++++++++++ .../NotifierTests.cs | 70 +++++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 src/ZB.MOM.WW.ScadaBridge.DelmiaNotifier/IRecipeSender.cs create mode 100644 src/ZB.MOM.WW.ScadaBridge.DelmiaNotifier/Notifier.cs create mode 100644 tests/ZB.MOM.WW.ScadaBridge.DelmiaNotifier.Tests/NotifierTests.cs diff --git a/src/ZB.MOM.WW.ScadaBridge.DelmiaNotifier/IRecipeSender.cs b/src/ZB.MOM.WW.ScadaBridge.DelmiaNotifier/IRecipeSender.cs new file mode 100644 index 00000000..40dae6fc --- /dev/null +++ b/src/ZB.MOM.WW.ScadaBridge.DelmiaNotifier/IRecipeSender.cs @@ -0,0 +1,21 @@ +namespace ZB.MOM.WW.ScadaBridge.DelmiaNotifier; + +/// Whether a single POST attempt reached the server () or never did (). +internal enum AttemptKind +{ + Connected, + ConnectFailed, +} + +/// Result of one POST attempt against a single base URL. +/// Did the attempt reach the server? +/// HTTP status when ; 0 otherwise. +/// Parsed response body on a 2xx; null otherwise. +/// Connection/exception detail when ; null otherwise. +internal sealed record AttemptOutcome(AttemptKind Kind, int StatusCode, RecipeDownloadResult? Body, string? Error); + +/// Seam over a single recipe-download POST attempt, so the failover loop is testable without real HTTP. +internal interface IRecipeSender +{ + Task SendAsync(string baseUrl, RecipeDownload payload, CancellationToken ct); +} diff --git a/src/ZB.MOM.WW.ScadaBridge.DelmiaNotifier/Notifier.cs b/src/ZB.MOM.WW.ScadaBridge.DelmiaNotifier/Notifier.cs new file mode 100644 index 00000000..93eb1fce --- /dev/null +++ b/src/ZB.MOM.WW.ScadaBridge.DelmiaNotifier/Notifier.cs @@ -0,0 +1,47 @@ +namespace ZB.MOM.WW.ScadaBridge.DelmiaNotifier; + +/// Final outcome of the notify operation, mapped 1:1 to the YES/NO + exit-code contract. +internal sealed record NotifyResult(bool Ok, string Reason); + +/// +/// Connect-failure-only failover loop: tries each base URL in order. A node that responds at all is +/// authoritative — its answer is final (success, business rejection, or HTTP error alike). Only a +/// failure to connect rolls over to the next URL; if every URL fails to connect the last error is reported. +/// +internal static class Notifier +{ + public static async Task RunAsync( + string[] baseUrls, RecipeDownload payload, IRecipeSender sender, CancellationToken ct) + { + var lastError = "no base URLs configured"; + + foreach (var baseUrl in baseUrls) + { + var outcome = await sender.SendAsync(baseUrl, payload, ct); + + if (outcome.Kind == AttemptKind.ConnectFailed) + { + lastError = outcome.Error ?? "connection failed"; + continue; // unreachable node → try the next + } + + // Connected — this node's answer is authoritative; never fail over past it. + if (IsSuccessStatus(outcome.StatusCode)) + { + if (outcome.Body is { Result: true }) + { + return new NotifyResult(true, outcome.Body.ResultText ?? string.Empty); + } + + var reason = outcome.Body?.ResultText; + return new NotifyResult(false, string.IsNullOrEmpty(reason) ? "request rejected" : reason); + } + + return new NotifyResult(false, $"HTTP {outcome.StatusCode}"); + } + + return new NotifyResult(false, $"all URLs unreachable: {lastError}"); + } + + private static bool IsSuccessStatus(int status) => status is >= 200 and < 300; +} diff --git a/tests/ZB.MOM.WW.ScadaBridge.DelmiaNotifier.Tests/NotifierTests.cs b/tests/ZB.MOM.WW.ScadaBridge.DelmiaNotifier.Tests/NotifierTests.cs new file mode 100644 index 00000000..95bc488e --- /dev/null +++ b/tests/ZB.MOM.WW.ScadaBridge.DelmiaNotifier.Tests/NotifierTests.cs @@ -0,0 +1,70 @@ +using ZB.MOM.WW.ScadaBridge.DelmiaNotifier; + +namespace ZB.MOM.WW.ScadaBridge.DelmiaNotifier.Tests; + +public class NotifierTests +{ + private sealed class FakeSender(params AttemptOutcome[] outcomes) : IRecipeSender + { + private readonly Queue _outcomes = new(outcomes); + public int Calls { get; private set; } + + public Task SendAsync(string baseUrl, RecipeDownload payload, CancellationToken ct) + { + Calls++; + return Task.FromResult(_outcomes.Dequeue()); + } + } + + private static readonly string[] TwoUrls = ["http://a", "http://b"]; + private static readonly RecipeDownload Payload = + new() { MachineCode = "Z", DownloadPath = "x", WorkOrderNumber = "W", PartNumber = "P" }; + + [Fact] + public async Task ConnectFailure_advances_to_next_url() + { + var sender = new FakeSender( + new AttemptOutcome(AttemptKind.ConnectFailed, 0, null, "refused"), + new AttemptOutcome(AttemptKind.Connected, 200, new RecipeDownloadResult { Result = true }, null)); + var result = await Notifier.RunAsync(TwoUrls, Payload, sender, CancellationToken.None); + Assert.True(result.Ok); + Assert.Equal(2, sender.Calls); + } + + [Fact] + public async Task Connected_result_false_is_final_no_failover() + { + var sender = new FakeSender( + new AttemptOutcome(AttemptKind.Connected, 200, new RecipeDownloadResult { Result = false, ResultText = "bad machine" }, null), + new AttemptOutcome(AttemptKind.Connected, 200, new RecipeDownloadResult { Result = true }, null)); + var result = await Notifier.RunAsync(TwoUrls, Payload, sender, CancellationToken.None); + Assert.False(result.Ok); + Assert.Contains("bad machine", result.Reason); + Assert.Equal(1, sender.Calls); + } + + [Fact] + public async Task Connected_5xx_is_final_no_failover() + { + var sender = new FakeSender( + new AttemptOutcome(AttemptKind.Connected, 500, null, null), + new AttemptOutcome(AttemptKind.Connected, 200, new RecipeDownloadResult { Result = true }, null)); + var result = await Notifier.RunAsync(TwoUrls, Payload, sender, CancellationToken.None); + Assert.False(result.Ok); + Assert.Contains("500", result.Reason); + Assert.Equal(1, sender.Calls); + } + + [Fact] + public async Task All_connect_failures_report_unreachable_with_last_error() + { + var sender = new FakeSender( + new AttemptOutcome(AttemptKind.ConnectFailed, 0, null, "refused-a"), + new AttemptOutcome(AttemptKind.ConnectFailed, 0, null, "refused-b")); + var result = await Notifier.RunAsync(TwoUrls, Payload, sender, CancellationToken.None); + Assert.False(result.Ok); + Assert.Contains("unreachable", result.Reason); + Assert.Contains("refused-b", result.Reason); + Assert.Equal(2, sender.Calls); + } +}