merge: full Titano Server (auth, users, uploads, sync, jobs, render nodes, events, admin panel, Windows service)
# Conflicts: # i18n/en/Server.resx # i18n/it/Server.it.resx # src/Titano.Data/Repositories/FrameRepository.cs
This commit is contained in:
104 files changed
+8879
-250
No files matched your search
@@ -0,0 +1,82 @@
|
||||
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 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));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user