feat: owner review 2 phase 5b: the server as a project home (#17)
CI / Build and test (push) Successful in 9m58s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 6s
Release / Build, test and publish (push) Successful in 14m8s
CI / PostgreSQL 16 integration (push) Successful in 8m55s
CI / PostgreSQL 17 integration (push) Successful in 8m50s
CI / Docker image (push) Successful in 57s
Release / Server image (push) Successful in 1m22s
CI / Build and test (push) Successful in 9m58s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 6s
Release / Build, test and publish (push) Successful in 14m8s
CI / PostgreSQL 16 integration (push) Successful in 8m55s
CI / PostgreSQL 17 integration (push) Successful in 8m50s
CI / Docker image (push) Successful in 57s
Release / Server image (push) Successful in 1m22s
A Chronos Server keeps its sequences' projects, as a PC's archive does: Main and named projects with versions under /api/v1/sequences/{id}/projects, the projects capability and the projectChanged event. The desktop opens a server's sequences in the workspace (proxies downloaded, hash-checked, resumable) with Save version, Sync, conflicts, History, Restore and the project actions against the server. Renders can be of a project version. The admin panel lists a sequence's projects and versions. Strings in all 38 languages.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
This commit was merged in pull request #17.
This commit is contained in:
1 parent
255e149087
commit
81c303f484
325 files changed
+11477
-516
No files matched your search
@@ -38,17 +38,12 @@ namespace Chronos.App.Editor;
|
||||
public sealed class RemoteSequenceSource : ISequenceSource
|
||||
{
|
||||
private const int SaveAttempts = 3;
|
||||
private static readonly TimeSpan MinBackoff = TimeSpan.FromSeconds(2);
|
||||
private static readonly TimeSpan MaxBackoff = TimeSpan.FromMinutes(1);
|
||||
|
||||
private readonly RemoteSequence _remote;
|
||||
private readonly IRemoteSequenceApi _api;
|
||||
private readonly IRenderFrameSource _frames;
|
||||
private readonly IFrameSource _pixels;
|
||||
private readonly ConcurrentDictionary<Guid, Job> _jobs = new();
|
||||
private readonly ConcurrentDictionary<Guid, byte> _foreign = new();
|
||||
private readonly CancellationTokenSource _stopping = new();
|
||||
private readonly Task _listening;
|
||||
private readonly ServerJobs _jobs;
|
||||
private readonly bool _noEditRight;
|
||||
private Stored? _stored;
|
||||
private int _framesVersion = -1;
|
||||
@@ -66,8 +61,7 @@ public sealed class RemoteSequenceSource : ISequenceSource
|
||||
_noEditRight = readOnly;
|
||||
_frames = remote.OpenFrames(decoder);
|
||||
_pixels = (IFrameSource)_frames;
|
||||
var stopping = _stopping.Token;
|
||||
_listening = Task.Run(() => ListenAsync(stopping), CancellationToken.None);
|
||||
_jobs = new ServerJobs(api, remote.Sequence.Id, remote.Sequence.DisplayTitle, job => JobChanged?.Invoke(job), OnEventAsync, OnReconnectedAsync);
|
||||
}
|
||||
|
||||
public bool IsRemote => true;
|
||||
@@ -190,126 +184,44 @@ public sealed class RemoteSequenceSource : ISequenceSource
|
||||
/// The server queues every job at the user's priority; another priority ("Add to queue") is set right after. A
|
||||
/// render embeds the project, whose frame positions mean nothing once the frames changed: refused while outdated.
|
||||
/// </remarks>
|
||||
public async Task<Guid> SubmitAsync(ArchiveJobRequest request, CancellationToken cancellationToken)
|
||||
public Task<Guid> SubmitAsync(ArchiveJobRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(request);
|
||||
if (request.Kind == JobKind.Render && IsOutdated) throw new ProxiesOutdatedException(Loc.L("Editor.Error.FramesChanged"));
|
||||
var dto = await Call(() => _api.CreateJobAsync(new CreateJobRequest(JobWire.Kind(request.Kind), request.Targets, request.ParametersJson), cancellationToken));
|
||||
if (request.Priority != JobPriority.User) dto = await PrioritiseAsync(dto, request.Priority, cancellationToken);
|
||||
// The server's events may have reported the job already; its first state never replaces a later one.
|
||||
if (JobWire.FromDto(dto) is { } job && _jobs.TryAdd(job.Id, job)) JobChanged?.Invoke(job);
|
||||
return dto.Id;
|
||||
return _jobs.SubmitAsync(request, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<Job>> ListJobsAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var active = await Call(() => _api.ListActiveJobsAsync(cancellationToken));
|
||||
var mine = active.Select(JobWire.FromDto).OfType<Job>().Where(Concerns).ToList();
|
||||
foreach (var job in mine) _jobs[job.Id] = job;
|
||||
return mine;
|
||||
}
|
||||
public Task<IReadOnlyList<Job>> ListJobsAsync(CancellationToken cancellationToken) => _jobs.ListAsync(cancellationToken);
|
||||
|
||||
public async Task CancelJobAsync(Guid jobId, CancellationToken cancellationToken)
|
||||
{
|
||||
if (JobWire.FromDto(await _api.CancelJobAsync(jobId, cancellationToken)) is { } job) Publish(job);
|
||||
}
|
||||
public Task CancelJobAsync(Guid jobId, CancellationToken cancellationToken) => _jobs.CancelAsync(jobId, cancellationToken);
|
||||
|
||||
public async Task RetryJobAsync(Guid jobId, CancellationToken cancellationToken)
|
||||
{
|
||||
if (JobWire.FromDto(await _api.RetryJobAsync(jobId, cancellationToken)) is { } job) Publish(job);
|
||||
}
|
||||
public Task RetryJobAsync(Guid jobId, CancellationToken cancellationToken) => _jobs.RetryAsync(jobId, cancellationToken);
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (Interlocked.Exchange(ref _disposed, 1) == 1) return;
|
||||
try
|
||||
{
|
||||
await _stopping.CancelAsync();
|
||||
await _listening;
|
||||
// Stops following the events, and closes the connection.
|
||||
await _jobs.DisposeAsync();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_stopping.Dispose();
|
||||
if (_frames is IAsyncDisposable frames) await frames.DisposeAsync();
|
||||
_api.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Follows the server's events until the editor closes, reconnecting with a growing delay (job states lost meanwhile
|
||||
/// are read back from the jobs, and the frames are compared again). Nothing that fails on the way ends it.
|
||||
/// </summary>
|
||||
private async Task ListenAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var failures = 0;
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
await foreach (var e in _api.EventsAsync(cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
failures = 0;
|
||||
await OnEventAsync(e, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
catch (Exception ex) when (ex is not OutOfMemoryException)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested) return;
|
||||
Log.Info("editor", $"Events of the server lost for {_remote.Sequence.DisplayTitle}: {ex.Message}");
|
||||
}
|
||||
try
|
||||
{
|
||||
var delay = Math.Min(MaxBackoff.Ticks, MinBackoff.Ticks << Math.Min(failures++, 10));
|
||||
await Task.Delay(TimeSpan.FromTicks(delay), cancellationToken).ConfigureAwait(false);
|
||||
await RefreshTrackedAsync(cancellationToken).ConfigureAwait(false);
|
||||
// A frames change announced while the socket was down is found now rather than at the next save.
|
||||
if (Volatile.Read(ref _framesVersion) >= 0) await CheckFramesAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OutOfMemoryException)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested) return;
|
||||
Log.Info("editor", $"Jobs of {_remote.Sequence.DisplayTitle} could not be read again: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The server's events besides the jobs': frames added or removed on the server make the sequence outdated.</summary>
|
||||
private async Task OnEventAsync(ServerEvent e, CancellationToken cancellationToken)
|
||||
{
|
||||
switch (e)
|
||||
{
|
||||
case SequenceChangedEvent changed when changed.SequenceId == Id && changed.FramesVersion != Volatile.Read(ref _framesVersion):
|
||||
await CheckFramesAsync(cancellationToken).ConfigureAwait(false);
|
||||
break;
|
||||
// A late progress event never brings a finished job back to running.
|
||||
case JobProgressEvent progress when _jobs.TryGetValue(progress.JobId, out var job) && job.State is JobState.Queued or JobState.Running:
|
||||
Publish(job with
|
||||
{
|
||||
State = JobState.Running,
|
||||
ProgressDone = progress.Done,
|
||||
ProgressTotal = progress.Total,
|
||||
ItemsPerSecond = progress.ItemsPerSecond,
|
||||
Eta = JobEta.FromSeconds(progress.EtaSeconds),
|
||||
});
|
||||
break;
|
||||
case JobStateEvent state when !_foreign.ContainsKey(state.JobId):
|
||||
await FetchAsync(state.JobId, cancellationToken).ConfigureAwait(false);
|
||||
break;
|
||||
}
|
||||
if (e is SequenceChangedEvent changed && changed.SequenceId == Id && changed.FramesVersion != Volatile.Read(ref _framesVersion))
|
||||
await CheckFramesAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>Reads a job the events named: tracked when it concerns this sequence, remembered as foreign otherwise.</summary>
|
||||
private async Task FetchAsync(Guid jobId, CancellationToken cancellationToken)
|
||||
/// <summary>A frames change announced while the event socket was down is found now rather than at the next save.</summary>
|
||||
private async Task OnReconnectedAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (JobWire.FromDto(await _api.GetJobAsync(jobId, cancellationToken).ConfigureAwait(false)) is { } job && Concerns(job)) Publish(job);
|
||||
else _foreign[jobId] = 0;
|
||||
}
|
||||
catch (Exception ex) when (ex is ChronosApiException or HttpRequestException or JsonException
|
||||
|| (ex is OperationCanceledException && !cancellationToken.IsCancellationRequested))
|
||||
{
|
||||
Log.Info("editor", $"Job {jobId} could not be read: {ex.Message}");
|
||||
}
|
||||
if (Volatile.Read(ref _framesVersion) >= 0) await CheckFramesAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>Frames added or removed on the server: whether this PC's frames are still the sequence's.</summary>
|
||||
@@ -326,34 +238,6 @@ public sealed class RemoteSequenceSource : ISequenceSource
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Sets a priority other than the user's on a job the server just queued; the job stays queued if that fails.</summary>
|
||||
private async Task<JobDto> PrioritiseAsync(JobDto dto, JobPriority priority, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _api.SetJobPriorityAsync(dto.Id, JobWire.Priority(priority), cancellationToken);
|
||||
}
|
||||
catch (Exception ex) when (ex is ChronosApiException or HttpRequestException)
|
||||
{
|
||||
// Already claimed (409) or refused: the job runs at the user's priority, which is never later than asked.
|
||||
Log.Info("editor", $"Job {dto.Id} keeps the user's priority on the server: {ex.Message}");
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>After a lost event socket: the tracked jobs as the server has them now.</summary>
|
||||
private async Task RefreshTrackedAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
foreach (var job in _jobs.Values.Where(j => j.State is JobState.Queued or JobState.Running or JobState.Paused).ToList())
|
||||
await FetchAsync(job.Id, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private void Publish(Job job)
|
||||
{
|
||||
_jobs[job.Id] = job;
|
||||
JobChanged?.Invoke(job);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Takes the server's frames as <paramref name="manifest"/> lists them: their frames version when they are this PC's
|
||||
/// (saves name it), outdated otherwise. Returns whether they are.
|
||||
@@ -387,8 +271,6 @@ public sealed class RemoteSequenceSource : ISequenceSource
|
||||
ReadOnlyChanged?.Invoke();
|
||||
}
|
||||
|
||||
private bool Concerns(Job job) => job.TargetIds.Contains(Id) && job.Kind is JobKind.DeepAnalyse or JobKind.LightAnalyse or JobKind.Render;
|
||||
|
||||
/// <summary>The frame of the project's hero, or <see cref="Guid.Empty"/> (the first frame shows) without one.</summary>
|
||||
private Guid HeroFrameId(ProjectSettings project) =>
|
||||
project.HeroFrame is { } hero && (uint)hero < (uint)_remote.Frames.Count ? _remote.Frames[hero].Id : Guid.Empty;
|
||||
@@ -424,17 +306,7 @@ public sealed class RemoteSequenceSource : ISequenceSource
|
||||
}
|
||||
|
||||
/// <summary>A sequence deleted on the server reads as gone, like a local one removed from the index.</summary>
|
||||
private static async Task<T> Call<T>(Func<Task<T>> call)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await call();
|
||||
}
|
||||
catch (ChronosApiException ex) when (ex.Status == HttpStatusCode.NotFound)
|
||||
{
|
||||
throw new EditorException(Loc.L("Editor.Error.Missing"), ex);
|
||||
}
|
||||
}
|
||||
private static Task<T> Call<T>(Func<Task<T>> call) => ServerJobs.Call(call);
|
||||
|
||||
/// <summary>The server's settings as last read or written, with the version a save must name.</summary>
|
||||
private sealed record Stored(ProjectSettings Project, int Version);
|
||||
|
||||
Reference in new issue
Block a user