Files
ScadaBridge/src/ZB.MOM.WW.ScadaBridge.SiteRuntime/ServiceCollectionExtensions.cs
T
Joseph Doherty c4186509f6 refactor(health): move ISiteHealthCollector to Commons; Communication drops HealthMonitoring reference (ScadaBridge#87)
Interface now lives in Commons.Interfaces.Services; AuditLog no longer gets it
transitively through Communication. Implementation stays in HealthMonitoring.

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
2026-11-09 13:13:58 -05:00

167 lines
10 KiB
C#

using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using ZB.MOM.WW.LocalDb;
using ZB.MOM.WW.ScadaBridge.Communication.Grpc;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.SiteEventLogging;
using ZB.MOM.WW.ScadaBridge.SiteRuntime.Deployment;
using ZB.MOM.WW.ScadaBridge.SiteRuntime.Persistence;
using ZB.MOM.WW.ScadaBridge.SiteRuntime.Repositories;
using ZB.MOM.WW.ScadaBridge.SiteRuntime.Scripts;
using ZB.MOM.WW.ScadaBridge.SiteRuntime.Secrets;
using ZB.MOM.WW.ScadaBridge.SiteRuntime.Streaming;
namespace ZB.MOM.WW.ScadaBridge.SiteRuntime;
public static class ServiceCollectionExtensions
{
/// <summary>
/// Registers Site Runtime services including SiteStorageService, which persists to the
/// consolidated site database resolved from <c>ILocalDb</c> (<c>LocalDb:Path</c>).
/// </summary>
/// <param name="services">The DI service collection to register services into.</param>
/// <returns>The same <see cref="IServiceCollection"/> to allow chaining.</returns>
public static IServiceCollection AddSiteRuntime(this IServiceCollection services)
{
// SiteStorageService takes ILocalDb (the consolidated site database at
// LocalDb:Path) rather than a connection string, so there is nothing left for a
// caller to supply — the string overload is gone and this is the only entry point.
services.AddSingleton(sp => new SiteStorageService(
sp.GetRequiredService<ILocalDb>(),
sp.GetRequiredService<ILogger<SiteStorageService>>()));
services.AddHostedService<SiteStorageInitializer>();
// Script compilation service.
//
// SINGLETON IS LOAD-BEARING (register row 100): this service owns the
// SiteScriptCompileCache, so "one Roslyn compile per script body per process"
// (N4) is exactly the statement that there is one of these per host. Downgrading
// it to Scoped/Transient would silently give every consumer its own cache and
// reintroduce the recompile storms that were removed — no test would fail on
// behaviour, only the registration pin in SiteRuntimeServiceRegistrationTests.
services.AddSingleton<ScriptCompilationService>();
// Shared script library
services.AddSingleton<SharedScriptLibrary>();
// Site stream manager — registered as singleton and exposed as ISiteStreamSubscriber
// so the gRPC server can subscribe relay actors to instance events.
// ActorSystem is injected later via Initialize() after AkkaHostedService starts.
services.AddSingleton(sp =>
{
var options = sp.GetRequiredService<IOptions<SiteRuntimeOptions>>().Value;
var logger = sp.GetRequiredService<ILogger<SiteStreamManager>>();
return new SiteStreamManager(options, logger);
});
services.AddSingleton<ISiteStreamSubscriber>(sp => sp.GetRequiredService<SiteStreamManager>());
// Site-local cached-operation tracking store — the SQLite source of truth
// that Tracking.Status(TrackedOperationId) reads and the cached-call telemetry
// forwarder writes. Registered HERE (site-only) so the two comments that claim
// "the operational tracking store is registered by AddSiteRuntime" are true
// again (AuditLog SCE + AkkaHostedService). Before this (arch-review 08 round 2
// NF4/T8) it had no DI registration anywhere in src/, so every consumer resolved
// it as null and silently ran degraded (no cached-drain, no PullSiteCalls
// reconciliation, Tracking.Status audit-only). Central roots never call
// AddSiteRuntime, so this stays absent on central — matching the SITE-ONLY
// contract. OperationTrackingOptions is bound + validated by the Host's
// SiteServiceRegistration site-options block — but it no longer supplies the
// store's database: the store takes ILocalDb (the consolidated site database
// at LocalDb:Path), so OperationTrackingOptions.ConnectionString is now only
// read by the site config DB. Resolving this store therefore forces ILocalDb
// construction, which is what runs SiteLocalDbSetup.OnReady.
services.AddSingleton<Commons.Interfaces.IOperationTrackingStore, Tracking.OperationTrackingStore>();
// Script file access (design 2026-09-28): the host reader behind the Files global.
// Always registered — with no ScriptFileAccess:AllowedRoots configured it refuses every
// call (Disabled), so an unconfigured site behaves exactly as before.
services.AddSingleton<Scripts.IScriptFileReader, Scripts.ScriptFileReader>();
// Site-local repository implementations backed by SQLite.
// ExternalSystemDefinitionCache is a singleton shared by every scoped
// SiteExternalSystemRepository instance (one per script-execution DI scope), so
// resolving the repository repeatedly does not each pay a fresh SQLite
// fetch-all — DeploymentManagerActor invalidates the shared cache after an
// artifact deploy applies external-system changes.
services.AddSingleton<ExternalSystemDefinitionCache>();
services.AddScoped<IExternalSystemRepository>(sp => new SiteExternalSystemRepository(
sp.GetRequiredService<SiteStorageService>(),
sp.GetRequiredService<ExternalSystemDefinitionCache>()));
// Notify-and-fetch: typed HttpClient for fetching deployment configs from central.
services.AddHttpClient<IDeploymentConfigFetcher, HttpDeploymentConfigFetcher>()
.ConfigureHttpClient((sp, c) =>
c.Timeout = TimeSpan.FromSeconds(
sp.GetRequiredService<IOptions<SiteRuntimeOptions>>().Value.ConfigFetchTimeoutSeconds));
// S2/UA5: periodically lift the shared script-execution scheduler gauges
// (queue depth, busy threads, oldest-busy age) onto the site health report.
// Registered via factory-lambda so ISiteHealthCollector (registered by the
// Host's AddSiteHealthMonitoring) is resolved lazily at host start.
services.AddHostedService(sp => new ScriptSchedulerStatsReporter(
sp.GetRequiredService<Commons.Interfaces.Services.ISiteHealthCollector>(),
sp.GetRequiredService<IOptions<SiteRuntimeOptions>>().Value,
sp.GetRequiredService<ILogger<ScriptSchedulerStatsReporter>>()));
// Periodically lift SiteStreamManager's alarm-publish-queue drop count
// onto the site health report.
services.AddHostedService(sp => new Streaming.SiteStreamAlarmDropReporter(
sp.GetRequiredService<Commons.Interfaces.Services.ISiteHealthCollector>(),
sp.GetRequiredService<SiteStreamManager>(),
sp.GetRequiredService<ILogger<Streaming.SiteStreamAlarmDropReporter>>()));
// Site-events volume policy: the recorder is a singleton so ScriptRunLauncher
// can resolve it from the root service provider on every run, the same way it already
// resolves ISiteEventLogger. The flush service is registered ONLY here — central
// composition roots never call AddSiteRuntime(), which is what makes the periodic
// "script" summary event site-only, matching the design memo.
services.AddSingleton<ScriptRunSummaryRecorder>();
services.AddHostedService(sp => new ScriptRunSummaryFlushService(
sp.GetRequiredService<ScriptRunSummaryRecorder>(),
sp.GetRequiredService<ISiteEventLogger>(),
sp.GetRequiredService<IOptionsMonitor<SiteRuntimeOptions>>(),
sp.GetRequiredService<ILogger<ScriptRunSummaryFlushService>>()));
// Site-secrets artifact deploy: per-node reconcile of the site-wide,
// LocalDb-replicated deployed_secrets table into THIS node's own ZB.MOM.WW.Secrets
// store. Runs on every site node — not a singleton, since mirrored_secrets (which
// names this node has already applied) is deliberately node-local, not replicated.
// Registered as a singleton (not just a hosted service) so ReconcileAsync can also
// be invoked synchronously — by tests, and by any future on-demand trigger (e.g.
// right after an artifact deploy lands secrets) — without waiting for the next tick.
// ISecretStore/ISecretResolver are registered by the Host's AddScadaBridgeSecrets
// (SecretsNodeRole.Site) — DI registration order does not matter, only that both
// calls happen before the provider is built, which SiteServiceRegistration.Configure
// guarantees.
services.TryAddSingleton(TimeProvider.System);
services.AddSingleton<DeployedSecretsMirror>();
services.AddHostedService(sp => sp.GetRequiredService<DeployedSecretsMirror>());
// Forward every node's replicated secret_mirror_status row onto the
// site health report, so the ACTIVE node's report carries the STANDBY's mirror failures.
services.AddHostedService(sp => new SecretMirrorStatusReporter(
sp.GetRequiredService<Commons.Interfaces.Services.ISiteHealthCollector>(),
sp.GetRequiredService<SiteStorageService>(),
sp.GetRequiredService<ILogger<SecretMirrorStatusReporter>>(),
staleAfter: sp.GetRequiredService<IOptions<SiteRuntimeOptions>>().Value.SecretMirrorStatusStaleAfter,
time: sp.GetRequiredService<TimeProvider>(),
pruneGrace: sp.GetRequiredService<IOptions<SiteRuntimeOptions>>().Value.SecretMirrorStatusRefreshInterval));
return services;
}
/// <summary>Registers any additional DI services needed by the Site Runtime Akka actors.</summary>
/// <param name="services">The DI service collection to register services into.</param>
/// <returns>The same <see cref="IServiceCollection"/> to allow chaining.</returns>
public static IServiceCollection AddSiteRuntimeActors(this IServiceCollection services)
{
// Actor registration is handled by AkkaHostedService.RegisterSiteActors()
// which creates the DeploymentManager singleton and proxy
return services;
}
}