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
@@ -59,6 +59,17 @@ public sealed class SyncAgent : IAsyncDisposable
|
||||
|
||||
private readonly SemaphoreSlim _refreshing = new(1, 1);
|
||||
private Task _running = Task.CompletedTask;
|
||||
|
||||
/// <summary>
|
||||
/// Whether project changes may have been missed: <see cref="Missed"/> from the moment the event stream was lost,
|
||||
/// <see cref="Reopened"/> once it is open again (no change is missed from then on), back to <see cref="InStep"/> when a
|
||||
/// comparison that started after that reached the server and <see cref="ProjectChanged"/> said so.
|
||||
/// </summary>
|
||||
private int _projectsMissed;
|
||||
|
||||
private const int InStep = 0;
|
||||
private const int Missed = 1;
|
||||
private const int Reopened = 2;
|
||||
private SyncSnapshot? _snapshot;
|
||||
private Exception? _lastError;
|
||||
|
||||
@@ -84,6 +95,13 @@ public sealed class SyncAgent : IAsyncDisposable
|
||||
/// <summary><see cref="Snapshot"/> or <see cref="LastError"/> changed (any thread).</summary>
|
||||
public event Action? Changed;
|
||||
|
||||
/// <summary>
|
||||
/// A project of one of the server's sequences changed there (owner review 2, R8: <c>projectChanged</c>, any thread), or,
|
||||
/// with null, a comparison reached the server again after its event stream was lost, so changes may have been missed:
|
||||
/// workspace projects whose home is this server read where they stand again.
|
||||
/// </summary>
|
||||
public event Action<ProjectChangedEvent?>? ProjectChanged;
|
||||
|
||||
/// <summary>Starts the comparison and event loops.</summary>
|
||||
public void Start()
|
||||
{
|
||||
@@ -104,12 +122,16 @@ public sealed class SyncAgent : IAsyncDisposable
|
||||
await _refreshing.WaitAsync(linked.Token).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
// Only a comparison that started once the stream was open again covers every change missed meanwhile: one that
|
||||
// started before may end while the stream is still down, and a change said then would be missed for good.
|
||||
var reopened = Volatile.Read(ref _projectsMissed) == Reopened;
|
||||
var policy = _policy();
|
||||
var snapshot = await _sync.RefreshAsync(_remote, policy, linked.Token).ConfigureAwait(false);
|
||||
Volatile.Write(ref _snapshot, snapshot);
|
||||
Volatile.Write(ref _lastError, null);
|
||||
if (policy.KeepProxiesOffline && _engine is not null) await QueueProxiesAsync(snapshot, linked.Token).ConfigureAwait(false);
|
||||
Changed?.Invoke();
|
||||
if (reopened && Interlocked.CompareExchange(ref _projectsMissed, InStep, Reopened) == Reopened) TellProjects(null);
|
||||
return snapshot;
|
||||
}
|
||||
finally
|
||||
@@ -169,9 +191,14 @@ public sealed class SyncAgent : IAsyncDisposable
|
||||
{
|
||||
try
|
||||
{
|
||||
await foreach (var e in _remote.EventsAsync(cancellationToken).ConfigureAwait(false))
|
||||
await foreach (var e in _remote.EventsAsync(Reconnected, cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
failures = 0;
|
||||
if (e is ProjectChangedEvent project)
|
||||
{
|
||||
TellProjects(project);
|
||||
continue;
|
||||
}
|
||||
if (e is not SequenceChangedEvent changed) continue;
|
||||
_changed[changed.SequenceId] = 0;
|
||||
_eventWake.Writer.TryWrite(true);
|
||||
@@ -185,6 +212,9 @@ public sealed class SyncAgent : IAsyncDisposable
|
||||
{
|
||||
Log.Info(LogCategory, $"Event stream of {Server} lost: {ex.Message}");
|
||||
}
|
||||
// Project changes said meanwhile are missed: the first comparison that reaches the server once the stream is open
|
||||
// again says so (Reconnected).
|
||||
Volatile.Write(ref _projectsMissed, Missed);
|
||||
try
|
||||
{
|
||||
await Task.Delay(Backoff(failures++), cancellationToken).ConfigureAwait(false);
|
||||
@@ -197,6 +227,28 @@ public sealed class SyncAgent : IAsyncDisposable
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The event stream is open (again): after a loss, a comparison is asked for now, and the one that starts from here says
|
||||
/// that project changes may have been missed (<see cref="ProjectChanged"/> with null).
|
||||
/// </summary>
|
||||
private void Reconnected()
|
||||
{
|
||||
if (Interlocked.CompareExchange(ref _projectsMissed, Reopened, Missed) == Missed) RequestRefresh();
|
||||
}
|
||||
|
||||
/// <summary>Tells <see cref="ProjectChanged"/>; what a listener throws is logged, never ends the event loop.</summary>
|
||||
private void TellProjects(ProjectChangedEvent? change)
|
||||
{
|
||||
try
|
||||
{
|
||||
ProjectChanged?.Invoke(change);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OutOfMemoryException)
|
||||
{
|
||||
Log.Warning(LogCategory, $"A project change of {Server} was not handled", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Compares the sequences named by events, a short while after the first one arrived.</summary>
|
||||
private async Task ChangedLoopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
@@ -63,6 +63,28 @@ public interface ISyncRemote : IDisposable
|
||||
|
||||
/// <summary>Server events until cancelled or the socket closes.</summary>
|
||||
IAsyncEnumerable<ServerEvent> EventsAsync(CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="EventsAsync(CancellationToken)"/>, calling <paramref name="connected"/> once the stream is open, before its
|
||||
/// first event: from then on no event the server sends is missed. A remote that cannot say calls it when the stream is
|
||||
/// asked for.
|
||||
/// </summary>
|
||||
IAsyncEnumerable<ServerEvent> EventsAsync(Action connected, CancellationToken cancellationToken)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(connected);
|
||||
connected();
|
||||
return EventsAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Every frame of a sequence as of one frames version, with the photo aliases of its conversions (owner review 2, R8:
|
||||
/// what a workspace copy of a server project holds; servers with the <c>projects</c> capability). A remote without the
|
||||
/// call (the screenshots' sample server) throws <see cref="NotSupportedException"/>.
|
||||
/// </summary>
|
||||
Task<ProjectFramesDto> GetProjectFramesAsync(Guid sequenceId, CancellationToken cancellationToken) => throw new NotSupportedException();
|
||||
|
||||
/// <summary>The sequence's deep-analysis report (<c>analysis.json</c>); null when it was never deep-analysed, or the remote has no such call.</summary>
|
||||
Task<string?> GetAnalysisReportAsync(Guid sequenceId, CancellationToken cancellationToken) => Task.FromResult<string?>(null);
|
||||
}
|
||||
|
||||
/// <summary>Where sync jobs find their server connection.</summary>
|
||||
@@ -138,6 +160,12 @@ public sealed class ChronosSyncRemote(ChronosClient client, bool ownsClient) : I
|
||||
|
||||
public IAsyncEnumerable<ServerEvent> EventsAsync(CancellationToken cancellationToken) => client.EventsAsync(cancellationToken);
|
||||
|
||||
public IAsyncEnumerable<ServerEvent> EventsAsync(Action connected, CancellationToken cancellationToken) => client.EventsAsync(connected, cancellationToken);
|
||||
|
||||
public Task<ProjectFramesDto> GetProjectFramesAsync(Guid sequenceId, CancellationToken cancellationToken) => client.GetProjectFramesAsync(sequenceId, cancellationToken);
|
||||
|
||||
public Task<string?> GetAnalysisReportAsync(Guid sequenceId, CancellationToken cancellationToken) => client.GetAnalysisReportAsync(sequenceId, cancellationToken);
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (ownsClient) client.Dispose();
|
||||
|
||||
@@ -774,32 +774,54 @@ internal sealed class SyncTransfers(SyncService sync)
|
||||
var cached = new FileInfo(path) is { Exists: true, Length: > 0 };
|
||||
if (cached && (cachedKind == ProxyCache.Proxy || !offered.Contains(ProxyCache.Proxy))) return (name, cachedKind ?? ProxyCache.Thumb);
|
||||
var part = path + ".part";
|
||||
foreach (var kind in offered)
|
||||
// The cached file is as good as the server's thumbnail.
|
||||
var kind = await DownloadFirstAsync(remote, frameId, offered.TakeWhile(k => !(cached && k == ProxyCache.Thumb)), part, null, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
if (kind is not null)
|
||||
{
|
||||
File.Move(part, path, overwrite: true);
|
||||
return (name, kind);
|
||||
}
|
||||
return cached ? (name, ProxyCache.Thumb) : null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Downloads the first of <paramref name="kinds"/> a frame has on the server (a 404 tries the next) into a fresh
|
||||
/// <paramref name="part"/> file, written through <paramref name="through"/> when given (a hash of what arrives, which
|
||||
/// leaves the file open) and flushed to the disk when <paramref name="durable"/>; the kind it got, or null when the server
|
||||
/// has none of them, no part file left then. The proxy cache and the workspace copy of a server project (owner review 2,
|
||||
/// R8) download alike.
|
||||
/// </summary>
|
||||
internal static async Task<string?> DownloadFirstAsync(ISyncRemote remote, Guid frameId, IEnumerable<string> kinds, string part, Func<Stream, Stream>? through,
|
||||
CancellationToken cancellationToken, bool durable = false)
|
||||
{
|
||||
foreach (var kind in kinds)
|
||||
{
|
||||
// The cached file is as good as the server's thumbnail.
|
||||
if (cached && kind == ProxyCache.Thumb) break;
|
||||
try
|
||||
{
|
||||
await using (var file = new FileStream(part, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
|
||||
await remote.DownloadFrameAsync(frameId, kind, file, cancellationToken).ConfigureAwait(false);
|
||||
File.Move(part, path, overwrite: true);
|
||||
return (name, kind);
|
||||
{
|
||||
await remote.DownloadFrameAsync(frameId, kind, through?.Invoke(file) ?? file, cancellationToken).ConfigureAwait(false);
|
||||
if (durable) file.Flush(flushToDisk: true);
|
||||
}
|
||||
return kind;
|
||||
}
|
||||
catch (ChronosApiException ex) when (ex.Status == HttpStatusCode.NotFound)
|
||||
{
|
||||
File.Delete(part);
|
||||
}
|
||||
}
|
||||
return cached ? (name, ProxyCache.Thumb) : null;
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A frame missing or damaged on the server: its manifest never asks for its proxy and a pass makes none of a missing
|
||||
/// one, so the thumbnail (or the lack of a file) that stands in for it does not keep the cache stale.
|
||||
/// </summary>
|
||||
private static bool NeverProxied(FrameDto frame) => ((FrameFlags)frame.Flags & (FrameFlags.Missing | FrameFlags.Corrupt)) != 0;
|
||||
internal static bool NeverProxied(FrameDto frame) => ((FrameFlags)frame.Flags & (FrameFlags.Missing | FrameFlags.Corrupt)) != 0;
|
||||
|
||||
private static string[] Kinds(ManifestFrame frame) => (frame.HasProxy, frame.HasThumb) switch
|
||||
/// <summary>The kinds of a frame's assets the server offers, the proxy before the thumbnail that stands in while it has none.</summary>
|
||||
internal static string[] Kinds(ManifestFrame frame) => (frame.HasProxy, frame.HasThumb) switch
|
||||
{
|
||||
(true, true) => [ProxyCache.Proxy, ProxyCache.Thumb],
|
||||
(true, false) => [ProxyCache.Proxy],
|
||||
@@ -867,3 +889,68 @@ internal sealed class SyncTransfers(SyncService sync)
|
||||
public ValueTask ReportAsync() => context.ReportAsync(done, total);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A write-only stream that hashes (SHA-256) what passes through it into <paramref name="inner"/>, which it leaves open: the
|
||||
/// hash of a download as it arrived, to check the file it was written to against (owner review 2, R8). It cannot seek,
|
||||
/// so a download through it always starts from the beginning.
|
||||
/// </summary>
|
||||
internal sealed class HashingStream(Stream inner) : Stream
|
||||
{
|
||||
private readonly System.Security.Cryptography.IncrementalHash _hash = System.Security.Cryptography.IncrementalHash.CreateHash(
|
||||
System.Security.Cryptography.HashAlgorithmName.SHA256);
|
||||
|
||||
private long _written;
|
||||
|
||||
/// <summary>The bytes written through.</summary>
|
||||
public long Written => _written;
|
||||
|
||||
/// <summary>The lower-case hex SHA-256 of what was written through (once; it resets the hash).</summary>
|
||||
public string Sha256() => Convert.ToHexStringLower(_hash.GetHashAndReset());
|
||||
|
||||
public override bool CanRead => false;
|
||||
public override bool CanSeek => false;
|
||||
public override bool CanWrite => true;
|
||||
public override long Length => throw new NotSupportedException();
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get => throw new NotSupportedException();
|
||||
set => throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public override void Flush() => inner.Flush();
|
||||
|
||||
public override Task FlushAsync(CancellationToken cancellationToken) => inner.FlushAsync(cancellationToken);
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException();
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
|
||||
|
||||
public override void SetLength(long value) => throw new NotSupportedException();
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count) => Write(buffer.AsSpan(offset, count));
|
||||
|
||||
public override void Write(ReadOnlySpan<byte> buffer)
|
||||
{
|
||||
_hash.AppendData(buffer);
|
||||
_written += buffer.Length;
|
||||
inner.Write(buffer);
|
||||
}
|
||||
|
||||
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) =>
|
||||
WriteAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask();
|
||||
|
||||
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
|
||||
{
|
||||
_hash.AppendData(buffer.Span);
|
||||
_written += buffer.Length;
|
||||
return inner.WriteAsync(buffer, cancellationToken);
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing) _hash.Dispose();
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user