Add client-side failover to CLI tool for redundancy testing
All commands gain --failover-urls (-F) to specify alternate endpoints. Short-lived commands try each URL in order on initial connect. The subscribe command monitors KeepAlive and automatically reconnects to the next available server, re-creating the subscription on failover. Verified with live service start/stop: primary down triggers failover to secondary, primary restart allows failback. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -9,9 +9,6 @@ namespace OpcUaCli.Commands;
|
||||
[Command("subscribe", Description = "Monitor a node for value changes")]
|
||||
public class SubscribeCommand : ICommand
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the OPC UA endpoint URL to connect to before subscribing.
|
||||
/// </summary>
|
||||
[CommandOption("url", 'u', Description = "OPC UA server endpoint URL", IsRequired = true)]
|
||||
public string Url { get; init; } = default!;
|
||||
|
||||
@@ -24,26 +21,108 @@ public class SubscribeCommand : ICommand
|
||||
[CommandOption("security", 'S', Description = "Transport security: none, sign, encrypt (default: none)")]
|
||||
public string Security { get; init; } = "none";
|
||||
|
||||
/// <summary>
|
||||
/// Gets the node identifier to monitor for value changes.
|
||||
/// </summary>
|
||||
[CommandOption("failover-urls", 'F', Description = "Comma-separated failover endpoint URLs for redundancy")]
|
||||
public string? FailoverUrls { get; init; }
|
||||
|
||||
[CommandOption("node", 'n', Description = "Node ID to monitor", IsRequired = true)]
|
||||
public string NodeId { get; init; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the sampling and publishing interval, in milliseconds, for the monitored item.
|
||||
/// </summary>
|
||||
[CommandOption("interval", 'i', Description = "Polling interval in milliseconds")]
|
||||
public int Interval { get; init; } = 1000;
|
||||
|
||||
/// <summary>
|
||||
/// Connects to the OPC UA endpoint and streams monitored-item notifications until cancellation.
|
||||
/// </summary>
|
||||
/// <param name="console">The console used to display subscription updates.</param>
|
||||
public async ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
using var session = await OpcUaHelper.ConnectAsync(Url, Username, Password, Security);
|
||||
var urls = FailoverUrlParser.Parse(Url, FailoverUrls);
|
||||
var hasFailover = urls.Length > 1;
|
||||
|
||||
if (hasFailover)
|
||||
{
|
||||
await RunWithFailoverAsync(console, urls);
|
||||
}
|
||||
else
|
||||
{
|
||||
await RunSimpleAsync(console);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RunSimpleAsync(IConsole console)
|
||||
{
|
||||
using var session = await OpcUaHelper.ConnectAsync(Url, Username, Password, Security);
|
||||
var (subscription, item) = await CreateSubscriptionAsync(session);
|
||||
await console.Output.WriteLineAsync($"Subscribed to {NodeId} (interval: {Interval}ms). Press Ctrl+C to stop.");
|
||||
|
||||
var ct = console.RegisterCancellationHandler();
|
||||
await MonitorLoopAsync(session, subscription, item, ct);
|
||||
await console.Output.WriteLineAsync("Unsubscribed.");
|
||||
}
|
||||
|
||||
private async Task RunWithFailoverAsync(IConsole console, string[] urls)
|
||||
{
|
||||
using var failover = new OpcUaFailoverHelper(urls, Username, Password, Security);
|
||||
var session = await failover.ConnectAsync();
|
||||
|
||||
Subscription? subscription = null;
|
||||
MonitoredItem? item = null;
|
||||
var subLock = new object();
|
||||
|
||||
(subscription, item) = await CreateSubscriptionAsync(session);
|
||||
await console.Output.WriteLineAsync(
|
||||
$"Subscribed to {NodeId} (interval: {Interval}ms, failover enabled). Press Ctrl+C to stop.");
|
||||
|
||||
// Install failover handler
|
||||
failover.OnFailover += (oldUrl, newUrl) =>
|
||||
{
|
||||
Console.WriteLine($" [failover] Switched from {oldUrl} to {newUrl}");
|
||||
};
|
||||
|
||||
failover.InstallKeepAliveHandler(async newSession =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var (newSub, newItem) = await CreateSubscriptionAsync(newSession);
|
||||
lock (subLock)
|
||||
{
|
||||
subscription = newSub;
|
||||
item = newItem;
|
||||
}
|
||||
Console.WriteLine($" [failover] Re-subscribed to {NodeId} on {failover.CurrentEndpointUrl}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($" [failover] Failed to re-subscribe: {ex.Message}");
|
||||
}
|
||||
});
|
||||
|
||||
var ct = console.RegisterCancellationHandler();
|
||||
|
||||
int tick = 0;
|
||||
while (!ct.IsCancellationRequested)
|
||||
{
|
||||
await Task.Delay(2000, ct).ContinueWith(_ => { });
|
||||
tick++;
|
||||
|
||||
Session? currentSession;
|
||||
MonitoredItem? currentItem;
|
||||
Subscription? currentSub;
|
||||
lock (subLock)
|
||||
{
|
||||
currentSession = failover.Session;
|
||||
currentSub = subscription;
|
||||
currentItem = item;
|
||||
}
|
||||
|
||||
Console.WriteLine(
|
||||
$" [tick {tick}] Server={failover.CurrentEndpointUrl}, Connected={currentSession?.Connected}, " +
|
||||
$"Sub.Id={currentSub?.Id}, " +
|
||||
$"LastValue={((currentItem?.LastValue as MonitoredItemNotification)?.Value?.Value)} " +
|
||||
$"({((currentItem?.LastValue as MonitoredItemNotification)?.Value?.StatusCode)})");
|
||||
}
|
||||
|
||||
await console.Output.WriteLineAsync("Unsubscribed.");
|
||||
}
|
||||
|
||||
private async Task<(Subscription, MonitoredItem)> CreateSubscriptionAsync(Session session)
|
||||
{
|
||||
var subscription = new Subscription(session.DefaultSubscription)
|
||||
{
|
||||
PublishingInterval = Interval,
|
||||
@@ -57,7 +136,7 @@ public class SubscribeCommand : ICommand
|
||||
SamplingInterval = Interval
|
||||
};
|
||||
|
||||
item.Notification += (monitoredItem, e) =>
|
||||
item.Notification += (_, e) =>
|
||||
{
|
||||
if (e.NotificationValue is MonitoredItemNotification notification)
|
||||
{
|
||||
@@ -70,11 +149,11 @@ public class SubscribeCommand : ICommand
|
||||
session.AddSubscription(subscription);
|
||||
await subscription.CreateAsync();
|
||||
|
||||
await console.Output.WriteLineAsync(
|
||||
$"Subscribed to {NodeId} (interval: {Interval}ms). Press Ctrl+C to stop.");
|
||||
|
||||
var ct = console.RegisterCancellationHandler();
|
||||
return (subscription, item);
|
||||
}
|
||||
|
||||
private static async Task MonitorLoopAsync(Session session, Subscription subscription, MonitoredItem item, CancellationToken ct)
|
||||
{
|
||||
int tick = 0;
|
||||
while (!ct.IsCancellationRequested)
|
||||
{
|
||||
@@ -87,7 +166,5 @@ public class SubscribeCommand : ICommand
|
||||
$"ItemStatus={item.Status?.Id}, " +
|
||||
$"LastNotification={((item.LastValue as MonitoredItemNotification)?.Value?.Value)} ({((item.LastValue as MonitoredItemNotification)?.Value?.StatusCode)})");
|
||||
}
|
||||
|
||||
await console.Output.WriteLineAsync("Unsubscribed.");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user