fix(server): refuse to start before touching the database when the archive module is missing
The services callback now runs before the server's registrations, so the check needs no database and integrations can override the clock. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
1 file changed
+14
-10
@@ -1,4 +1,5 @@
|
|||||||
using Microsoft.AspNetCore.HttpOverrides;
|
using Microsoft.AspNetCore.HttpOverrides;
|
||||||
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||||
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
||||||
using Titano.Core.Diagnostics;
|
using Titano.Core.Diagnostics;
|
||||||
using Titano.Data;
|
using Titano.Data;
|
||||||
@@ -15,9 +16,11 @@ namespace Titano.Server.Hosting;
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Builds the Titano Server web application from <see cref="ServerOptions"/> (used by Program, the Windows service
|
/// Builds the Titano Server web application from <see cref="ServerOptions"/> (used by Program, the Windows service
|
||||||
/// host and tests). The archive module plugs in through <paramref name="configureServices"/> of <see cref="Build"/>:
|
/// host and tests). The archive module plugs in through the <c>configureServices</c> callback of <see cref="Build"/>,
|
||||||
/// it must register <see cref="IArchiveIngest"/> and <see cref="IFrameAssetLocator"/>, and may add
|
/// which runs before the server's own registrations: it must register <see cref="IArchiveIngest"/> and
|
||||||
/// <see cref="Titano.Data.Jobs.IJobHandler"/>s; the server refuses to start without the first two.
|
/// <see cref="IFrameAssetLocator"/> (the server refuses to start without them), may add
|
||||||
|
/// <see cref="Titano.Data.Jobs.IJobHandler"/>s, and may resolve <see cref="TitanoDatabase"/>, <see cref="ServerArchive"/>
|
||||||
|
/// and <see cref="ServerOptions"/> from DI in its factories.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class ServerHost
|
public static class ServerHost
|
||||||
{
|
{
|
||||||
@@ -38,6 +41,10 @@ public static class ServerHost
|
|||||||
builder.Logging.AddFilter("Microsoft.AspNetCore", Microsoft.Extensions.Logging.LogLevel.Warning);
|
builder.Logging.AddFilter("Microsoft.AspNetCore", Microsoft.Extensions.Logging.LogLevel.Warning);
|
||||||
builder.Logging.AddFilter("Microsoft.Hosting.Lifetime", Microsoft.Extensions.Logging.LogLevel.Information);
|
builder.Logging.AddFilter("Microsoft.Hosting.Lifetime", Microsoft.Extensions.Logging.LogLevel.Information);
|
||||||
|
|
||||||
|
// The archive module registers first, so a missing piece stops the server before it touches the database.
|
||||||
|
configureServices?.Invoke(builder.Services);
|
||||||
|
RequireArchiveModule(builder.Services);
|
||||||
|
|
||||||
using var bootLoggerFactory = LoggerFactory.Create(l => l.AddSimpleConsole(c => c.SingleLine = true));
|
using var bootLoggerFactory = LoggerFactory.Create(l => l.AddSimpleConsole(c => c.SingleLine = true));
|
||||||
var bootLogger = bootLoggerFactory.CreateLogger("Titano.Server");
|
var bootLogger = bootLoggerFactory.CreateLogger("Titano.Server");
|
||||||
var certificate = CertificateStore.Load(options, bootLogger);
|
var certificate = CertificateStore.Load(options, bootLogger);
|
||||||
@@ -66,10 +73,8 @@ public static class ServerHost
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
ConfigureServices(builder.Services, options, state, database, archive);
|
ConfigureServices(builder.Services, options, state, database, archive);
|
||||||
configureServices?.Invoke(builder.Services);
|
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
RequireArchiveModule(app.Services);
|
|
||||||
Log.SetSink(new LoggerSink(app.Services.GetRequiredService<ILoggerFactory>()));
|
Log.SetSink(new LoggerSink(app.Services.GetRequiredService<ILoggerFactory>()));
|
||||||
ConfigurePipeline(app, options);
|
ConfigurePipeline(app, options);
|
||||||
app.Logger.LogInformation("Titano Server {Version} · archive {Archive} · data {Data} · {Database} · TLS {Tls}{Fingerprint}",
|
app.Logger.LogInformation("Titano Server {Version} · archive {Archive} · data {Data} · {Database} · TLS {Tls}{Fingerprint}",
|
||||||
@@ -93,7 +98,7 @@ public static class ServerHost
|
|||||||
services.AddSingleton(state);
|
services.AddSingleton(state);
|
||||||
services.AddSingleton(database);
|
services.AddSingleton(database);
|
||||||
services.AddSingleton(archive);
|
services.AddSingleton(archive);
|
||||||
services.AddSingleton(TimeProvider.System);
|
services.TryAddSingleton(TimeProvider.System);
|
||||||
services.ConfigureHttpJsonOptions(j => j.SerializerOptions.TypeInfoResolverChain.Insert(0, ProtocolJson.Default));
|
services.ConfigureHttpJsonOptions(j => j.SerializerOptions.TypeInfoResolverChain.Insert(0, ProtocolJson.Default));
|
||||||
services.Configure<RouteHandlerOptions>(o => o.ThrowOnBadRequest = true);
|
services.Configure<RouteHandlerOptions>(o => o.ThrowOnBadRequest = true);
|
||||||
services.Configure<HostOptions>(o => o.ShutdownTimeout = TimeSpan.FromSeconds(30));
|
services.Configure<HostOptions>(o => o.ShutdownTimeout = TimeSpan.FromSeconds(30));
|
||||||
@@ -152,12 +157,11 @@ public static class ServerHost
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Fails fast when the archive module's services are missing (the server would accept uploads it cannot commit).</summary>
|
/// <summary>Fails fast when the archive module's services are missing (the server would accept uploads it cannot commit).</summary>
|
||||||
private static void RequireArchiveModule(IServiceProvider services)
|
private static void RequireArchiveModule(IServiceCollection services)
|
||||||
{
|
{
|
||||||
var registry = services.GetRequiredService<IServiceProviderIsService>();
|
|
||||||
var missing = new List<string>();
|
var missing = new List<string>();
|
||||||
if (!registry.IsService(typeof(IArchiveIngest))) missing.Add($"{typeof(IArchiveIngest).FullName} (commits verified uploads into the archive layout)");
|
if (!services.Any(d => d.ServiceType == typeof(IArchiveIngest))) missing.Add($"{typeof(IArchiveIngest).FullName} (commits verified uploads into the archive layout)");
|
||||||
if (!registry.IsService(typeof(IFrameAssetLocator))) missing.Add($"{typeof(IFrameAssetLocator).FullName} (finds proxies and thumbnails)");
|
if (!services.Any(d => d.ServiceType == typeof(IFrameAssetLocator))) missing.Add($"{typeof(IFrameAssetLocator).FullName} (finds proxies and thumbnails)");
|
||||||
if (missing.Count > 0)
|
if (missing.Count > 0)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException(
|
throw new InvalidOperationException(
|
||||||
|
|||||||
Reference in new issue
Block a user