feat(app): connect the archive engine to the desktop client
The client builds the archive service on its index (WIC, then managed decoders), registers its scan, rebuild, verify, import, thumbnail, proxy and maintenance handlers, reopens the last archive at start and rescans it every 10 minutes. Welcome and Library create or adopt archives through it and queue a full scan; a failure shows a toast. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
6 files changed
+115
-45
No files matched your search
@@ -1,3 +1,4 @@
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Reflection;
|
||||
using Titano.App.About;
|
||||
@@ -5,30 +6,32 @@ using Titano.App.Jobs;
|
||||
using Titano.App.Library;
|
||||
using Titano.App.Server;
|
||||
using Titano.App.Settings;
|
||||
using Titano.Core.Archiving;
|
||||
using Titano.Core.Diagnostics;
|
||||
using Titano.Core.Imaging.Decoders;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Core.Windows.Wic;
|
||||
using Titano.Data;
|
||||
using Titano.Data.Archiving;
|
||||
using Titano.Data.Jobs;
|
||||
|
||||
namespace Titano.App.Infrastructure;
|
||||
|
||||
/// <summary>
|
||||
/// Composition root of local mode (BRIEF §4.4): the SQLite index under the data folder (migrated on open), the one
|
||||
/// <see cref="JobEngine"/> of this PC with the handlers of <see cref="CreateJobHandlers"/>, thumbnails, the server
|
||||
/// session, the encoder inventory, the update check and the archive setup. Started after the window shows, stopped
|
||||
/// when the app exits.
|
||||
/// Composition root of local mode (BRIEF §4.4): the SQLite index under the data folder (migrated on open), the
|
||||
/// archive engine (WIC first, then managed decoders), the one <see cref="JobEngine"/> of this PC with the handlers of
|
||||
/// <see cref="CreateJobHandlers"/>, thumbnails, the server session, the encoder inventory and the update check.
|
||||
/// Started after the window shows, stopped when the app exits. The last archive is reopened at start and rescanned
|
||||
/// every <see cref="ScanInterval"/> (BRIEF §7: polling, no file-system watchers).
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Integration points (one place each):
|
||||
/// <list type="bullet">
|
||||
/// <item><see cref="CreateJobHandlers"/> — register the archive handlers (scan, rebuild, verify, adopt, …) and the
|
||||
/// render handler here.</item>
|
||||
/// <item><see cref="ArchiveSetup"/> — replace <see cref="LocalArchiveSetup"/> with the archive engine's
|
||||
/// implementation (it receives the first-run choices: create a local archive, adopt a folder).</item>
|
||||
/// </list>
|
||||
/// </remarks>
|
||||
public sealed class AppServices : IAsyncDisposable
|
||||
{
|
||||
public static readonly TimeSpan ScanInterval = TimeSpan.FromMinutes(10);
|
||||
|
||||
private static readonly Lazy<HttpClient> Http = new(() => new HttpClient { Timeout = TimeSpan.FromSeconds(20) });
|
||||
private readonly CancellationTokenSource _stopping = new();
|
||||
private readonly Dictionary<Guid, Guid> _scans = [];
|
||||
private Task _polling = Task.CompletedTask;
|
||||
|
||||
public AppServices(AppEnvironment environment, AppSettings settings, Action saveSettings, IAppDialogs dialogs)
|
||||
{
|
||||
@@ -36,7 +39,7 @@ public sealed class AppServices : IAsyncDisposable
|
||||
Settings = settings;
|
||||
SaveSettingsAction = saveSettings;
|
||||
Dialogs = dialogs;
|
||||
ArchiveSetup = new LocalArchiveSetup(settings, saveSettings);
|
||||
ArchiveSetup = new EngineArchiveSetup(this);
|
||||
Server = new ServerSession(settings, saveSettings, new TitanoServerApiFactory(), new DpapiTokenProtector());
|
||||
Encoders = new MediaFoundationInventory();
|
||||
CheckForUpdateAsync = ct => UpdateCheck.FetchAsync(Http.Value, ProductLinks.LatestReleaseApi, VersionText, ct);
|
||||
@@ -48,9 +51,12 @@ public sealed class AppServices : IAsyncDisposable
|
||||
|
||||
private Action SaveSettingsAction { get; }
|
||||
|
||||
/// <summary>First-run archive actions; replaced by the integrator with the archive engine's implementation.</summary>
|
||||
/// <summary>First-run and Library archive actions.</summary>
|
||||
public IArchiveSetup ArchiveSetup { get; set; }
|
||||
|
||||
/// <summary>The archive engine; null until the index is open.</summary>
|
||||
public ArchiveService? Archives { get; private set; }
|
||||
|
||||
public ServerSession Server { get; set; }
|
||||
|
||||
public IEncoderInventory Encoders { get; set; }
|
||||
@@ -77,13 +83,27 @@ public sealed class AppServices : IAsyncDisposable
|
||||
public static string VersionText { get; } =
|
||||
typeof(AppServices).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "0.0.0";
|
||||
|
||||
/// <summary>
|
||||
/// The job handlers of this PC's engine — the single registration list. The integrator adds the archive
|
||||
/// handlers and the render handler here; a kind without a handler stays queued (it is never claimed here).
|
||||
/// </summary>
|
||||
/// <summary>The job handlers of this PC's engine — the single registration list; a kind without a handler stays
|
||||
/// queued (it is never claimed here).</summary>
|
||||
public static IReadOnlyList<IJobHandler> CreateJobHandlers(AppServices services) =>
|
||||
[
|
||||
];
|
||||
services.Archives?.CreateJobHandlers() ?? [];
|
||||
|
||||
/// <summary>Queues a scan of <paramref name="archive"/> unless one is already waiting or running.</summary>
|
||||
public async Task QueueScanAsync(Archive archive, bool full, CancellationToken ct = default)
|
||||
{
|
||||
if (Engine is null || Database is null) return;
|
||||
Guid previous;
|
||||
lock (_scans)
|
||||
{
|
||||
_scans.TryGetValue(archive.Id, out previous);
|
||||
}
|
||||
if (previous != Guid.Empty && await Database.Jobs.GetAsync(previous, ct) is { State: JobState.Queued or JobState.Running }) return;
|
||||
var job = await Engine.EnqueueAsync(ArchiveJobs.Scan(archive, full), cancellationToken: ct);
|
||||
lock (_scans)
|
||||
{
|
||||
_scans[archive.Id] = job.Id;
|
||||
}
|
||||
}
|
||||
|
||||
public void SaveSettings() => SaveSettingsAction();
|
||||
|
||||
@@ -103,6 +123,7 @@ public sealed class AppServices : IAsyncDisposable
|
||||
}
|
||||
await StartJobsAsync(Database, startEngine: true, ct);
|
||||
Thumbnails = new ThumbnailService(Database, Settings.ThumbnailMemoryItems);
|
||||
if (Settings.LastArchivePath is { Length: > 0 } root) _polling = PollAsync(root, _stopping.Token);
|
||||
}
|
||||
|
||||
/// <summary>Uses an already open index (capture, tests) instead of opening one; sample data keeps the engine stopped.</summary>
|
||||
@@ -115,6 +136,9 @@ public sealed class AppServices : IAsyncDisposable
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
await _stopping.CancelAsync();
|
||||
await _polling;
|
||||
_stopping.Dispose();
|
||||
(Jobs as IDisposable)?.Dispose();
|
||||
if (Engine is not null) await Engine.DisposeAsync();
|
||||
Thumbnails?.Dispose();
|
||||
@@ -122,8 +146,32 @@ public sealed class AppServices : IAsyncDisposable
|
||||
if (Database is not null) await Database.DisposeAsync();
|
||||
}
|
||||
|
||||
/// <summary>Reopens the last archive (relinking a moved one, finishing interrupted renames) and rescans it periodically.</summary>
|
||||
private async Task PollAsync(string root, CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Archives is null) return;
|
||||
var archive = await Task.Run(() => Archives.OpenAsync(root, ct), ct);
|
||||
using var timer = new PeriodicTimer(ScanInterval);
|
||||
do
|
||||
{
|
||||
if (ArchiveService.GetAvailability(archive) == ArchiveAvailability.Online) await QueueScanAsync(archive, full: false, ct);
|
||||
}
|
||||
while (await timer.WaitForNextTickAsync(ct));
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task StartJobsAsync(TitanoDatabase database, bool startEngine, CancellationToken ct)
|
||||
{
|
||||
Archives = new ArchiveService(database, new ArchiveServiceOptions { Decoder = DecoderFactory.CreateClient(new WicDecoderProvider()) });
|
||||
var handlers = CreateJobHandlers(this);
|
||||
var kinds = handlers.Select(h => h.Kind).Distinct().ToArray();
|
||||
Engine = new JobEngine(database, handlers, new JobEngineOptions { WorkerId = WorkerId, Kinds = kinds });
|
||||
|
||||
Reference in new issue
Block a user