using Akka.Actor; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using ZB.MOM.WW.OtOpcUa.Cluster; using ZB.MOM.WW.OtOpcUa.Commons.Interfaces; using ZB.MOM.WW.OtOpcUa.Commons.Messages.Fleet; using ZB.MOM.WW.OtOpcUa.Commons.Types; namespace ZB.MOM.WW.OtOpcUa.AdminUI.Clients; /// /// backed by an Akka against /// the target node's DriverHostActor at akka.tcp://otopcua@<host>:<port>/user/driver-host. /// Sends ; expects a reply. /// /// On timeout or any other failure (peer down, GetDiagnostics handler missing) returns an /// empty snapshot so the UI degrades to "no data" instead of throwing. The failure is logged at /// Warning, since the snapshot shape cannot carry it: an empty snapshot reads the same as a node with /// no drivers. Consumed by the /fleet/diagnostics page. /// public sealed class FleetDiagnosticsClient : IFleetDiagnosticsClient { private static readonly TimeSpan AskTimeout = TimeSpan.FromSeconds(3); private readonly ActorSystem _system; private readonly string _systemName; private readonly ILogger _log; /// Initializes a new FleetDiagnosticsClient with the given actor system and cluster options. /// The Akka actor system. /// Cluster configuration options. /// Logger for the degraded-to-empty path; defaults to a no-op logger. public FleetDiagnosticsClient( ActorSystem system, IOptions options, ILogger? log = null) { _system = system; _systemName = options.Value.SystemName; _log = log ?? NullLogger.Instance; } /// public async Task GetDiagnosticsAsync(NodeId nodeId, CancellationToken ct) { try { // Inside the try: a malformed node value makes ActorSelection throw, and that must degrade to // "unavailable" like a dead node does, not surface through the page's error boundary. var selection = _system.ActorSelection($"akka.tcp://{_systemName}@{nodeId.Value}/user/driver-host"); using var linked = CancellationTokenSource.CreateLinkedTokenSource(ct); linked.CancelAfter(AskTimeout); return await selection.Ask( new GetDiagnostics(CorrelationId.NewId()), AskTimeout, linked.Token); } catch (Exception ex) when (!ct.IsCancellationRequested) { _log.LogWarning(ex, "Fleet diagnostics for node {NodeId} unavailable (no answer within {Timeout}s); returning an empty snapshot", nodeId.Value, AskTimeout.TotalSeconds); return new NodeDiagnosticsSnapshot( nodeId, CurrentRevision: null, Drivers: Array.Empty(), AsOfUtc: DateTime.UtcNow); } } }