merge: or5 parcel p4
# Conflicts: # src/Chronos.App/Sections/ServerView.xaml # src/Chronos.App/Sections/ServerViewModel.cs # src/Chronos.Server/Jobs/JobEngineHost.cs
This commit is contained in:
commit
e367f03c80
127 files changed
+6822
-1117
No files matched your search
@@ -15,18 +15,21 @@ namespace Chronos.Server.Jobs;
|
||||
/// 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 (<see cref="ISelectiveJobHandler"/>: 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.
|
||||
/// When Chronos Desktop shares its library (owner review 5, #67) the engine is the desktop's own: this host relays it while the
|
||||
/// server runs, never starts, stops or disposes it, and the jobs API shows only the kinds a server runs (<see cref="Exposes"/>):
|
||||
/// the desktop's workspace, sync and download jobs stay its own.
|
||||
/// Progress goes out through the <see cref="Gate"/> render nodes share, with what each job does now (owner review 5, #65).
|
||||
/// </summary>
|
||||
public sealed class JobEngineHost : IHostedService
|
||||
{
|
||||
private readonly ChronosDatabase _db;
|
||||
private readonly EventHub _events;
|
||||
private readonly HashSet<JobKind> _kinds;
|
||||
private readonly bool _ownsEngine = true;
|
||||
private int _subscribed;
|
||||
|
||||
public JobEngineHost(ChronosDatabase db, IEnumerable<IJobHandler> handlers, IEnumerable<IJobHandlerProvider> providers, ServerOptions options,
|
||||
EventHub events, TimeProvider time)
|
||||
{
|
||||
_db = db;
|
||||
_events = events;
|
||||
Gate = new ProgressGate(time);
|
||||
// A render handler must be selective: a plain one would take the jobs that need a render node's encoder.
|
||||
@@ -39,8 +42,21 @@ public sealed class JobEngineHost : IHostedService
|
||||
Kinds = _kinds,
|
||||
PriorityQueue = () => ServerSettingsStore.PriorityQueue(options),
|
||||
});
|
||||
Engine.JobChanged += OnJobChanged;
|
||||
Engine.Progress += OnProgress;
|
||||
Subscribe();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Relays <paramref name="engine"/>, an engine Chronos Desktop runs (owner review 5, #67), from <see cref="StartAsync"/> to
|
||||
/// <see cref="StopAsync"/>; the engine stays the desktop's.
|
||||
/// </summary>
|
||||
public JobEngineHost(JobEngine engine, EventHub events, TimeProvider time)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(engine);
|
||||
_events = events;
|
||||
Gate = new ProgressGate(time);
|
||||
_ownsEngine = false;
|
||||
_kinds = [];
|
||||
Engine = engine;
|
||||
}
|
||||
|
||||
/// <summary>Node id stamped on jobs this server runs.</summary>
|
||||
@@ -51,15 +67,60 @@ public sealed class JobEngineHost : IHostedService
|
||||
/// <summary>The pace of every job's progress events, the server's own jobs and render nodes' alike.</summary>
|
||||
public ProgressGate Gate { get; }
|
||||
|
||||
/// <summary>The engine is the one Chronos Desktop runs, which shares its library (owner review 5, #67).</summary>
|
||||
public bool Hosted => !_ownsEngine;
|
||||
|
||||
/// <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 bool CanRun(JobKind kind) => kind == JobKind.Render || (_ownsEngine ? _kinds.Contains(kind) : Engine.CanRun(kind) && Exposes(kind));
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken) => Engine.StartAsync(cancellationToken);
|
||||
/// <summary>
|
||||
/// Whether the jobs API lists, shows and acts on jobs of <paramref name="kind"/>: every kind on a server of its own; on a
|
||||
/// shared desktop library only the kinds a server runs (<see cref="ServerSettingsStore.Kinds"/>), so a client never sees or
|
||||
/// cancels the desktop's own workspace, sync and download jobs (owner review 5, #67).
|
||||
/// </summary>
|
||||
public bool Exposes(JobKind kind) => _ownsEngine || ServerSettingsStore.Kinds.Contains(kind);
|
||||
|
||||
public async Task StopAsync(CancellationToken cancellationToken) => await Engine.DisposeAsync();
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (_ownsEngine) return Engine.StartAsync(cancellationToken);
|
||||
Subscribe();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (_ownsEngine)
|
||||
{
|
||||
await Engine.DisposeAsync();
|
||||
return;
|
||||
}
|
||||
// The desktop's engine runs on: only this server stops following it.
|
||||
if (Interlocked.Exchange(ref _subscribed, 0) == 1)
|
||||
{
|
||||
Engine.JobChanged -= OnJobChanged;
|
||||
Engine.Progress -= OnProgress;
|
||||
}
|
||||
}
|
||||
|
||||
private void Subscribe()
|
||||
{
|
||||
if (Interlocked.Exchange(ref _subscribed, 1) == 1) return;
|
||||
Engine.JobChanged += OnJobChanged;
|
||||
Engine.Progress += OnProgress;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A job's state changed outside the engine (render nodes, cancellation of node jobs): Chronos Desktop's Jobs section
|
||||
/// follows these as it follows its engine's own changes while it shares its library (owner review 5, #67).
|
||||
/// </summary>
|
||||
public event Action<Job>? ChangedOutside;
|
||||
|
||||
/// <summary>Publishes a state change made outside the engine (render nodes, cancellation of node jobs).</summary>
|
||||
public void Publish(Job job) => OnJobChanged(job);
|
||||
public void Publish(Job job)
|
||||
{
|
||||
OnJobChanged(job);
|
||||
ChangedOutside?.Invoke(job);
|
||||
}
|
||||
|
||||
private void OnJobChanged(Job job)
|
||||
{
|
||||
|
||||
@@ -19,7 +19,10 @@ namespace Chronos.Server.Jobs;
|
||||
/// retention in force is the audit clean-up schedule's age limit, 0 while it is off or without its audit step:
|
||||
/// Settings › Logs shows it (<see cref="AuditRetentionAsync"/>) and sets it (<see cref="ApplyAuditRetentionAsync"/>);
|
||||
/// <see cref="ServerOptions.AuditRetentionDays"/> is only what the schedule is seeded and reset with, or what an environment
|
||||
/// variable fixes. Every schedule change and run goes out as a <see cref="ScheduleChangedEvent"/>.
|
||||
/// variable fixes. Every schedule change and run goes out as a <see cref="ScheduleChangedEvent"/>. On the library Chronos
|
||||
/// Desktop shares (owner review 5, #67) the scheduler is the desktop's, already running: this host follows it while the
|
||||
/// server runs, adds the server's own system schedules to it (<see cref="ServerScheduleHost.ShareSchedules"/>) and never
|
||||
/// starts, seeds the desktop's schedules for or disposes it.
|
||||
/// </summary>
|
||||
public sealed class SchedulerHost : IHostedService
|
||||
{
|
||||
@@ -29,6 +32,10 @@ public sealed class SchedulerHost : IHostedService
|
||||
private readonly CancellationTokenSource _stopping = new();
|
||||
private Task _startScan = Task.CompletedTask;
|
||||
private Task _refresh = Task.CompletedTask;
|
||||
private readonly bool _ownsScheduler = true;
|
||||
private readonly Action<ScheduleDto> _changed;
|
||||
private readonly Action<Guid> _deleted;
|
||||
private readonly int _auditRetentionDays;
|
||||
private int _stopped;
|
||||
|
||||
public SchedulerHost(ChronosDatabase db, JobEngineHost jobs, ServerScheduleHost host, EventHub events, TimeProvider time, ILogger<SchedulerHost> logger)
|
||||
@@ -41,8 +48,31 @@ public sealed class SchedulerHost : IHostedService
|
||||
Host = host;
|
||||
Scheduler = new Scheduler(db, jobs.Engine, new SchedulerOptions { Time = time });
|
||||
// The Scheduled tab of the admin panel follows them, the runs that queue no job included.
|
||||
Scheduler.ScheduleChanged += schedule => events.Publish(new ScheduleChangedEvent(schedule.Id, Deleted: false));
|
||||
Scheduler.ScheduleDeleted += id => events.Publish(new ScheduleChangedEvent(id, Deleted: true));
|
||||
_changed = schedule => events.Publish(new ScheduleChangedEvent(schedule.Id, Deleted: false));
|
||||
_deleted = id => events.Publish(new ScheduleChangedEvent(id, Deleted: true));
|
||||
Scheduler.ScheduleChanged += _changed;
|
||||
Scheduler.ScheduleDeleted += _deleted;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Follows <paramref name="scheduler"/>, the running scheduler of Chronos Desktop (owner review 5, #67), from
|
||||
/// <see cref="StartAsync"/> to <see cref="StopAsync"/>.
|
||||
/// </summary>
|
||||
public SchedulerHost(ChronosDatabase db, Scheduler scheduler, ServerScheduleHost host, ServerOptions options, EventHub events, TimeProvider time,
|
||||
ILogger<SchedulerHost> logger)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(scheduler);
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
ArgumentNullException.ThrowIfNull(events);
|
||||
_db = db;
|
||||
_time = time;
|
||||
_logger = logger;
|
||||
_ownsScheduler = false;
|
||||
_auditRetentionDays = options.AuditRetentionDays;
|
||||
Host = host;
|
||||
Scheduler = scheduler;
|
||||
_changed = schedule => events.Publish(new ScheduleChangedEvent(schedule.Id, Deleted: false));
|
||||
_deleted = id => events.Publish(new ScheduleChangedEvent(id, Deleted: true));
|
||||
}
|
||||
|
||||
public Scheduler Scheduler { get; }
|
||||
@@ -51,10 +81,21 @@ public sealed class SchedulerHost : IHostedService
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var started = Scheduler.Options.Time.GetUtcNow();
|
||||
await Scheduler.StartAsync(Host, ApplyLockedAgesAsync, cancellationToken);
|
||||
var stopping = _stopping.Token;
|
||||
_startScan = Task.Run(() => ScanAtStartAsync(started, stopping), CancellationToken.None);
|
||||
if (!_ownsScheduler)
|
||||
{
|
||||
Scheduler.ScheduleChanged += _changed;
|
||||
Scheduler.ScheduleDeleted += _deleted;
|
||||
// The desktop seeds its own system schedules; the server's state brings two more (never overwritten once there).
|
||||
if (await _db.Schedules.EnsureSystemAsync(ServerScheduleHost.ShareSchedules(_auditRetentionDays), _time.GetUtcNow(), cancellationToken) > 0)
|
||||
Scheduler.Wake();
|
||||
}
|
||||
else
|
||||
{
|
||||
var started = Scheduler.Options.Time.GetUtcNow();
|
||||
await Scheduler.StartAsync(Host, ApplyLockedAgesAsync, cancellationToken);
|
||||
_startScan = Task.Run(() => ScanAtStartAsync(started, stopping), CancellationToken.None);
|
||||
}
|
||||
// Created here, so its first period counts from the start.
|
||||
var timer = new PeriodicTimer(TimeSpan.FromMinutes(ServerScheduleHost.ScanMinutes), _time);
|
||||
_refresh = Task.Run(() => RefreshArchiveAsync(timer, stopping), CancellationToken.None);
|
||||
@@ -66,7 +107,16 @@ public sealed class SchedulerHost : IHostedService
|
||||
await _stopping.CancelAsync();
|
||||
await _startScan;
|
||||
await _refresh;
|
||||
await Scheduler.DisposeAsync();
|
||||
if (_ownsScheduler)
|
||||
{
|
||||
await Scheduler.DisposeAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
// The desktop's scheduler runs on: only this server stops following it.
|
||||
Scheduler.ScheduleChanged -= _changed;
|
||||
Scheduler.ScheduleDeleted -= _deleted;
|
||||
}
|
||||
_stopping.Dispose();
|
||||
}
|
||||
|
||||
|
||||
@@ -66,13 +66,13 @@ public sealed class ServerScheduleHost : IScheduleHost
|
||||
{
|
||||
get
|
||||
{
|
||||
var nightly = (Backup: new TimeOnly(3, 0), History: new TimeOnly(4, 0), Audit: new TimeOnly(4, 30));
|
||||
var nightly = (Backup: new TimeOnly(3, 0), History: new TimeOnly(4, 0));
|
||||
var share = ShareSchedules(_options.AuditRetentionDays);
|
||||
var list = new List<SystemSchedule>
|
||||
{
|
||||
// A missed scan is not caught up: the next one comes within minutes and SchedulerHost scans once at start.
|
||||
new(ArchiveScan, "Archive scan", ScheduleActions.Scan, ScheduleRecurrence.Interval(ScanMinutes, RecurrenceUnit.Minutes), CatchUp: false),
|
||||
new(UploadCleanup, "Upload clean-up", ScheduleActions.Cleanup, ScheduleRecurrence.Interval(UploadSweepHours, RecurrenceUnit.Hours),
|
||||
new ScheduleParametersDto(OlderThanDays: UploadMaintenance.FinishedRetentionDays, Steps: [UploadCleanupStep.StepName])),
|
||||
share[0],
|
||||
};
|
||||
// PostgreSQL is backed up with its own tools (BRIEF §6.2): no schedule that could never run. Seeded due at once
|
||||
// when the newest copy is a day old, as the daily loop it replaced made one at start.
|
||||
@@ -83,13 +83,24 @@ public sealed class ServerScheduleHost : IScheduleHost
|
||||
}
|
||||
list.Add(new(Housekeeping, "Housekeeping", ScheduleActions.Cleanup, ScheduleRecurrence.DailyAt(nightly.History),
|
||||
new ScheduleParametersDto(OlderThanDays: _options.JobHistoryDays, Steps: [Chronos.Data.Housekeeping.CleanupSteps.JobHistory])));
|
||||
var audit = _options.AuditRetentionDays;
|
||||
list.Add(new(AuditCleanup, "Audit log clean-up", ScheduleActions.Cleanup, ScheduleRecurrence.DailyAt(nightly.Audit),
|
||||
new ScheduleParametersDto(OlderThanDays: audit, Steps: [Chronos.Data.Housekeeping.CleanupSteps.Audit]), Enabled: audit > 0));
|
||||
list.Add(share[1]);
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The system schedules of a server's own state, which Chronos Desktop adds to its own while it shares its library
|
||||
/// (owner review 5, #67), the archive scan, the index backup and the housekeeping being the desktop's: the uploads sweep
|
||||
/// every <see cref="UploadSweepHours"/> hours and the audit log clean-up, nightly at 04:30, off at 0 days.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<SystemSchedule> ShareSchedules(int auditRetentionDays) =>
|
||||
[
|
||||
new(UploadCleanup, "Upload clean-up", ScheduleActions.Cleanup, ScheduleRecurrence.Interval(UploadSweepHours, RecurrenceUnit.Hours),
|
||||
new ScheduleParametersDto(OlderThanDays: UploadMaintenance.FinishedRetentionDays, Steps: [UploadCleanupStep.StepName])),
|
||||
new(AuditCleanup, "Audit log clean-up", ScheduleActions.Cleanup, ScheduleRecurrence.DailyAt(new TimeOnly(4, 30)),
|
||||
new ScheduleParametersDto(OlderThanDays: auditRetentionDays, Steps: [Chronos.Data.Housekeeping.CleanupSteps.Audit]), Enabled: auditRetentionDays > 0),
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// The served archive, after taking the settings another writer sharing it (a desktop) put into its archive.json, as
|
||||
/// the former polling did; while it is offline, or the file cannot be read, as the index has it.
|
||||
|
||||
Reference in new issue
Block a user