test(sync): upload, download, resume, proxies and live events against the real server
The first original of an upload now creates the server sequence through its session key (the archive ingest files API-created empty sequences nowhere), and settings sent after an upload are retried when the server's own analysis edits the new sequence meanwhile. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
9 files changed
+420
-49
No files matched your search
@@ -38,22 +38,41 @@ internal sealed class SyncTransfers(SyncService sync)
|
||||
var paths = new ArchivePaths(archive.RootPath);
|
||||
var (remoteId, created) = await TargetAsync(context, remote, local).ConfigureAwait(false);
|
||||
|
||||
var manifest = await remote.GetManifestAsync(remoteId, ct).ConfigureAwait(false);
|
||||
var present = manifest.Frames.Select(f => f.Sha256.ToLowerInvariant()).ToHashSet(StringComparer.Ordinal);
|
||||
var frames = (await SequenceFrames.ListAsync(db, local.Id, ct).ConfigureAwait(false)).Where(f => (f.Flags & FrameFlags.Missing) == 0).ToList();
|
||||
var pending = frames.Where(f => f.Sha256 is null || !present.Contains(f.Sha256.ToLowerInvariant())).ToList();
|
||||
var present = remoteId is { } known
|
||||
? (await remote.GetManifestAsync(known, ct).ConfigureAwait(false)).Frames.Select(f => f.Sha256.ToLowerInvariant()).ToHashSet(StringComparer.Ordinal)
|
||||
: [];
|
||||
var pending = new Queue<Frame>(frames.Where(f => f.Sha256 is null || !present.Contains(f.Sha256.ToLowerInvariant())));
|
||||
var progress = new TransferProgress(context, frames.Count, frames.Count - pending.Count);
|
||||
await progress.ReportAsync().ConfigureAwait(false);
|
||||
var skipped = 0;
|
||||
|
||||
// Without a server sequence the originals go out one by one under this sequence's session key until the
|
||||
// server has filed one, which creates the sequence (an original it already holds elsewhere creates nothing).
|
||||
while (remoteId is null && pending.TryDequeue(out var first))
|
||||
{
|
||||
var sent = await UploadFrameAsync(context, remote, null, SyncService.SessionKey(local.Id), paths.FromRelative(first.RelativePath), first, ct)
|
||||
.ConfigureAwait(false);
|
||||
if (sent is null) skipped++;
|
||||
else if (sent.SequenceId != Guid.Empty)
|
||||
{
|
||||
remoteId = sent.SequenceId;
|
||||
await context.CheckpointAsync(sent.SequenceId.ToString(), progress.Done + 1, frames.Count).ConfigureAwait(false);
|
||||
await sync.LinkAsync(remote.Server, sent.SequenceId, local, ct).ConfigureAwait(false);
|
||||
}
|
||||
await progress.StepAsync().ConfigureAwait(false);
|
||||
}
|
||||
if (remoteId is not { } target) throw new InvalidOperationException(Loc.L("Errors.Sync.NothingSent"));
|
||||
|
||||
await Parallel.ForEachAsync(pending, Workers(sync.Options.Uploads, ct), async (frame, token) =>
|
||||
{
|
||||
if (!await UploadFrameAsync(context, remote, remoteId, paths.FromRelative(frame.RelativePath), frame, token).ConfigureAwait(false))
|
||||
if (await UploadFrameAsync(context, remote, target, null, paths.FromRelative(frame.RelativePath), frame, token).ConfigureAwait(false) is null)
|
||||
Interlocked.Increment(ref skipped);
|
||||
await progress.StepAsync().ConfigureAwait(false);
|
||||
}).ConfigureAwait(false);
|
||||
|
||||
if (created) await sync.PushAllAsync(remote, remoteId, ct).ConfigureAwait(false);
|
||||
await sync.RefreshSequencesAsync(remote, [remoteId], CompareOnly, ct).ConfigureAwait(false);
|
||||
if (created) await sync.PushAllAsync(remote, target, ct).ConfigureAwait(false);
|
||||
await sync.RefreshSequencesAsync(remote, [target], CompareOnly, ct).ConfigureAwait(false);
|
||||
if (skipped > 0) throw new InvalidDataException(Loc.Plural("Errors.Sync.Unreadable", skipped));
|
||||
}
|
||||
|
||||
@@ -139,8 +158,11 @@ internal sealed class SyncTransfers(SyncService sync)
|
||||
if (cached < frames.Count) context.Log($"{frames.Count - cached} frame(s) have neither proxy nor thumbnail on the server");
|
||||
}
|
||||
|
||||
/// <summary>The server sequence an upload fills and whether this job created it: the linked one, one an interrupted attempt created, or a new one.</summary>
|
||||
private async Task<(Guid RemoteId, bool Created)> TargetAsync(JobContext context, ISyncRemote remote, Sequence local)
|
||||
/// <summary>
|
||||
/// The server sequence an upload fills and whether this job creates it: the linked one, the one an interrupted
|
||||
/// attempt created, or none yet (the first original creates it).
|
||||
/// </summary>
|
||||
private async Task<(Guid? RemoteId, bool Created)> TargetAsync(JobContext context, ISyncRemote remote, Sequence local)
|
||||
{
|
||||
var ct = context.Cancellation;
|
||||
Guid? started = Guid.TryParse(context.Checkpoint, out var saved) ? saved : null;
|
||||
@@ -153,13 +175,10 @@ internal sealed class SyncTransfers(SyncService sync)
|
||||
if (link?.RemoteId != id) await sync.LinkAsync(remote.Server, id, local, ct).ConfigureAwait(false);
|
||||
return (id, id == started);
|
||||
}
|
||||
// Deleted on the server meanwhile: upload into a new sequence.
|
||||
// Deleted on the server meanwhile: the originals go into a new sequence.
|
||||
if (id == link?.RemoteId) await sync.Links.DeleteAsync(remote.Server, id, ct).ConfigureAwait(false);
|
||||
}
|
||||
var dto = await remote.CreateSequenceAsync(new CreateSequenceRequest(local.Title, local.CameraModel), ct).ConfigureAwait(false);
|
||||
await context.CheckpointAsync(dto.Id.ToString(), 0, local.FrameCount).ConfigureAwait(false);
|
||||
await sync.LinkAsync(remote.Server, dto.Id, local, ct).ConfigureAwait(false);
|
||||
return (dto.Id, true);
|
||||
return (null, true);
|
||||
}
|
||||
|
||||
private static async Task<bool> ExistsAsync(ISyncRemote remote, Guid id, CancellationToken cancellationToken)
|
||||
@@ -175,22 +194,26 @@ internal sealed class SyncTransfers(SyncService sync)
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Hashes the original again and sends it; false (logged) when it is gone or no longer the archived file.</summary>
|
||||
private static async Task<bool> UploadFrameAsync(JobContext context, ISyncRemote remote, Guid remoteId, string path, Frame frame, CancellationToken cancellationToken)
|
||||
/// <summary>
|
||||
/// Hashes the original again and sends it into <paramref name="sequenceId"/> (or the sequence of
|
||||
/// <paramref name="sessionKey"/>); null (logged) when it is gone or no longer the archived file.
|
||||
/// </summary>
|
||||
private static async Task<CompleteUploadResponse?> UploadFrameAsync(JobContext context, ISyncRemote remote, Guid? sequenceId, string? sessionKey, string path,
|
||||
Frame frame, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
context.Log($"{frame.FileName}: missing, not uploaded");
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
var sha = await TitanoClient.HashFileAsync(path, cancellationToken).ConfigureAwait(false);
|
||||
if (frame.Sha256 is { } archived && !archived.Equals(sha, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
context.Log($"{frame.FileName}: changed since it was archived, not uploaded");
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
await remote.UploadFileAsync(path, remoteId, sha, frame.CapturedAt, cancellationToken).ConfigureAwait(false);
|
||||
return true;
|
||||
return await remote.UploadFileAsync(path, sequenceId, sessionKey, sha, frame.CapturedAt, cancellationToken).ConfigureAwait(false)
|
||||
?? new CompleteUploadResponse(Guid.Empty, sequenceId ?? Guid.Empty, -1);
|
||||
}
|
||||
|
||||
/// <summary>Downloads one original into staging and commits it into the local sequence (created from the server id when null).</summary>
|
||||
@@ -325,6 +348,8 @@ internal sealed class SyncTransfers(SyncService sync)
|
||||
}
|
||||
}
|
||||
|
||||
public long Done => done;
|
||||
|
||||
public ValueTask ReportAsync() => context.ReportAsync(done, total);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user