feat(adminui): add DriverStatusSignalRBridge + InMemory snapshot store
This commit is contained in:
@@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.AspNetCore.Components.Web;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Hubs;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.AdminUI;
|
||||
|
||||
@@ -37,6 +38,7 @@ public static class EndpointRouteBuilderExtensions
|
||||
public static IServiceCollection AddAdminUI(this IServiceCollection services)
|
||||
{
|
||||
services.AddRazorComponents().AddInteractiveServerComponents();
|
||||
services.AddOtOpcUaDriverStatusServices();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
using Akka.Actor;
|
||||
using Akka.Cluster.Tools.PublishSubscribe;
|
||||
using Akka.Event;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Drivers;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Hubs;
|
||||
|
||||
/// <summary>
|
||||
/// Akka actor that subscribes to the <c>driver-health</c> DistributedPubSub topic and
|
||||
/// forwards every <see cref="DriverHealthChanged"/> snapshot to (a) the in-memory snapshot
|
||||
/// store and (b) all SignalR clients connected to <see cref="DriverStatusHub"/> grouped
|
||||
/// by <see cref="DriverHealthChanged.DriverInstanceId"/>. Spawned on admin-role nodes by
|
||||
/// <c>AddOtOpcUaSignalRBridges</c>.
|
||||
/// </summary>
|
||||
public sealed class DriverStatusSignalRBridge : ReceiveActor
|
||||
{
|
||||
public const string TopicName = "driver-health";
|
||||
|
||||
private readonly IHubContext<DriverStatusHub> _hub;
|
||||
private readonly IDriverStatusSnapshotStore _store;
|
||||
private readonly ILoggingAdapter _log = Context.GetLogger();
|
||||
|
||||
/// <summary>Creates actor props for a <see cref="DriverStatusSignalRBridge"/>.</summary>
|
||||
/// <param name="hub">The SignalR hub context for pushing snapshots to grouped clients.</param>
|
||||
/// <param name="store">Snapshot store updated before each SignalR push.</param>
|
||||
public static Props Props(IHubContext<DriverStatusHub> hub, IDriverStatusSnapshotStore store) =>
|
||||
Akka.Actor.Props.Create(() => new DriverStatusSignalRBridge(hub, store));
|
||||
|
||||
/// <summary>Initializes a new instance of <see cref="DriverStatusSignalRBridge"/>.</summary>
|
||||
/// <param name="hub">The SignalR hub context for pushing snapshots to grouped clients.</param>
|
||||
/// <param name="store">Snapshot store updated before each SignalR push.</param>
|
||||
public DriverStatusSignalRBridge(IHubContext<DriverStatusHub> hub, IDriverStatusSnapshotStore store)
|
||||
{
|
||||
_hub = hub;
|
||||
_store = store;
|
||||
ReceiveAsync<DriverHealthChanged>(ForwardAsync);
|
||||
Receive<SubscribeAck>(_ => { /* DPS confirmation */ });
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void PreStart() =>
|
||||
DistributedPubSub.Get(Context.System).Mediator.Tell(new Subscribe(TopicName, Self));
|
||||
|
||||
private async Task ForwardAsync(DriverHealthChanged msg)
|
||||
{
|
||||
try
|
||||
{
|
||||
_store.Upsert(msg);
|
||||
await _hub.Clients.Group(DriverStatusHub.GroupName(msg.DriverInstanceId))
|
||||
.SendAsync(DriverStatusHub.MethodName, msg);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.Warning(ex, "DriverStatusSignalRBridge: SignalR push failed (instance={Instance})", msg.DriverInstanceId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,19 @@ public static class HubServiceCollectionExtensions
|
||||
public const string FleetStatusSignalRBridgeName = "fleet-status-signalr-bridge";
|
||||
public const string AlertSignalRBridgeName = "alert-signalr-bridge";
|
||||
public const string ScriptLogSignalRBridgeName = "script-log-signalr-bridge";
|
||||
public const string DriverStatusSignalRBridgeName = "driver-status-signalr-bridge";
|
||||
|
||||
/// <summary>
|
||||
/// Registers services required by the driver-status hub pipeline:
|
||||
/// <see cref="IDriverStatusSnapshotStore"/> as a singleton backed by
|
||||
/// <see cref="InMemoryDriverStatusSnapshotStore"/>.
|
||||
/// </summary>
|
||||
/// <param name="services">The service collection.</param>
|
||||
public static IServiceCollection AddOtOpcUaDriverStatusServices(this IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IDriverStatusSnapshotStore, InMemoryDriverStatusSnapshotStore>();
|
||||
return services;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Spawns the SignalR bridge actors that forward DPS messages to browser-facing SignalR
|
||||
@@ -41,6 +54,11 @@ public static class HubServiceCollectionExtensions
|
||||
var scriptLogHub = resolver.GetService<IHubContext<ScriptLogHub>>();
|
||||
var scriptLogBridge = system.ActorOf(ScriptLogSignalRBridge.Props(scriptLogHub), ScriptLogSignalRBridgeName);
|
||||
registry.Register<ScriptLogSignalRBridgeKey>(scriptLogBridge);
|
||||
|
||||
var driverStatusHub = resolver.GetService<IHubContext<DriverStatusHub>>();
|
||||
var driverStatusStore = resolver.GetService<IDriverStatusSnapshotStore>();
|
||||
var driverStatusBridge = system.ActorOf(DriverStatusSignalRBridge.Props(driverStatusHub, driverStatusStore), DriverStatusSignalRBridgeName);
|
||||
registry.Register<DriverStatusSignalRBridgeKey>(driverStatusBridge);
|
||||
});
|
||||
return builder;
|
||||
}
|
||||
@@ -50,3 +68,4 @@ public static class HubServiceCollectionExtensions
|
||||
public sealed class FleetStatusSignalRBridgeKey { }
|
||||
public sealed class AlertSignalRBridgeKey { }
|
||||
public sealed class ScriptLogSignalRBridgeKey { }
|
||||
public sealed class DriverStatusSignalRBridgeKey { }
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Drivers;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Hubs;
|
||||
|
||||
/// <summary>
|
||||
/// Singleton last-snapshot-per-instance cache. Populated by
|
||||
/// <c>DriverStatusSignalRBridge</c> as it forwards DPS messages; read by
|
||||
/// <see cref="DriverStatusHub.JoinDriver"/> so newly-joined clients see current state
|
||||
/// without waiting for the next change event.
|
||||
/// </summary>
|
||||
public interface IDriverStatusSnapshotStore
|
||||
{
|
||||
void Upsert(DriverHealthChanged snapshot);
|
||||
bool TryGet(string driverInstanceId, out DriverHealthChanged snapshot);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System.Collections.Concurrent;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Drivers;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Hubs;
|
||||
|
||||
/// <summary>
|
||||
/// Thread-safe in-memory implementation of <see cref="IDriverStatusSnapshotStore"/>.
|
||||
/// Keyed by <see cref="DriverHealthChanged.DriverInstanceId"/>; last write wins.
|
||||
/// </summary>
|
||||
public sealed class InMemoryDriverStatusSnapshotStore : IDriverStatusSnapshotStore
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, DriverHealthChanged> _byInstance = new();
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Upsert(DriverHealthChanged snapshot)
|
||||
=> _byInstance[snapshot.DriverInstanceId] = snapshot;
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool TryGet(string driverInstanceId, out DriverHealthChanged snapshot)
|
||||
=> _byInstance.TryGetValue(driverInstanceId, out snapshot!);
|
||||
}
|
||||
Reference in New Issue
Block a user