feat(delmia-notifier): connect-failure-only failover loop
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
namespace ZB.MOM.WW.ScadaBridge.DelmiaNotifier;
|
||||
|
||||
/// <summary>Whether a single POST attempt reached the server (<see cref="Connected"/>) or never did (<see cref="ConnectFailed"/>).</summary>
|
||||
internal enum AttemptKind
|
||||
{
|
||||
Connected,
|
||||
ConnectFailed,
|
||||
}
|
||||
|
||||
/// <summary>Result of one POST attempt against a single base URL.</summary>
|
||||
/// <param name="Kind">Did the attempt reach the server?</param>
|
||||
/// <param name="StatusCode">HTTP status when <see cref="AttemptKind.Connected"/>; 0 otherwise.</param>
|
||||
/// <param name="Body">Parsed response body on a 2xx; null otherwise.</param>
|
||||
/// <param name="Error">Connection/exception detail when <see cref="AttemptKind.ConnectFailed"/>; null otherwise.</param>
|
||||
internal sealed record AttemptOutcome(AttemptKind Kind, int StatusCode, RecipeDownloadResult? Body, string? Error);
|
||||
|
||||
/// <summary>Seam over a single recipe-download POST attempt, so the failover loop is testable without real HTTP.</summary>
|
||||
internal interface IRecipeSender
|
||||
{
|
||||
Task<AttemptOutcome> SendAsync(string baseUrl, RecipeDownload payload, CancellationToken ct);
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
namespace ZB.MOM.WW.ScadaBridge.DelmiaNotifier;
|
||||
|
||||
/// <summary>Final outcome of the notify operation, mapped 1:1 to the YES/NO + exit-code contract.</summary>
|
||||
internal sealed record NotifyResult(bool Ok, string Reason);
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
internal static class Notifier
|
||||
{
|
||||
public static async Task<NotifyResult> 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;
|
||||
}
|
||||
Reference in New Issue
Block a user