feat(server): publish card imports as cardEvent
BRIEF §14.3 lists cardEvent among the WebSocket events, but the server had none. A client importing a card into the server now reports its start, progress (at most once a second, a report still waiting replaced by the next) and end to the new POST /api/v1/uploads/cards (Upload permission; import id, card name, state, done, total, failed), and the server publishes a CardEvent naming the account and client to every connected client; the protocol client parses it and offers ReportCardAsync. A report that fails is logged and never stops the import; a server without the endpoint gets no more reports (SRS-10). 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
+376
-6
No files matched your search
@@ -26,6 +26,27 @@ public sealed class ServerImportSink(TitanoClient client, string stagingDirector
|
||||
/// <summary>Every file committed or refused so far, by card path; pass it to <see cref="LocalImport.RunAsync"/>.</summary>
|
||||
public ImportSinkReport Report { get; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Runs a confirmed plan into the server through this sink (<see cref="LocalImport.RunAsync"/>) and tells the server's
|
||||
/// other clients about it as <paramref name="card"/> (BRIEF §14.3 <c>cardEvent</c>): started, its progress, and
|
||||
/// finished, also when it stopped early.
|
||||
/// </summary>
|
||||
public async Task<ImportOutcome> RunAsync(ImportPlan plan, string card, bool verify, IProgress<ImportProgress>? progress, CancellationToken cancellationToken)
|
||||
{
|
||||
var events = new CardImportReporter(client, plan.Id, card, plan.FileCount);
|
||||
events.Started();
|
||||
ImportOutcome? outcome = null;
|
||||
try
|
||||
{
|
||||
outcome = await LocalImport.RunAsync(plan, this, verify, events.Forward(progress), cancellationToken, Report).ConfigureAwait(false);
|
||||
return outcome;
|
||||
}
|
||||
finally
|
||||
{
|
||||
await events.FinishedAsync(outcome).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<SinkCommit>> CommitAsync(PlannedSession session, IReadOnlyList<StagedFile> files, CancellationToken cancellationToken)
|
||||
{
|
||||
var results = new List<SinkCommit>(files.Count);
|
||||
@@ -81,3 +102,123 @@ public sealed class ServerImportSink(TitanoClient client, string stagingDirector
|
||||
private static bool IsRefusal(TitanoApiException ex) =>
|
||||
!ex.IsTransient && ex.Status is not (HttpStatusCode.Unauthorized or HttpStatusCode.Forbidden) && (int)ex.Status is >= 400 and < 500;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The reports of one card import into the server (BRIEF §14.3 <c>cardEvent</c>, §14.4), sent one at a time and in order:
|
||||
/// progress at most once a second, and a progress report still waiting is replaced by the next report, so a slow or
|
||||
/// unreachable server never piles them up. A report that fails is logged and the import goes on; a server without the
|
||||
/// endpoint (404) gets no more reports.
|
||||
/// </summary>
|
||||
/// <param name="total">Photos of the plan, skipped ones included (like <see cref="ImportProgress.ItemsTotal"/>).</param>
|
||||
public sealed class CardImportReporter(TitanoClient client, Guid importId, string card, int total, TimeProvider? time = null)
|
||||
{
|
||||
private static readonly TimeSpan ProgressInterval = TimeSpan.FromSeconds(1);
|
||||
private static readonly TimeSpan SendTimeout = TimeSpan.FromSeconds(10);
|
||||
|
||||
private readonly TimeProvider _time = time ?? TimeProvider.System;
|
||||
private readonly Lock _gate = new();
|
||||
private readonly List<CardImportReport> _queue = [];
|
||||
private Task _sending = Task.CompletedTask;
|
||||
private bool _draining;
|
||||
private bool _finished;
|
||||
private bool _unsupported;
|
||||
private long? _lastProgress;
|
||||
private ImportProgress? _latest;
|
||||
|
||||
public void Started() => Post(CardImportStates.Started, 0, 0);
|
||||
|
||||
/// <summary>Progress that reaches <paramref name="inner"/> (the screen's own) and these reports.</summary>
|
||||
public IProgress<ImportProgress> Forward(IProgress<ImportProgress>? inner) => new Forwarding(this, inner);
|
||||
|
||||
public void Report(ImportProgress progress)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
_latest = progress;
|
||||
var now = _time.GetTimestamp();
|
||||
if (_lastProgress is { } last && _time.GetElapsedTime(last, now) < ProgressInterval) return;
|
||||
_lastProgress = now;
|
||||
}
|
||||
Post(CardImportStates.Progress, progress.ItemsDone, progress.Failed);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reports the end: every photo handled when the import gave an <paramref name="outcome"/>, else as far as its progress
|
||||
/// got. Completes when the reports still queued are sent (each gives up after 10 s).
|
||||
/// </summary>
|
||||
public Task FinishedAsync(ImportOutcome? outcome)
|
||||
{
|
||||
int done, failed;
|
||||
lock (_gate)
|
||||
{
|
||||
done = outcome is null ? _latest?.ItemsDone ?? 0 : total;
|
||||
failed = outcome?.Failures.Count ?? _latest?.Failed ?? 0;
|
||||
}
|
||||
Post(CardImportStates.Finished, done, failed);
|
||||
lock (_gate) return _sending;
|
||||
}
|
||||
|
||||
private void Post(string state, int done, int failed)
|
||||
{
|
||||
var report = new CardImportReport(importId, card, state, Math.Clamp(done, 0, total), total, Math.Clamp(failed, 0, total));
|
||||
lock (_gate)
|
||||
{
|
||||
if (_finished || _unsupported) return;
|
||||
_finished = state == CardImportStates.Finished;
|
||||
if (_queue.Count > 0 && _queue[^1].State == CardImportStates.Progress) _queue[^1] = report;
|
||||
else _queue.Add(report);
|
||||
if (_draining) return;
|
||||
_draining = true;
|
||||
_sending = Task.Run(DrainAsync);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task DrainAsync()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
CardImportReport report;
|
||||
lock (_gate)
|
||||
{
|
||||
if (_queue.Count == 0 || _unsupported)
|
||||
{
|
||||
_queue.Clear();
|
||||
_draining = false;
|
||||
return;
|
||||
}
|
||||
report = _queue[0];
|
||||
_queue.RemoveAt(0);
|
||||
}
|
||||
await SendAsync(report).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SendAsync(CardImportReport report)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var timeout = new CancellationTokenSource(SendTimeout);
|
||||
await client.ReportCardAsync(report, timeout.Token).ConfigureAwait(false);
|
||||
}
|
||||
catch (TitanoApiException ex) when (ex.Status is HttpStatusCode.NotFound or HttpStatusCode.MethodNotAllowed)
|
||||
{
|
||||
// A server from before card events.
|
||||
lock (_gate) _unsupported = true;
|
||||
Log.Info("import", $"The server takes no card reports ({ex.Error.Code})");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// The reports only inform the other clients: nothing about them may stop or fail the import.
|
||||
Log.Warning("import", $"The {report.State} report of the import from {card} did not reach the server", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class Forwarding(CardImportReporter reporter, IProgress<ImportProgress>? inner) : IProgress<ImportProgress>
|
||||
{
|
||||
public void Report(ImportProgress value)
|
||||
{
|
||||
inner?.Report(value);
|
||||
reporter.Report(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1165,7 +1165,7 @@ public sealed class ImportViewModel : SectionViewModel
|
||||
if (choices.Destination == ImportDestinationKind.Folder)
|
||||
outcome = await Task.Run(() => LocalImport.RunAsync(plan, new FolderImportSink(choices.FolderPath!), choices.VerifyCopies, progress, cts.Token), cts.Token);
|
||||
else if (Services.Server.Flow is { Address: { } address, Token: { } token })
|
||||
outcome = await RunServerAsync(plan, choices, address, token, progress, cts.Token);
|
||||
outcome = await RunServerAsync(plan, choices, source.Title, address, token, progress, cts.Token);
|
||||
else
|
||||
{
|
||||
Fail(Loc.L("Import.Blocked.NoServer"));
|
||||
@@ -1185,15 +1185,15 @@ public sealed class ImportViewModel : SectionViewModel
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<ImportOutcome> RunServerAsync(ImportPlan plan, ImportChoices choices, Uri address, string token, IProgress<ImportProgress> progress,
|
||||
CancellationToken ct)
|
||||
private async Task<ImportOutcome> RunServerAsync(ImportPlan plan, ImportChoices choices, string card, Uri address, string token,
|
||||
IProgress<ImportProgress> progress, CancellationToken ct)
|
||||
{
|
||||
var staging = Path.Combine(ServerStagingRoot, plan.Id.ToString("N"));
|
||||
using var client = new TitanoClient(address, new CertificatePinning(Services.Server.Fingerprints), $"Titano ({Environment.MachineName})") { Token = token };
|
||||
try
|
||||
{
|
||||
var sink = new ServerImportSink(client, staging);
|
||||
return await Task.Run(() => LocalImport.RunAsync(plan, sink, choices.VerifyCopies, progress, ct, sink.Report), ct);
|
||||
return await Task.Run(() => sink.RunAsync(plan, card, choices.VerifyCopies, progress, ct), ct);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
@@ -173,6 +173,13 @@ public sealed class TitanoClient : IDisposable
|
||||
return await RetryAsync(() => SendAsync<CompleteUploadResponse>(HttpMethod.Post, $"api/v1/uploads/{uploadId}/complete", ProtocolJson.Default.CompleteUploadResponse, ct), ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reports a card import this client streams into the server (started, progress, finished); the server tells every
|
||||
/// connected client with a <see cref="CardEvent"/> (BRIEF §14.3).
|
||||
/// </summary>
|
||||
public Task ReportCardAsync(CardImportReport report, CancellationToken ct = default) =>
|
||||
SendAsync(HttpMethod.Post, "api/v1/uploads/cards", report, ProtocolJson.Default.CardImportReport, ct);
|
||||
|
||||
/// <summary>
|
||||
/// Render node: uploads the output of a claimed job in resumable chunks, then completes the job; the server verifies
|
||||
/// the SHA-256 in <paramref name="result"/> and files the render under the sequence's exports (BRIEF §14.7).
|
||||
|
||||
@@ -10,6 +10,7 @@ namespace Titano.Protocol;
|
||||
[JsonDerivedType(typeof(JobProgressEvent), "jobProgress")]
|
||||
[JsonDerivedType(typeof(JobStateEvent), "jobState")]
|
||||
[JsonDerivedType(typeof(SequenceChangedEvent), "sequenceChanged")]
|
||||
[JsonDerivedType(typeof(CardEvent), "cardEvent")]
|
||||
[JsonDerivedType(typeof(NodeStateEvent), "nodeState")]
|
||||
public abstract record ServerEvent;
|
||||
|
||||
@@ -19,4 +20,24 @@ public sealed record JobStateEvent(Guid JobId, string State, string? Error) : Se
|
||||
|
||||
public sealed record SequenceChangedEvent(Guid SequenceId, string Change, int FramesVersion, int SettingsVersion) : ServerEvent;
|
||||
|
||||
/// <summary>
|
||||
/// A card import that a connected client streams into the server (BRIEF §14.4), as that client reported it
|
||||
/// (<see cref="CardImportReport"/>): <see cref="State"/> is one of <see cref="CardImportStates"/>, <see cref="Done"/>
|
||||
/// counts the photos of its plan handled so far (failures included), <see cref="User"/> and <see cref="ClientName"/>
|
||||
/// say who imports it.
|
||||
/// </summary>
|
||||
public sealed record CardEvent(Guid ImportId, string Card, string State, int Done, int Total, int Failed, string User, string? ClientName) : ServerEvent;
|
||||
|
||||
/// <summary>States of a card import report and of its <see cref="CardEvent"/>.</summary>
|
||||
public static class CardImportStates
|
||||
{
|
||||
public const string Started = "started";
|
||||
public const string Progress = "progress";
|
||||
|
||||
/// <summary>The import ended: every photo handled, or fewer when it stopped early.</summary>
|
||||
public const string Finished = "finished";
|
||||
|
||||
public static bool IsKnown(string? state) => state is Started or Progress or Finished;
|
||||
}
|
||||
|
||||
public sealed record NodeStateEvent(Guid NodeId, string ClientName, string State) : ServerEvent;
|
||||
@@ -35,6 +35,7 @@ namespace Titano.Protocol;
|
||||
[JsonSerializable(typeof(ChunkReceived))]
|
||||
[JsonSerializable(typeof(CompleteUploadResponse))]
|
||||
[JsonSerializable(typeof(UploadState))]
|
||||
[JsonSerializable(typeof(CardImportReport))]
|
||||
[JsonSerializable(typeof(JobDto))]
|
||||
[JsonSerializable(typeof(Page<JobDto>))]
|
||||
[JsonSerializable(typeof(CreateJobRequest))]
|
||||
|
||||
@@ -17,6 +17,13 @@ public sealed record CompleteUploadResponse(Guid FrameId, Guid SequenceId, int I
|
||||
/// <summary><c>GET /api/v1/uploads/{id}</c>: resume state.</summary>
|
||||
public sealed record UploadState(Guid UploadId, string FileName, long Size, string Sha256, int ChunkSize, IReadOnlyList<int> Received, string State);
|
||||
|
||||
/// <summary>
|
||||
/// <c>POST /api/v1/uploads/cards</c>: a client importing a card into the server through these uploads reports its start,
|
||||
/// progress and end (BRIEF §14.4); the server tells every connected client with a <see cref="CardEvent"/> (BRIEF §14.3).
|
||||
/// <see cref="ImportId"/> is the client's import plan, <see cref="Card"/> the card's name as the client shows it.
|
||||
/// </summary>
|
||||
public sealed record CardImportReport(Guid ImportId, string Card, string State, int Done, int Total, int Failed);
|
||||
|
||||
/// <summary>
|
||||
/// <c>POST /api/v1/sequences/{id}/renders</c>: a client offers one of its renders of the sequence (BRIEF §14.5,
|
||||
/// renders are uploaded on request). The render keeps its id on the server, so both sides recognise it; when the
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Titano.Core.Model;
|
||||
using Titano.Protocol;
|
||||
using Titano.Server.Events;
|
||||
using Titano.Server.Security;
|
||||
using Titano.Server.Storage;
|
||||
|
||||
@@ -7,10 +8,13 @@ namespace Titano.Server.Api;
|
||||
|
||||
/// <summary>
|
||||
/// <c>/api/v1/uploads</c> (BRIEF §14.4). Chunk, state and cancel calls also serve render nodes uploading their output,
|
||||
/// which start and complete through the node endpoints.
|
||||
/// which start and complete through the node endpoints. <c>POST /uploads/cards</c> takes the reports of a card import a
|
||||
/// client streams through these uploads and tells every connected client (BRIEF §14.3 <c>cardEvent</c>).
|
||||
/// </summary>
|
||||
public static class UploadEndpoints
|
||||
{
|
||||
private const int MaxCardName = 200;
|
||||
|
||||
public static void Map(RouteGroupBuilder api)
|
||||
{
|
||||
var uploads = api.MapGroup("/uploads").Requires(Permissions.Upload | Permissions.RenderNode);
|
||||
@@ -23,6 +27,7 @@ public static class UploadEndpoints
|
||||
uploads.MapPost("/{id:guid}/complete", async (HttpContext context, Caller caller, Guid id, UploadService service) =>
|
||||
(await service.CompleteAsync(caller, id)).ToResult(context, r => Results.Json(r, ProtocolJson.Default.CompleteUploadResponse)))
|
||||
.Requires(Permissions.Upload);
|
||||
uploads.MapPost("/cards", ReportCard).Requires(Permissions.Upload);
|
||||
}
|
||||
|
||||
private static async Task<IResult> StartAsync(HttpContext context, Caller caller, StartUploadRequest request, UploadService service)
|
||||
@@ -32,6 +37,19 @@ public static class UploadEndpoints
|
||||
statusCode: r.AlreadyExists ? StatusCodes.Status200OK : StatusCodes.Status201Created));
|
||||
}
|
||||
|
||||
/// <summary>A card import's start, progress or end, published as a <see cref="CardEvent"/> naming who imports it; 204.</summary>
|
||||
private static IResult ReportCard(HttpContext context, Caller caller, CardImportReport report, EventHub events)
|
||||
{
|
||||
if (report.ImportId == Guid.Empty) return ApiErrors.BadRequest(context, "bad_request", "importId");
|
||||
var card = report.Card?.Trim();
|
||||
if (string.IsNullOrEmpty(card) || card.Length > MaxCardName || card.Any(char.IsControl)) return ApiErrors.BadRequest(context, "bad_request", "card");
|
||||
if (!CardImportStates.IsKnown(report.State)) return ApiErrors.BadRequest(context, "bad_request", "state");
|
||||
if (report.Total < 0 || report.Done < 0 || report.Done > report.Total || report.Failed < 0 || report.Failed > report.Total)
|
||||
return ApiErrors.BadRequest(context, "bad_request", "done");
|
||||
events.Publish(new CardEvent(report.ImportId, card, report.State, report.Done, report.Total, report.Failed, caller.User.Username, caller.ClientName));
|
||||
return Results.NoContent();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Chunks are the only large request bodies; every other endpoint keeps Kestrel's small default limit. The service
|
||||
/// raises it to the chunk size of this upload (fixed when it started) before it reads the body.
|
||||
|
||||
Reference in new issue
Block a user