Files
Chronos/src/Chronos.Server/Storage/ProjectCatchUp.cs
T
Alby96andClaude Opus 5.5 81c303f484
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
feat: owner review 2 phase 5b: the server as a project home (#17)
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>
2026-11-04 14:58:11 +01:00

90 lines
4.0 KiB
C#

using System.Collections.Concurrent;
using System.Threading.Channels;
using Chronos.Core.Archiving;
using Chronos.Data;
using Chronos.Data.Archiving;
using Chronos.Data.Projects;
namespace Chronos.Server.Storage;
/// <summary>
/// Brings a sequence's projects in step soon after its settings were written outside the projects (owner review 2, R8): an
/// older client's or a desktop sync's <c>PATCH /sequences/{id}</c>, an analysis's Auto values, a conversion. Main takes them
/// as an external version at once (<see cref="ProjectService.CatchUpAsync"/>), and connected clients hear
/// <c>projectChanged</c> without waiting for the next project call, which would catch up all the same. Only a sequence that
/// has its Main project already: Main is made when the projects are first read, never for every sequence an analysis or a
/// sync touches. One sequence at a time in the background; a sequence asked for again while it waits is caught up once; one
/// another writer holds is tried again a few times, then left to that next call. Nothing happens without the archive engine
/// (a test host without it).
/// </summary>
public sealed class ProjectCatchUp(IServiceProvider services, ILogger<ProjectCatchUp> logger) : BackgroundService
{
/// <summary>The waits before each new try of a sequence another writer holds.</summary>
private static readonly TimeSpan[] Retries = [TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(30), TimeSpan.FromMinutes(2)];
private readonly Channel<(Guid Id, int Attempt)> _queue = Channel.CreateUnbounded<(Guid Id, int Attempt)>(new UnboundedChannelOptions { SingleReader = true });
private readonly ConcurrentDictionary<Guid, byte> _waiting = new();
private CancellationToken _stopping;
/// <summary>Catches the projects of <paramref name="sequenceId"/> up soon (at once when nothing waits).</summary>
public void Touch(Guid sequenceId) => Enqueue(sequenceId, 0);
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_stopping = stoppingToken;
try
{
await foreach (var (id, attempt) in _queue.Reader.ReadAllAsync(stoppingToken))
{
_waiting.TryRemove(id, out _);
await CatchUpAsync(id, attempt, stoppingToken);
}
}
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
{
// The server stops: what waits is caught up by the next project call.
}
}
private void Enqueue(Guid id, int attempt)
{
if (_waiting.TryAdd(id, 0)) _queue.Writer.TryWrite((id, attempt));
}
private async Task CatchUpAsync(Guid id, int attempt, CancellationToken cancellationToken)
{
if (services.GetService<ArchiveService>() is not { } archives) return;
try
{
if (await services.GetRequiredService<ChronosDatabase>().Projects.GetMainAsync(id, cancellationToken) is null) return;
await archives.Projects.CatchUpAsync(id, cancellationToken);
}
catch (LeaseUnavailableException) when (attempt < Retries.Length)
{
_ = RetryLaterAsync(id, attempt);
}
catch (Exception ex) when (ex is ProjectException or ArchiveOfflineException or SequenceFolderMovedException or LeaseUnavailableException)
{
// The sequence is gone, its archive offline or its folder elsewhere, or it stayed busy: the next project call catches up.
logger.LogDebug("The projects of sequence {Sequence} are caught up later: {Reason}", id, ex.Message);
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
logger.LogWarning(ex, "The projects of sequence {Sequence} could not be caught up", id);
}
}
private async Task RetryLaterAsync(Guid id, int attempt)
{
try
{
await Task.Delay(Retries[attempt], _stopping);
Enqueue(id, attempt + 1);
}
catch (OperationCanceledException)
{
// Stopping.
}
}
}