ArchiveModule registers the archive engine as the server's ingest, its proxy/thumbnail locator and its job handlers (through the new IJobHandlerProvider); Program.cs passes it to ServerHost.Build. The server opens its archive through ArchiveService so archive.json always exists. The server's path-safety helper is renamed ArchivePathGuard to stop clashing with the archive layout's ArchivePaths, and the server migration is renumbered 0003. An end-to-end test uploads a photo, finds it in the layout and fetches the thumbnail built by the queued job. CI's PostgreSQL jobs now run the server tests too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
85 lines
3.5 KiB
C#
85 lines
3.5 KiB
C#
using System.Diagnostics;
|
|
using Titano.Core.Model;
|
|
using Titano.Data;
|
|
using Titano.Data.Jobs;
|
|
using Titano.Protocol;
|
|
using Titano.Server.Api;
|
|
using Titano.Server.Configuration;
|
|
using Titano.Server.Events;
|
|
|
|
namespace Titano.Server.Jobs;
|
|
|
|
/// <summary>
|
|
/// Hosts the server's <see cref="JobEngine"/> (BRIEF §14.6) with the <see cref="IJobHandler"/>s registered in DI (directly or
|
|
/// through <see cref="IJobHandlerProvider"/>s) and
|
|
/// relays its state changes and progress as WebSocket events. Render jobs are never run here: render nodes claim them
|
|
/// (BRIEF §14.7). On shutdown running jobs stay marked running and are requeued from their checkpoints at the next start.
|
|
/// </summary>
|
|
public sealed class JobEngineHost : IHostedService
|
|
{
|
|
private static readonly long ProgressInterval = Stopwatch.Frequency / 4;
|
|
|
|
private readonly TitanoDatabase _db;
|
|
private readonly EventHub _events;
|
|
private readonly HashSet<JobKind> _kinds;
|
|
private readonly Dictionary<Guid, long> _lastProgress = [];
|
|
private readonly Lock _gate = new();
|
|
|
|
public JobEngineHost(TitanoDatabase db, IEnumerable<IJobHandler> handlers, IEnumerable<IJobHandlerProvider> providers, ServerOptions options,
|
|
EventHub events)
|
|
{
|
|
_db = db;
|
|
_events = events;
|
|
var local = handlers.Concat(providers.SelectMany(p => p.CreateJobHandlers())).Where(h => h.Kind != JobKind.Render).ToList();
|
|
_kinds = [.. local.Select(h => h.Kind)];
|
|
// Without handlers no worker starts: an engine with an empty kind list would claim every kind.
|
|
Workers = _kinds.Count == 0 ? 0 : options.Workers;
|
|
Engine = new JobEngine(db, local, new JobEngineOptions
|
|
{
|
|
Workers = Workers,
|
|
WorkerId = WorkerId,
|
|
Kinds = _kinds,
|
|
HistoryRetention = TimeSpan.FromDays(options.JobHistoryDays),
|
|
});
|
|
Engine.JobChanged += OnJobChanged;
|
|
Engine.Progress += OnProgress;
|
|
}
|
|
|
|
/// <summary>Node id stamped on jobs this server runs (its workers use <c>WorkerId#n</c>).</summary>
|
|
public static string WorkerId { get; } = "server:" + Environment.MachineName;
|
|
|
|
public JobEngine Engine { get; }
|
|
|
|
public int Workers { get; }
|
|
|
|
/// <summary>True when a job of <paramref name="kind"/> has somewhere to run: a local handler, or render nodes for renders.</summary>
|
|
public bool CanRun(JobKind kind) => kind == JobKind.Render || _kinds.Contains(kind);
|
|
|
|
public Task StartAsync(CancellationToken cancellationToken) => Engine.StartAsync(cancellationToken);
|
|
|
|
public async Task StopAsync(CancellationToken cancellationToken) => await Engine.DisposeAsync();
|
|
|
|
/// <summary>Publishes a state change made outside the engine (render nodes, cancellation of node jobs).</summary>
|
|
public void Publish(Job job) => OnJobChanged(job);
|
|
|
|
private void OnJobChanged(Job job)
|
|
{
|
|
if (job.State != JobState.Running)
|
|
{
|
|
lock (_gate) _lastProgress.Remove(job.Id);
|
|
}
|
|
_events.Publish(new JobStateEvent(job.Id, JobWire.State(job.State), job.Error));
|
|
}
|
|
|
|
private void OnProgress(JobProgressInfo progress)
|
|
{
|
|
var now = Stopwatch.GetTimestamp();
|
|
lock (_gate)
|
|
{
|
|
if (_lastProgress.TryGetValue(progress.JobId, out var last) && now - last < ProgressInterval && progress.Done < progress.Total) return;
|
|
_lastProgress[progress.JobId] = now;
|
|
}
|
|
_events.Publish(new JobProgressEvent(progress.JobId, progress.Done, progress.Total, progress.ItemsPerSecond, progress.Eta?.TotalSeconds));
|
|
}
|
|
}
|