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; /// /// Hosts the server's (BRIEF §14.6) with the s registered in DI (directly or /// through 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. /// public sealed class JobEngineHost : IHostedService { private static readonly long ProgressInterval = Stopwatch.Frequency / 4; private readonly TitanoDatabase _db; private readonly EventHub _events; private readonly HashSet _kinds; private readonly Dictionary _lastProgress = []; private readonly Lock _gate = new(); public JobEngineHost(TitanoDatabase db, IEnumerable handlers, IEnumerable 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; } /// Node id stamped on jobs this server runs (its workers use WorkerId#n). public static string WorkerId { get; } = "server:" + Environment.MachineName; public JobEngine Engine { get; } public int Workers { get; } /// True when a job of has somewhere to run: a local handler, or render nodes for renders. 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(); /// Publishes a state change made outside the engine (render nodes, cancellation of node jobs). 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)); } }