feat: Chronos Desktop shares its library on the LAN (owner review 5 P4)
Chronos Server for Windows is replaced by Settings > Server > Share this library: Chronos Desktop hosts the server engine in its own process over its own index, archive and processing services, job engine and scheduler, so a second PC, the Uploader and render nodes use this PC's library without a Windows service or a second index. - Chronos.Server is a library; Chronos.Server.Host is the console exe that Docker and development runs start. The App references the library and the ASP.NET Core shared framework (no package). - ServerHost.BuildHosted/HostedServer: reuses the desktop's engines and never creates, starts or disposes them; no static log sink, no console lifetime; server.json plus desktop-owned values locked "Set in Chronos Desktop"; only upload-cleanup and audit-cleanup schedules are added. - Both modes: by-id endpoints scoped to the served archive, a client's render export folder dropped; hosted mode exposes server job kinds only. - Desktop: LibraryShare (start after the schedules, stop first, port in use named), Settings > Server sharing rows, Server > This library panel (state, addresses with Copy, ports, fingerprint, temporary admin password, clients, admin panel), own library refused by ConnectFlow and dropped from LAN discovery, the uploads sweep step, thumbnails after scans while sharing, capture scenes server-share and server-share-failed. - Removed: the Windows service hosting of Chronos.Server, its switches and its tests. Refs #67 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9ad8c6f7a9
commit
8d41992d84
69 files changed
+2633
-859
No files matched your search
@@ -16,12 +16,13 @@ using Chronos.Server.Storage;
|
||||
namespace Chronos.Server.Hosting;
|
||||
|
||||
/// <summary>
|
||||
/// Builds the Chronos Server web application from <see cref="ServerOptions"/> (used by Program, the Windows service
|
||||
/// host and tests). The archive module plugs in through the <c>configureServices</c> callback of <see cref="Build"/>,
|
||||
/// Builds the Chronos Server web application from <see cref="ServerOptions"/> (used by Chronos.Server.Host's Program and
|
||||
/// tests). The archive module plugs in through the <c>configureServices</c> callback of <see cref="Build"/>,
|
||||
/// which runs before the server's own registrations: it must register <see cref="IArchiveIngest"/> and
|
||||
/// <see cref="IFrameAssetLocator"/> (the server refuses to start without them), may add
|
||||
/// <see cref="Chronos.Data.Jobs.IJobHandler"/>s, and may resolve <see cref="ChronosDatabase"/>, <see cref="ServerArchive"/>
|
||||
/// and <see cref="ServerOptions"/> from DI in its factories.
|
||||
/// and <see cref="ServerOptions"/> from DI in its factories. <see cref="BuildHosted"/> builds the same server over the index,
|
||||
/// engines and scheduler of Chronos Desktop, which shares its library with it (owner review 5, #67).
|
||||
/// </summary>
|
||||
public static class ServerHost
|
||||
{
|
||||
@@ -61,19 +62,7 @@ public static class ServerHost
|
||||
ServerId = ServerIdAsync(database, bootLogger, CancellationToken.None).GetAwaiter().GetResult(),
|
||||
};
|
||||
|
||||
builder.WebHost.ConfigureKestrel(k =>
|
||||
{
|
||||
k.AddServerHeader = false;
|
||||
k.Limits.MaxRequestBodySize = JsonBodyLimit;
|
||||
k.Limits.RequestHeadersTimeout = TimeSpan.FromSeconds(30);
|
||||
k.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(2);
|
||||
k.ListenAnyIP(options.HttpPort);
|
||||
if (certificate is not null) k.ListenAnyIP(options.HttpsPort, l =>
|
||||
{
|
||||
l.Protocols = HttpProtocols.Http1AndHttp2;
|
||||
l.UseHttps(certificate);
|
||||
});
|
||||
});
|
||||
ConfigureKestrel(builder, options, certificate);
|
||||
ConfigureServices(builder.Services, options, state, database, archive);
|
||||
|
||||
var app = builder.Build();
|
||||
@@ -118,6 +107,69 @@ public static class ServerHost
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The server Chronos Desktop runs in its own process to share this PC's library (owner review 5, #67): the same API, admin
|
||||
/// panel, accounts, TLS, discovery, uploads, sync, events and render nodes as a server of its own, over the desktop's
|
||||
/// index, archive service, processing service, job engine and scheduler (<paramref name="engine"/>), which it never
|
||||
/// creates, starts, stops or disposes. Its own state is in <see cref="ServerOptions.DataPath"/> (made private first);
|
||||
/// the first administrator is made there as on a new server. It sets no process-wide log sink or level and hooks no
|
||||
/// console signal: the desktop's stay. Runs synchronously: call it off the UI thread.
|
||||
/// </summary>
|
||||
public static HostedServer BuildHosted(ServerOptions options, HostedEngine engine)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
ArgumentNullException.ThrowIfNull(engine);
|
||||
if (!options.Hosted) throw new ArgumentException("The options are not those of a hosted server (ServerOptions.ForHosted).", nameof(options));
|
||||
DataFolderAccess.Restrict(options.DataPath);
|
||||
Directory.CreateDirectory(options.LogPath);
|
||||
Directory.CreateDirectory(options.UploadsPath);
|
||||
|
||||
// The desktop's command line and environment are not the server's.
|
||||
var builder = WebApplication.CreateSlimBuilder(new WebApplicationOptions { Args = [], ContentRootPath = AppContext.BaseDirectory });
|
||||
var logging = new ServerLogging(options, setsEngineLevel: false);
|
||||
logging.AddTo(builder.Logging);
|
||||
builder.Services.AddSingleton(logging);
|
||||
|
||||
var bootLogger = new FileLoggerProvider(logging).CreateLogger("Chronos.Server");
|
||||
var certificate = CertificateStore.Load(options, bootLogger);
|
||||
AdminBootstrap.RunAsync(engine.Database, options, TimeProvider.System, bootLogger, CancellationToken.None).GetAwaiter().GetResult();
|
||||
var state = new ServerState
|
||||
{
|
||||
TlsFingerprint = certificate is null ? null : CertificateStore.Fingerprint(certificate),
|
||||
Database = ServerDatabase.DescribeAsync(engine.Database, CancellationToken.None).GetAwaiter().GetResult(),
|
||||
ServerId = ServerIdAsync(engine.Database, bootLogger, CancellationToken.None).GetAwaiter().GetResult(),
|
||||
};
|
||||
var archive = ServerArchive.ForArchive(engine.Archive);
|
||||
|
||||
ConfigureKestrel(builder, options, certificate);
|
||||
ConfigureServices(builder.Services, options, state, engine.Database, archive, engine);
|
||||
|
||||
var app = builder.Build();
|
||||
ConfigurePipeline(app, options);
|
||||
app.Logger.LogInformation("Chronos Server {Version} in Chronos Desktop · archive {Archive} · data {Data} · TLS {Tls}{Fingerprint}",
|
||||
SystemEndpoints.Version, archive.Root, options.DataPath, options.TlsMode,
|
||||
state.TlsFingerprint is null ? string.Empty : " · SHA-256 " + state.TlsFingerprint);
|
||||
if (archive.Structure is { IsComplete: false } structure)
|
||||
app.Logger.LogWarning("The archive's folders are incomplete: {Problem} {Detail}", structure.Message, structure.Detail ?? string.Empty);
|
||||
return new HostedServer(app, options, state, logging);
|
||||
}
|
||||
|
||||
/// <summary>Kestrel on every interface: the HTTP port, and the HTTPS port with HTTP/2 when there is a certificate.</summary>
|
||||
private static void ConfigureKestrel(WebApplicationBuilder builder, ServerOptions options, System.Security.Cryptography.X509Certificates.X509Certificate2? certificate) =>
|
||||
builder.WebHost.ConfigureKestrel(k =>
|
||||
{
|
||||
k.AddServerHeader = false;
|
||||
k.Limits.MaxRequestBodySize = JsonBodyLimit;
|
||||
k.Limits.RequestHeadersTimeout = TimeSpan.FromSeconds(30);
|
||||
k.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(2);
|
||||
k.ListenAnyIP(options.HttpPort);
|
||||
if (certificate is not null) k.ListenAnyIP(options.HttpsPort, l =>
|
||||
{
|
||||
l.Protocols = HttpProtocols.Http1AndHttp2;
|
||||
l.UseHttps(certificate);
|
||||
});
|
||||
});
|
||||
|
||||
/// <summary>The app setting that holds the server's stable id (<see cref="ServerInfo.ServerId"/>).</summary>
|
||||
public const string ServerIdKey = "server.id";
|
||||
|
||||
@@ -153,8 +205,12 @@ public static class ServerHost
|
||||
return id;
|
||||
}
|
||||
|
||||
/// <summary>The single composition root of the server's services.</summary>
|
||||
public static void ConfigureServices(IServiceCollection services, ServerOptions options, ServerState state, ChronosDatabase database, ServerArchive archive)
|
||||
/// <summary>
|
||||
/// The single composition root of the server's services; with <paramref name="hosted"/>, those of the library Chronos
|
||||
/// Desktop shares, whose engines, engine and scheduler are the desktop's (owner review 5, #67).
|
||||
/// </summary>
|
||||
public static void ConfigureServices(IServiceCollection services, ServerOptions options, ServerState state, ChronosDatabase database, ServerArchive archive,
|
||||
HostedEngine? hosted = null)
|
||||
{
|
||||
services.AddSingleton(options);
|
||||
services.AddSingleton(state);
|
||||
@@ -165,7 +221,9 @@ public static class ServerHost
|
||||
services.TryAddSingleton<VolumeSpace>();
|
||||
services.ConfigureHttpJsonOptions(j => j.SerializerOptions.TypeInfoResolverChain.Insert(0, ProtocolJson.Default));
|
||||
services.Configure<RouteHandlerOptions>(o => o.ThrowOnBadRequest = true);
|
||||
services.Configure<HostOptions>(o => o.ShutdownTimeout = TimeSpan.FromSeconds(30));
|
||||
// The desktop's exit waits for everything it runs: the server it hosts stops within a few seconds.
|
||||
services.Configure<HostOptions>(o => o.ShutdownTimeout = TimeSpan.FromSeconds(hosted is null ? 30 : 5));
|
||||
if (hosted is not null) RegisterHosted(services, hosted);
|
||||
if (options.TlsMode == TlsMode.Proxy)
|
||||
{
|
||||
// Only the configured proxies may say who the client is: a client that reaches the HTTP port directly would
|
||||
@@ -192,18 +250,24 @@ public static class ServerHost
|
||||
services.AddSingleton<SequenceJobs>();
|
||||
services.AddSingleton<PlanCache>();
|
||||
services.AddSingleton<RenderNodeService>();
|
||||
services.AddSingleton<JobEngineHost>();
|
||||
services.AddSingleton<ClientActivity>();
|
||||
if (hosted is null) services.AddSingleton<JobEngineHost>();
|
||||
else services.AddSingleton(sp => new JobEngineHost(hosted.Engine, sp.GetRequiredService<EventHub>()));
|
||||
services.AddHostedService(sp => sp.GetRequiredService<JobEngineHost>());
|
||||
// After the engine's host: the scheduler queues on its engine, starts after it and stops before it (owner review 2, R10).
|
||||
services.AddSingleton<ServerScheduleHost>();
|
||||
services.AddSingleton<SchedulerHost>();
|
||||
if (hosted is null) services.AddSingleton<SchedulerHost>();
|
||||
else services.AddSingleton(sp => new SchedulerHost(database, hosted.Scheduler, sp.GetRequiredService<ServerScheduleHost>(), options,
|
||||
sp.GetRequiredService<EventHub>(), sp.GetRequiredService<TimeProvider>(), sp.GetRequiredService<ILogger<SchedulerHost>>()));
|
||||
services.AddHostedService(sp => sp.GetRequiredService<SchedulerHost>());
|
||||
services.AddSingleton<RenderNodeMonitor>();
|
||||
services.AddHostedService(sp => sp.GetRequiredService<RenderNodeMonitor>());
|
||||
// Recovers interrupted commits at start; its sweep is the clean-up step of the upload-cleanup schedule.
|
||||
services.AddSingleton<UploadMaintenance>();
|
||||
services.AddHostedService(sp => sp.GetRequiredService<UploadMaintenance>());
|
||||
services.AddSingleton<ICleanupStep, UploadCleanupStep>();
|
||||
// The desktop's archive service holds a step of its own that runs this one while it shares its library.
|
||||
services.AddSingleton<UploadCleanupStep>();
|
||||
services.AddSingleton<ICleanupStep>(sp => sp.GetRequiredService<UploadCleanupStep>());
|
||||
// Main takes settings written outside the projects soon after they are (owner review 2, R8).
|
||||
services.AddSingleton<ProjectCatchUp>();
|
||||
services.AddHostedService(sp => sp.GetRequiredService<ProjectCatchUp>());
|
||||
@@ -211,6 +275,22 @@ public static class ServerHost
|
||||
services.AddHostedService(sp => sp.GetRequiredService<DiscoveryBeaconService>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The desktop's engines as the archive and processing modules would register them, and a lifetime that hooks no console
|
||||
/// signal of the desktop's process (owner review 5, #67).
|
||||
/// </summary>
|
||||
private static void RegisterHosted(IServiceCollection services, HostedEngine hosted)
|
||||
{
|
||||
services.AddSingleton(hosted);
|
||||
services.AddSingleton(hosted.Archives);
|
||||
services.AddSingleton<IArchiveIngest>(hosted.Archives.Ingest);
|
||||
services.AddSingleton<Chronos.Data.Videos.IVideoLibrary>(hosted.Archives.Videos);
|
||||
services.AddSingleton<IFrameAssetLocator, ArchiveAssetLocator>();
|
||||
services.AddSingleton(hosted.Processing);
|
||||
services.AddSingleton<RenderNodeAvailability>();
|
||||
services.AddSingleton<IHostLifetime, DesktopHostLifetime>();
|
||||
}
|
||||
|
||||
private static void ConfigurePipeline(WebApplication app, ServerOptions options)
|
||||
{
|
||||
if (options.TlsMode == TlsMode.Proxy) app.UseForwardedHeaders();
|
||||
@@ -221,6 +301,8 @@ public static class ServerHost
|
||||
|
||||
AdminPanel.Map(app, options);
|
||||
var api = app.MapGroup("/api/v1");
|
||||
// A sequence, frame, render or video of another archive of the index answers as an unknown id (owner review 5, #67).
|
||||
api.AddEndpointFilter(ServedItems.FilterAsync);
|
||||
SystemEndpoints.Map(app, api);
|
||||
AuthEndpoints.Map(api);
|
||||
UserEndpoints.Map(api);
|
||||
|
||||
Reference in new issue
Block a user