using Microsoft.AspNetCore.SignalR; namespace ZB.MOM.WW.MxGateway.Server.Dashboard.Hubs; /// /// Background service that subscribes to /// and broadcasts every snapshot it produces to every connected /// client. There is one publisher per /// gateway process; clients listen via the hub. /// /// /// wraps the snapshot subscription in /// a reconnect loop with a configurable retry delay (5s by default, /// mirroring ). A transient failure inside /// — e.g. a /// one-time logger-init failure or a transient SQL error from the Galaxy /// summary projection — would otherwise end the BackgroundService with no /// reconnect, taking the dashboard offline until process restart. /// public sealed class DashboardSnapshotPublisher : BackgroundService { private static readonly TimeSpan DefaultReconnectDelay = TimeSpan.FromSeconds(5); private readonly IDashboardSnapshotService _snapshotService; private readonly IHubContext _hubContext; private readonly ILogger _logger; private readonly TimeSpan _reconnectDelay; /// Initializes a new instance of the DashboardSnapshotPublisher class. /// The snapshot service to subscribe to. /// The SignalR hub context for broadcasting. /// The logger instance. public DashboardSnapshotPublisher( IDashboardSnapshotService snapshotService, IHubContext hubContext, ILogger logger) : this(snapshotService, hubContext, logger, DefaultReconnectDelay) { } /// Initializes a new instance of the DashboardSnapshotPublisher class with custom reconnect delay. /// Internal hook for testing: tests inject a very short reconnect delay so assertions don't wait full 5s. /// The snapshot service to subscribe to. /// The SignalR hub context for broadcasting. /// The logger instance. /// The delay before reconnecting after a subscription failure. internal DashboardSnapshotPublisher( IDashboardSnapshotService snapshotService, IHubContext hubContext, ILogger logger, TimeSpan reconnectDelay) { _snapshotService = snapshotService; _hubContext = hubContext; _logger = logger; _reconnectDelay = reconnectDelay; } /// protected override async Task ExecuteAsync(CancellationToken stoppingToken) { // Loop forever — when WatchSnapshotsAsync completes or throws, reopen // the subscription after a short delay. The hosted-service lifetime // ends only when the host stops. Mirrors AlarmsHubPublisher. while (!stoppingToken.IsCancellationRequested) { try { await foreach (DashboardSnapshot snapshot in _snapshotService .WatchSnapshotsAsync(stoppingToken) .ConfigureAwait(false)) { if (stoppingToken.IsCancellationRequested) { break; } try { await _hubContext.Clients .All .SendAsync(DashboardSnapshotHub.SnapshotMessage, snapshot, stoppingToken) .ConfigureAwait(false); } catch (Exception ex) when (ex is not OperationCanceledException) { _logger.LogWarning(ex, "Snapshot broadcast failed; will retry on the next snapshot tick."); } } } catch (OperationCanceledException) { return; } catch (Exception ex) { _logger.LogWarning(ex, "Snapshot subscription faulted; reconnecting in {DelaySeconds:F1}s.", _reconnectDelay.TotalSeconds); try { await Task.Delay(_reconnectDelay, stoppingToken).ConfigureAwait(false); } catch (OperationCanceledException) { return; } } } } }