feat(server): API v1 with auth, uploads, sync, jobs, render nodes and events

- Security: PBKDF2-SHA512 (210,000 iterations) hashes, opaque bearer tokens
  stored as SHA-256 with sliding expiry and revocation, HttpOnly/Secure/
  SameSite=Strict admin cookie with a session-bound CSRF token, sign-in rate
  limit and lockout, role/permission metadata on every endpoint, audit log,
  first-run administrator bootstrap.
- Sequences with keyset paging, ETags and settings_version conflicts; sync
  manifest; frames, proxies, thumbnails and exports streamed with Range.
- Chunked resumable uploads with hash short-circuit, quota and disk checks,
  staging on the archive volume, SHA-256 verification and IArchiveIngest.
- Job engine host, jobs API, render nodes with heartbeat timeout and
  reassignment, WebSocket events, LAN discovery beacon, upload cleanup.
- Windows service host over advapi32 (--service, --install-service,
  --uninstall-service); fails fast without the archive module services.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 11:32:19 +00:00
1 parent 7353c17d7b
commit c78747053e
56 files changed
+4726 -108

No files matched your search

+87
View File
@@ -0,0 +1,87 @@
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 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, ServerOptions options, EventHub events)
{
_db = db;
_events = events;
var local = handlers.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 async Task StartAsync(CancellationToken cancellationToken)
{
// The engine requeues only its exact worker id; numbered workers of the previous run are requeued here.
await _db.Jobs.RequeueRunningWorkersAsync(WorkerId, cancellationToken);
await 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));
}
}