using System.Diagnostics;
using Chronos.Core.Model;
using Chronos.Data;
using Chronos.Data.Jobs;
using Chronos.Protocol;
using Chronos.Server.Api;
using Chronos.Server.Configuration;
using Chronos.Server.Events;
namespace Chronos.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. Jobs run one at a time, the parallelism working inside the
/// running one, in the order of the priority queue of the settings (owner review 1). Render jobs run here only when the registered render
/// handler accepts them (: managed formats, and MP4 jobs no render node can take);
/// render nodes claim the others (BRIEF §14.5, §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 ChronosDatabase _db;
private readonly EventHub _events;
private readonly HashSet _kinds;
private readonly Dictionary _lastProgress = [];
private readonly Lock _gate = new();
public JobEngineHost(ChronosDatabase db, IEnumerable handlers, IEnumerable providers, ServerOptions options,
EventHub events)
{
_db = db;
_events = events;
// A render handler must be selective: a plain one would take the jobs that need a render node's encoder.
var local = handlers.Concat(providers.SelectMany(p => p.CreateJobHandlers())).Where(h => h.Kind != JobKind.Render || h is ISelectiveJobHandler).ToList();
_kinds = [.. local.Select(h => h.Kind)];
// Without handlers no worker starts (StartAsync): an engine with an empty kind list would claim every kind.
Engine = new JobEngine(db, local, new JobEngineOptions
{
WorkerId = WorkerId,
Kinds = _kinds,
HistoryRetention = TimeSpan.FromDays(options.JobHistoryDays),
PriorityQueue = () => ServerSettingsStore.PriorityQueue(options),
});
Engine.JobChanged += OnJobChanged;
Engine.Progress += OnProgress;
}
/// Node id stamped on jobs this server runs.
public static string WorkerId { get; } = "server:" + Environment.MachineName;
public JobEngine Engine { 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));
}
}