fix(server): scan the archive at start and every ten minutes

The server indexed only what arrived through its own uploads, so photos
already in an adopted folder and sequences another writer added stayed
unknown until an administrator asked for a rescan. ArchivePolling, registered
by the archive module, queues an incremental scan of the online archive at
start and every ten minutes at background priority, unless a scan or rebuild
of it is waiting, running or paused (ENG-13).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 09:58:45 +00:00
1 parent c87357b02c
commit 078cac75e4
3 files changed
+136 -4

No files matched your search

+4 -2
View File
@@ -7,8 +7,8 @@ namespace Titano.Server.Storage;
/// <summary>
/// The archive engine behind the server (BRIEF §7, §14.4): ingest of verified uploads, lookup of proxies and
/// thumbnails, and its job handlers (scan, rebuild, verify, import, thumbnails, proxies, maintenance). Decoding is
/// managed only (BRIEF §4.2).
/// thumbnails, its job handlers (scan, rebuild, verify, import, thumbnails, proxies, maintenance) and the periodic
/// incremental scan (<see cref="ArchivePolling"/>). Decoding is managed only (BRIEF §4.2).
/// </summary>
public static class ArchiveModule
{
@@ -22,5 +22,7 @@ public static class ArchiveModule
services.AddSingleton<IArchiveIngest>(sp => sp.GetRequiredService<ArchiveService>().Ingest);
services.AddSingleton<IJobHandlerProvider>(sp => sp.GetRequiredService<ArchiveService>());
services.AddSingleton<IFrameAssetLocator, ArchiveAssetLocator>();
services.AddSingleton<ArchivePolling>();
services.AddHostedService(sp => sp.GetRequiredService<ArchivePolling>());
}
}
@@ -0,0 +1,68 @@
using Titano.Core.Archiving;
using Titano.Core.Model;
using Titano.Data;
using Titano.Data.Archiving;
using Titano.Server.Jobs;
namespace Titano.Server.Storage;
/// <summary>
/// Polls the archive (BRIEF §7: by modification time and size, no file-system watchers): an incremental scan at start
/// and every <see cref="Interval"/> while the archive is online, so photos already in an adopted folder and sequences
/// another writer adds (a desktop sharing the archive, files copied in by hand) are indexed without an administrator's
/// rescan. A scan or rebuild of the archive that is waiting, running or paused is not queued again.
/// </summary>
public sealed class ArchivePolling(TitanoDatabase db, ServerArchive archive, JobEngineHost jobs, TimeProvider time, ILogger<ArchivePolling> logger)
: BackgroundService
{
/// <summary>The desktop's rhythm (it polls its archive the same way).</summary>
public static readonly TimeSpan Interval = TimeSpan.FromMinutes(10);
private static readonly JobState[] Pending = [JobState.Queued, JobState.Running, JobState.Paused];
/// <summary>Queues an incremental scan of an online archive unless one is pending; returns it, or null when none was queued.</summary>
public async Task<Job?> PollAsync(CancellationToken cancellationToken)
{
if (!jobs.CanRun(JobKind.Scan)) return null;
var current = archive.Current;
if (ArchiveService.GetAvailability(current) != ArchiveAvailability.Online) return null;
foreach (var kind in new[] { JobKind.Scan, JobKind.Rebuild })
{
foreach (var state in Pending)
{
if ((await db.Jobs.ListAsync(state, kind, 50, null, cancellationToken)).Items.Any(j => j.TargetIds.Contains(current.Id))) return null;
}
}
var scan = ArchiveJobs.Scan(current);
// Behind what users asked for (BRIEF §14.6).
return await jobs.Engine.EnqueueAsync(scan.Kind, scan.Targets, scan.Label, scan.ParametersJson, JobPriority.Background,
cancellationToken: cancellationToken);
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (true)
{
try
{
await PollAsync(stoppingToken);
}
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
{
return;
}
catch (Exception ex)
{
logger.LogWarning(ex, "The archive could not be polled");
}
try
{
await Task.Delay(Interval, time, stoppingToken);
}
catch (OperationCanceledException)
{
return;
}
}
}
}