fca978de07
Sweep of 203 source files resolving CommentChecker findings: add <summary>/<param>/<returns>/<inheritdoc> where missing, and remove resolved task/issue tracking markers (Tests-NNN, Worker-NNN, Server-NNN, Task N) from code comments. Comment/doc-only — no logic changes. Server+Tests build clean under TreatWarningsAsErrors.
110 lines
4.7 KiB
C#
110 lines
4.7 KiB
C#
using Microsoft.AspNetCore.SignalR;
|
|
|
|
namespace ZB.MOM.WW.MxGateway.Server.Dashboard.Hubs;
|
|
|
|
/// <summary>
|
|
/// Background service that subscribes to <see cref="IDashboardSnapshotService.WatchSnapshotsAsync"/>
|
|
/// and broadcasts every snapshot it produces to every connected
|
|
/// <see cref="DashboardSnapshotHub"/> client. There is one publisher per
|
|
/// gateway process; clients listen via the hub.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <see cref="ExecuteAsync"/> wraps the snapshot subscription in
|
|
/// a reconnect loop with a configurable retry delay (5s by default,
|
|
/// mirroring <see cref="AlarmsHubPublisher"/>). A transient failure inside
|
|
/// <see cref="IDashboardSnapshotService.WatchSnapshotsAsync"/> — 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.
|
|
/// </remarks>
|
|
public sealed class DashboardSnapshotPublisher : BackgroundService
|
|
{
|
|
private static readonly TimeSpan DefaultReconnectDelay = TimeSpan.FromSeconds(5);
|
|
|
|
private readonly IDashboardSnapshotService _snapshotService;
|
|
private readonly IHubContext<DashboardSnapshotHub> _hubContext;
|
|
private readonly ILogger<DashboardSnapshotPublisher> _logger;
|
|
private readonly TimeSpan _reconnectDelay;
|
|
|
|
/// <summary>Initializes a new instance of the DashboardSnapshotPublisher class.</summary>
|
|
/// <param name="snapshotService">The snapshot service to subscribe to.</param>
|
|
/// <param name="hubContext">The SignalR hub context for broadcasting.</param>
|
|
/// <param name="logger">The logger instance.</param>
|
|
public DashboardSnapshotPublisher(
|
|
IDashboardSnapshotService snapshotService,
|
|
IHubContext<DashboardSnapshotHub> hubContext,
|
|
ILogger<DashboardSnapshotPublisher> logger)
|
|
: this(snapshotService, hubContext, logger, DefaultReconnectDelay)
|
|
{
|
|
}
|
|
|
|
/// <summary>Initializes a new instance of the DashboardSnapshotPublisher class with custom reconnect delay.</summary>
|
|
/// <remarks>Internal hook for testing: tests inject a very short reconnect delay so assertions don't wait full 5s.</remarks>
|
|
/// <param name="snapshotService">The snapshot service to subscribe to.</param>
|
|
/// <param name="hubContext">The SignalR hub context for broadcasting.</param>
|
|
/// <param name="logger">The logger instance.</param>
|
|
/// <param name="reconnectDelay">The delay before reconnecting after a subscription failure.</param>
|
|
internal DashboardSnapshotPublisher(
|
|
IDashboardSnapshotService snapshotService,
|
|
IHubContext<DashboardSnapshotHub> hubContext,
|
|
ILogger<DashboardSnapshotPublisher> logger,
|
|
TimeSpan reconnectDelay)
|
|
{
|
|
_snapshotService = snapshotService;
|
|
_hubContext = hubContext;
|
|
_logger = logger;
|
|
_reconnectDelay = reconnectDelay;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|