From b344d2a527dc32016c69a339b4a7c367b896e353 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 16:02:15 +0000 Subject: [PATCH] fix(archive): polling follows the archive folder; the index follows an archive.json changed elsewhere MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - The desktop's rescans run for the whole session and each one targets the archive at Settings › Archive's folder (as the Import and Archive screens do), so an archive created on the first run, chosen in Settings, relinked or imported with settings is rescanned from the next tick on, and Rescan every always reaches a live timer. The folder of the start is reopened (relink, interrupted renames) as soon as its archive.json is there, so an unplugged drive or a folder without an archive no longer stops polling for the session. - ArchiveService.RefreshSettingsAsync reads archive.json and registers the archive again when its name, templates or proxy size changed elsewhere (a server's admin panel, another PC); null when the file cannot be read, is newer or belongs to another archive. Desktop rescans and the server's polling call it first, so imports, uploads and the admin panel follow the shared settings without a restart. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- src/Titano.App/Infrastructure/AppServices.cs | 95 +++++++++++++++---- src/Titano.Data/Archiving/ArchiveService.cs | 50 ++++++++-- src/Titano.Server/Storage/ArchivePolling.cs | 8 +- .../Infrastructure/AppServicesTests.cs | 47 +++++++++ .../Archiving/ArchiveSettingsTests.cs | 33 +++++++ .../Storage/ArchiveModuleTests.cs | 22 +++++ 6 files changed, 224 insertions(+), 31 deletions(-) diff --git a/src/Titano.App/Infrastructure/AppServices.cs b/src/Titano.App/Infrastructure/AppServices.cs index 699422bf..d4f3c278 100644 --- a/src/Titano.App/Infrastructure/AppServices.cs +++ b/src/Titano.App/Infrastructure/AppServices.cs @@ -3,6 +3,7 @@ using System.IO; using System.Net.Http; using System.Reflection; using Titano.App.About; +using Titano.App.Archiving; using Titano.App.Import; using Titano.App.Jobs; using Titano.App.Library; @@ -32,13 +33,13 @@ namespace Titano.App.Infrastructure; /// managed encoders), the one of this PC with the handlers of , /// thumbnails, the server session, the render node, the encoder and format inventories, the update check and the sync /// with the signed-in server (). Started after the window shows, stopped when the app exits. The -/// last archive is reopened at start and rescanned every (Settings › Archive; BRIEF §7: -/// polling, no file-system watchers). Jobs follow Settings › Rendering and Analysis from the next one on (memory -/// budget, quality profile, analysis parameters through ; parallelism as the engine's -/// workers). The render node runs while the user offers this PC () and a -/// server session is open (never in capture mode). Renders and the render node use hardware encoders as Settings › -/// Rendering allows (). Disposal never needs the UI thread, -/// which waits for it. +/// archive at Settings › Archive's folder is reopened at start and rescanned every , as is one +/// chosen later (BRIEF §7: polling, no file-system watchers). Jobs follow Settings › Rendering and Analysis from the +/// next one on (memory budget, quality profile, analysis parameters through ; parallelism +/// as the engine's workers). The render node runs while the user offers this PC () +/// and a server session is open (never in capture mode). Renders and the render node use hardware encoders as +/// Settings › Rendering allows (). Disposal never needs the UI +/// thread, which waits for it. /// public sealed class AppServices : IAsyncDisposable { @@ -228,7 +229,7 @@ public sealed class AppServices : IAsyncDisposable Devices.Start(); var database = Database; _backups = Task.Run(() => DatabaseBackups.RunDailyAsync(database, Environment.BackupDirectory, null, _stopping.Token)); - if (Settings.LastArchivePath is { Length: > 0 } root) _polling = PollAsync(root, _stopping.Token); + _polling = PollAsync(_stopping.Token); } /// Uses an already open index (capture, tests) instead of opening one; sample data keeps the engine stopped. @@ -258,35 +259,89 @@ public sealed class AppServices : IAsyncDisposable if (Database is not null) await Database.DisposeAsync().ConfigureAwait(false); } - /// Reopens the last archive (relinking a moved one, finishing interrupted renames) and rescans it periodically, - /// off the UI thread (disposal waits for it there). - private async Task PollAsync(string root, CancellationToken ct) + /// The period of the rescans while they run (Settings › Archive › Rescan every); null before the index opens. + internal TimeSpan? PollingPeriod => _pollTimer?.Period; + + /// + /// One rescan of the polling, off the UI thread: the archive the Import and Archive screens work on + /// (, the one at Settings › Archive's folder) first takes the settings another process + /// sharing it wrote into its archive.json (also on a read-only share), then gets an incremental scan while it is + /// online. Returns that archive; null when the index holds none. + /// + internal Task RescanAsync(CancellationToken ct = default) => Task.Run(async () => { + if (Database is not { } db || Archives is not { } archives) return null; + var archive = CurrentArchive.Pick(await db.Archives.ListAsync(ct).ConfigureAwait(false), Settings.LastArchivePath); + if (archive is null) return null; + var availability = ArchiveService.GetAvailability(archive); + if (availability != ArchiveAvailability.Offline) await archives.RefreshSettingsAsync(archive.Id, ct).ConfigureAwait(false); + if (availability == ArchiveAvailability.Online) await QueueScanAsync(archive, full: false, ct).ConfigureAwait(false); + return archive; + }, ct); + + /// + /// Rescans the archive now and every () until the app exits, which + /// waits for it on the UI thread. Each rescan follows the archive folder: one chosen on the first run, in Settings or + /// by a relink is rescanned from the next tick on, the one before no longer. The archive at the folder of the start is + /// first reopened (a moved one relinked, interrupted renames finished), at each tick until its archive.json is there + /// (an unplugged drive), unless the folder changed meanwhile: whatever changed it opened the new archive. + /// + private async Task PollAsync(CancellationToken ct) + { + var reopen = Settings.LastArchivePath is { Length: > 0 } start ? start : null; + // Settings › Archive changes the period of this timer (ApplyJobSettings). + using var timer = _pollTimer = new PeriodicTimer(ScanInterval); try { - if (Archives is null) return; - var archive = await Task.Run(() => Archives.OpenAsync(root, ct), ct).ConfigureAwait(false); - // Settings › Archive changes the period of this timer (ApplyJobSettings). - using var timer = _pollTimer = new PeriodicTimer(ScanInterval); do { - if (ArchiveService.GetAvailability(archive) == ArchiveAvailability.Online) await QueueScanAsync(archive, full: false, ct).ConfigureAwait(false); + if (reopen is not null + && (!string.Equals(reopen, Settings.LastArchivePath, StringComparison.OrdinalIgnoreCase) || await ReopenAsync(reopen, ct).ConfigureAwait(false))) + reopen = null; + try + { + await RescanAsync(ct).ConfigureAwait(false); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException or DatabaseException or ArgumentException) + { + // The next tick tries again (a share that dropped, an archive removed from the index meanwhile). + Log.Warning("archive", "The archive could not be rescanned", ex); + } } while (await timer.WaitForNextTickAsync(ct).ConfigureAwait(false)); } catch (OperationCanceledException) when (ct.IsCancellationRequested) { } - catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException or DatabaseException) - { - Log.Warning("archive", $"The archive at {root} could not be opened", ex); - } finally { _pollTimer = null; } } + /// + /// Registers the archive at again as at open; false while its archive.json is not there. A + /// damaged or newer one is not tried again: the archive the index holds at the folder is still rescanned. + /// + private async Task ReopenAsync(string root, CancellationToken ct) + { + if (Archives is not { } archives) return true; + try + { + return await Task.Run(async () => + { + if (!File.Exists(new ArchivePaths(root).ManifestPath)) return false; + await archives.OpenAsync(root, ct).ConfigureAwait(false); + return true; + }, ct).ConfigureAwait(false); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException or DatabaseException or NewerFormatException) + { + Log.Warning("archive", $"The archive at {root} could not be opened", ex); + return true; + } + } + /// /// A restore staged in Settings › Advanced replaces the index file before the index is opened, so it also recovers an /// index that no longer opens (). diff --git a/src/Titano.Data/Archiving/ArchiveService.cs b/src/Titano.Data/Archiving/ArchiveService.cs index 562d9e42..ddd1fce3 100644 --- a/src/Titano.Data/Archiving/ArchiveService.cs +++ b/src/Titano.Data/Archiving/ArchiveService.cs @@ -147,17 +147,31 @@ public sealed class ArchiveService : IJobHandlerProvider public async Task GetSettingsAsync(Guid archiveId, CancellationToken cancellationToken = default) { var archive = await LoadArchiveAsync(archiveId, cancellationToken).ConfigureAwait(false); - var paths = new ArchivePaths(archive.RootPath); - try + return TryReadManifest(archive) is { } manifest + ? ArchiveSettings.Of(manifest) + : new ArchiveSettings(archive.Name, new LayoutTemplates { Folder = archive.FolderTemplate, File = archive.FileTemplate }, + new ProxyPolicy { LongEdge = archive.ProxyLongEdge }, new ThumbnailPolicy()); + } + + /// + /// The settings in the archive's archive.json, with its index row brought up to them when another process + /// sharing the archive changed the file (a server's admin panel, another PC's Settings): imports and uploads take their + /// templates and proxy size from that row. Null while the file cannot be read (offline, damaged, newer, of another + /// archive): the row stays as it is, and only 's stand-ins are known. + /// + public async Task RefreshSettingsAsync(Guid archiveId, CancellationToken cancellationToken = default) + { + var archive = await LoadArchiveAsync(archiveId, cancellationToken).ConfigureAwait(false); + if (TryReadManifest(archive) is not { } manifest) return null; + if (manifest.Name != archive.Name || manifest.LayoutTemplates.Folder != archive.FolderTemplate || manifest.LayoutTemplates.File != archive.FileTemplate + || manifest.ProxyPolicy.LongEdge != archive.ProxyLongEdge) { - if (File.Exists(paths.ManifestPath)) return ArchiveSettings.Of(ManifestStore.ReadArchive(paths.ManifestPath)); + // Read without the settings lease, as at open: an edit made meanwhile writes its own row, and the next refresh + // settles a file that changes again. + await RegisterAsync(new ArchivePaths(archive.RootPath), manifest, cancellationToken).ConfigureAwait(false); + Log.Info("archive", $"Settings of {archive.RootPath} changed elsewhere: {manifest.LayoutTemplates.Folder} · {manifest.LayoutTemplates.File}, proxies {manifest.ProxyPolicy.LongEdge}"); } - catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException) - { - Log.Warning("archive", $"The settings of {paths.Root} cannot be read; the index's are used", ex); - } - return new ArchiveSettings(archive.Name, new LayoutTemplates { Folder = archive.FolderTemplate, File = archive.FileTemplate }, - new ProxyPolicy { LongEdge = archive.ProxyLongEdge }, new ThumbnailPolicy()); + return ArchiveSettings.Of(manifest); } /// @@ -325,6 +339,24 @@ public sealed class ArchiveService : IJobHandlerProvider return archive; } + /// The archive's archive.json; null (logged) while it cannot be read or when it belongs to another archive. + private static ArchiveManifest? TryReadManifest(Archive archive) + { + var paths = new ArchivePaths(archive.RootPath); + try + { + if (!File.Exists(paths.ManifestPath)) return null; + var manifest = ManifestStore.ReadArchive(paths.ManifestPath); + if (manifest.Id == archive.Id) return manifest; + Log.Warning("archive", $"{paths.ManifestPath} belongs to archive {manifest.Id}, not {archive.Id}; the index's settings are used"); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException or NewerFormatException) + { + Log.Warning("archive", $"The settings of {paths.Root} cannot be read; the index's are used", ex); + } + return null; + } + private async Task LoadArchiveAsync(Guid archiveId, CancellationToken cancellationToken) => await _db.Archives.GetAsync(archiveId, cancellationToken).ConfigureAwait(false) ?? throw new ArgumentException($"Unknown archive {archiveId}.", nameof(archiveId)); diff --git a/src/Titano.Server/Storage/ArchivePolling.cs b/src/Titano.Server/Storage/ArchivePolling.cs index fe5bb2e4..739d6451 100644 --- a/src/Titano.Server/Storage/ArchivePolling.cs +++ b/src/Titano.Server/Storage/ArchivePolling.cs @@ -10,9 +10,11 @@ namespace Titano.Server.Storage; /// Polls the archive (BRIEF §7: by modification time and size, no file-system watchers): an incremental scan at start /// and every 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. +/// rescan. Settings such a writer changed in archive.json reach the index first, so uploads and the admin panel follow +/// them. A scan or rebuild of the archive that is waiting, running or paused is not queued again. /// -public sealed class ArchivePolling(TitanoDatabase db, ServerArchive archive, JobEngineHost jobs, TimeProvider time, ILogger logger) +public sealed class ArchivePolling(TitanoDatabase db, ServerArchive archive, ArchiveService archives, JobEngineHost jobs, TimeProvider time, + ILogger logger) : BackgroundService { /// The desktop's rhythm (it polls its archive the same way). @@ -26,6 +28,8 @@ public sealed class ArchivePolling(TitanoDatabase db, ServerArchive archive, Job 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; foreach (var kind in new[] { JobKind.Scan, JobKind.Rebuild }) { foreach (var state in Pending) diff --git a/tests/Titano.App.Tests/Infrastructure/AppServicesTests.cs b/tests/Titano.App.Tests/Infrastructure/AppServicesTests.cs index 7d442036..187a40b0 100644 --- a/tests/Titano.App.Tests/Infrastructure/AppServicesTests.cs +++ b/tests/Titano.App.Tests/Infrastructure/AppServicesTests.cs @@ -51,6 +51,53 @@ public sealed class AppServicesTests } } + /// BRIEF §7: polling follows the archive folder, also one chosen after the start (first run, Settings, relink). + [Test] + [WindowsOnly] + public void RescansFollowTheArchiveFolderChosenAfterTheStart() + { + var environment = AppEnvironment.ForCapture(); + try + { + SingleThreadedContext.Run(async () => + { + // The first run: the index opens before any archive folder is set. + var settings = new AppSettings { Language = "en" }; + var services = new AppServices(environment, settings, () => { }, new NoDialogs()); + try + { + await services.OpenAsync(environment.DatabasePath); + Assert.Null(await services.RescanAsync()); + // The rescans run all the same: Settings › Archive › Rescan every reaches them. + settings.ScanIntervalMinutes = 30; + services.SaveSettings(); + Assert.Equal(TimeSpan.FromMinutes(30), services.PollingPeriod); + + // The first-run dialog creates an archive: the next rescan is of it. + await services.ArchiveSetup.CreateAsync(Path.Combine(environment.DataDirectory, "first")); + Assert.Equal("first", (await services.RescanAsync())?.Name); + + // Settings › Archive moves to another archive: the next rescan is of that one, no longer of the first. + var second = Path.Combine(environment.DataDirectory, "second"); + await new ArchiveService(services.Database!).CreateAsync(second, "Second"); + settings.LastArchivePath = second; + services.SaveSettings(); + var rescanned = Assert.NotNull(await services.RescanAsync()); + Assert.Equal("Second", rescanned.Name); + Assert.Contains((await services.Database!.Jobs.ListAsync(null, JobKind.Scan)).Items, j => j.TargetIds.Contains(rescanned.Id)); + } + finally + { + Assert.True(services.DisposeAsync().AsTask().Wait(TimeSpan.FromSeconds(10)), "services stopped"); + } + }); + } + finally + { + environment.DeleteCaptureData(); + } + } + /// BRIEF §6.2: a restore staged in Settings › Advanced recovers an index that no longer opens. [Test] [WindowsOnly] diff --git a/tests/Titano.Data.Tests/Archiving/ArchiveSettingsTests.cs b/tests/Titano.Data.Tests/Archiving/ArchiveSettingsTests.cs index 258b2bd7..fe5ab3bb 100644 --- a/tests/Titano.Data.Tests/Archiving/ArchiveSettingsTests.cs +++ b/tests/Titano.Data.Tests/Archiving/ArchiveSettingsTests.cs @@ -114,6 +114,39 @@ public sealed class ArchiveSettingsTests(TestContext context) fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Proxies = s.Proxies with { LongEdge = 2560 } }, fx.Ct)); var shown = await fx.Service.GetSettingsAsync(fx.Archive.Id, fx.Ct); Assert.Equal((1280, FrameImageRenderer.ThumbnailLongEdge), (shown.Proxies.LongEdge, shown.Thumbnails.LongEdge)); + // A refresh reports that only the index's stand-ins are known: the thumbnail size above is not the archive's. + Assert.Null(await fx.Service.RefreshSettingsAsync(fx.Archive.Id, fx.Ct)); + } + + [Test] + public async Task ARefreshBringsTheIndexUpToSettingsChangedElsewhere() + { + await using var fx = await ArchiveFixture.CreateAsync(context); + // Another process sharing the archive (a server's admin panel) changed archive.json: this index still has the old row. + var manifest = ManifestStore.ReadArchive(fx.Paths.ManifestPath); + ManifestStore.WriteArchive(fx.Paths.ManifestPath, manifest with + { + Name = "Shared", + LayoutTemplates = manifest.LayoutTemplates with { Folder = "{year}/{camera}" }, + ProxyPolicy = manifest.ProxyPolicy with { LongEdge = 2560 }, + ThumbnailPolicy = manifest.ThumbnailPolicy with { LongEdge = 480 }, + }); + Assert.Equal(fx.Archive.FolderTemplate, (await fx.Db.Archives.GetAsync(fx.Archive.Id, fx.Ct))!.FolderTemplate); + + var settings = Assert.NotNull(await fx.Service.RefreshSettingsAsync(fx.Archive.Id, fx.Ct)); + Assert.Equal(("{year}/{camera}", 2560, 480), (settings.Templates.Folder, settings.Proxies.LongEdge, settings.Thumbnails.LongEdge)); + var row = (await fx.Db.Archives.GetAsync(fx.Archive.Id, fx.Ct))!; + Assert.Equal(("Shared", "{year}/{camera}", 2560), (row.Name, row.FolderTemplate, row.ProxyLongEdge)); + + // The drive is unplugged: nothing is known beyond the index, which stays as it is. + File.Move(fx.Paths.ManifestPath, fx.Paths.ManifestPath + ".away"); + Assert.Null(await fx.Service.RefreshSettingsAsync(fx.Archive.Id, fx.Ct)); + Assert.Equal("{year}/{camera}", (await fx.Db.Archives.GetAsync(fx.Archive.Id, fx.Ct))!.FolderTemplate); + + // The folder now holds another archive: its settings are neither shown nor registered as this one's. + ManifestStore.WriteArchive(fx.Paths.ManifestPath, manifest with { Id = Guid.CreateVersion7(), Name = "Other" }); + Assert.Null(await fx.Service.RefreshSettingsAsync(fx.Archive.Id, fx.Ct)); + Assert.Equal("Shared", (await fx.Db.Archives.GetAsync(fx.Archive.Id, fx.Ct))!.Name); } [Test] diff --git a/tests/Titano.Server.Tests/Storage/ArchiveModuleTests.cs b/tests/Titano.Server.Tests/Storage/ArchiveModuleTests.cs index 9112850f..68520d42 100644 --- a/tests/Titano.Server.Tests/Storage/ArchiveModuleTests.cs +++ b/tests/Titano.Server.Tests/Storage/ArchiveModuleTests.cs @@ -102,6 +102,28 @@ public sealed class ArchiveModuleTests(TestContext context) await UntilIndexedAsync(server, archive.Id, "garden"); } + [Test] + public async Task SettingsADesktopChangedInArchiveJsonReachTheServerAtTheNextPoll() + { + await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); + var archive = server.Services.GetRequiredService(); + // A desktop sharing the archive changed its folder template and proxy size in archive.json. + var path = new ArchivePaths(archive.Root).ManifestPath; + var manifest = ManifestStore.ReadArchive(path); + ManifestStore.WriteArchive(path, manifest with + { + LayoutTemplates = manifest.LayoutTemplates with { Folder = "{camera}/{date}" }, + ProxyPolicy = manifest.ProxyPolicy with { LongEdge = 2560 }, + }); + + await server.Services.GetRequiredService().PollAsync(context.Cancellation); + + // Uploads (the index row) and the admin panel (the served archive) follow it. + Assert.Equal(("{camera}/{date}", 2560), (archive.Current.FolderTemplate, archive.Current.ProxyLongEdge)); + var row = Assert.NotNull(await server.Database.Archives.GetAsync(archive.Id, context.Cancellation)); + Assert.Equal(("{camera}/{date}", 2560), (row.FolderTemplate, row.ProxyLongEdge)); + } + private static async Task UntilIndexedAsync(TestServer server, Guid archiveId, string folder) { var until = DateTime.UtcNow + TimeSpan.FromSeconds(30);