fix(archive): the admin form reads archive.json; a poll never undoes an admin's edit
GET /api/v1/archive/settings refreshes the served archive from archive.json first, so the admin form (which saves every field) starts from a desktop's change made since the last poll instead of writing the old templates back. ServerArchive.RefreshAsync replaces the served archive only when the refresh registered a change, and only through a compare-and-swap against the archive it read, so a poll racing an admin's PUT no longer puts the pre-edit row back for ten minutes. RefreshSettingsAsync now ignores an archive.json a newer Titano wrote, as documented, instead of registering what this version reads of it. AppServicesTests now points the folder at an archive the index does not list first, and checks that the archive no longer chosen gets no scan. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
7 files changed
+98
-20
No files matched your search
@@ -19,7 +19,9 @@ public static class ArchiveEndpoints
|
||||
public static void Map(RouteGroupBuilder api)
|
||||
{
|
||||
var archive = api.MapGroup("/archive").Requires(Permissions.ManageArchive);
|
||||
archive.MapGet("/settings", (ServerArchive a) => Results.Json(Settings(a.Current), ProtocolJson.Default.ArchiveSettingsDto));
|
||||
// The form saves every field: it starts from what archive.json holds now, also a desktop's change since the last poll.
|
||||
archive.MapGet("/settings", async (HttpContext c, ServerArchive a, TitanoDatabase db, [FromServices] ArchiveService? archives) =>
|
||||
Results.Json(Settings(archives is null ? a.Current : await a.RefreshAsync(archives, db, c.RequestAborted)), ProtocolJson.Default.ArchiveSettingsDto));
|
||||
archive.MapPut("/settings", UpdateAsync);
|
||||
archive.MapGet("/stats", async (HttpContext c, TitanoDatabase db, ServerArchive a) =>
|
||||
Results.Json(await StatsAsync(db, a, c.RequestAborted), ProtocolJson.Default.ArchiveStatsDto));
|
||||
|
||||
@@ -28,8 +28,7 @@ public sealed class ArchivePolling(TitanoDatabase db, ServerArchive archive, Arc
|
||||
if (!jobs.CanRun(JobKind.Scan)) return null;
|
||||
var current = archive.Current;
|
||||
if (ArchiveService.GetAvailability(current) != ArchiveAvailability.Online) return null;
|
||||
if (await archives.RefreshSettingsAsync(current.Id, cancellationToken) is not null && await db.Archives.GetAsync(current.Id, cancellationToken) is { } row)
|
||||
archive.Current = current = row;
|
||||
current = await archive.RefreshAsync(archives, db, cancellationToken);
|
||||
foreach (var kind in new[] { JobKind.Scan, JobKind.Rebuild })
|
||||
{
|
||||
foreach (var state in Pending)
|
||||
|
||||
@@ -19,6 +19,29 @@ public sealed class ServerArchive(Archive archive)
|
||||
set => _current = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serves <paramref name="value"/> unless <see cref="Current"/> is no longer <paramref name="expected"/>, the archive
|
||||
/// it was read from: an edit an administrator saved meanwhile is never undone. False when it was not replaced.
|
||||
/// </summary>
|
||||
public bool TryReplace(Archive expected, Archive value) => ReferenceEquals(Interlocked.CompareExchange(ref _current, value, expected), expected);
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="Current"/> once it and the index row hold the settings another writer sharing the archive (a desktop)
|
||||
/// put into its archive.json (<see cref="ArchiveService.RefreshSettingsAsync"/>): uploads, the polling and the admin
|
||||
/// panel, whose form saves every field, never work from older ones. Unchanged while the file cannot be read.
|
||||
/// </summary>
|
||||
public async Task<Archive> RefreshAsync(ArchiveService archives, TitanoDatabase db, CancellationToken cancellationToken)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(archives);
|
||||
ArgumentNullException.ThrowIfNull(db);
|
||||
var current = Current;
|
||||
// Only a change the refresh registered is served, and never over an edit an administrator saved since the read above.
|
||||
if (await archives.RefreshSettingsAsync(current.Id, cancellationToken).ConfigureAwait(false) is { } settings && !settings.IsRegisteredIn(current)
|
||||
&& await db.Archives.GetAsync(current.Id, cancellationToken).ConfigureAwait(false) is { } row)
|
||||
TryReplace(current, row);
|
||||
return Current;
|
||||
}
|
||||
|
||||
public Guid Id => _current.Id;
|
||||
|
||||
public string Root => _current.RootPath;
|
||||
|
||||
Reference in new issue
Block a user