using Microsoft.Data.Sqlite; using Microsoft.Extensions.Diagnostics.HealthChecks; using ZB.MOM.WW.Auth.ApiKeys.Sqlite; namespace ZB.MOM.WW.MxGateway.Server.Diagnostics; /// /// Readiness probe: verifies the SQLite authentication store is reachable. The gateway /// authenticates every gRPC call against this store, so its reachability gates readiness. /// public sealed class AuthStoreHealthCheck : IHealthCheck { private readonly AuthSqliteConnectionFactory _connectionFactory; public AuthStoreHealthCheck(AuthSqliteConnectionFactory connectionFactory) => _connectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory)); public async Task CheckHealthAsync( HealthCheckContext context, CancellationToken cancellationToken = default) { try { await using SqliteConnection connection = await _connectionFactory.OpenConnectionAsync(cancellationToken).ConfigureAwait(false); await using SqliteCommand command = connection.CreateCommand(); command.CommandText = "SELECT 1;"; await command.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false); return HealthCheckResult.Healthy("Auth store is reachable."); } catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) { throw; } catch (Exception ex) { return HealthCheckResult.Unhealthy("Auth store is unreachable.", ex); } } }