CopyEngine puts the planners together behind ICopyEngine so the App can find, compare, integrate, merge, link, unlink and keep pairs apart. It also moves photos that one side deleted to the other side's trash. Each trash step re-reads both sides when applied, and nothing moves if any photo was restored since the dry run. The sync agent now queues the new photos of pairs that were integrated with automatic photos, both ways, at background priority. Failures back off, and three in a row are reported so the pair can show "not sent". On a metered connection, integrations are held like other server transfers. AppServices builds the engine and registers the Integrate and Merge handlers. A CopyWatch looks for local copies after start and after jobs that change photos. Merges wait for workspace work that has not been sent home. Refs #73 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
758 lines
40 KiB
C#
758 lines
40 KiB
C#
using System.ComponentModel;
|
||
using System.IO;
|
||
using System.Net.Http;
|
||
using System.Reflection;
|
||
using Chronos.App.Archiving;
|
||
using Chronos.App.Import;
|
||
using Chronos.App.Jobs;
|
||
using Chronos.App.Library;
|
||
using Chronos.App.Server;
|
||
using Chronos.App.Settings;
|
||
using Chronos.App.Workspace;
|
||
using Chronos.Core.Archiving;
|
||
using Chronos.Core.Diagnostics;
|
||
using Chronos.Core.Export;
|
||
using Chronos.Core.IO;
|
||
using Chronos.Core.Imaging;
|
||
using Chronos.Core.Imaging.Decoders;
|
||
using Chronos.Core.Model;
|
||
using Chronos.Core.Updates;
|
||
using Chronos.Core.Video.Managed;
|
||
using Chronos.Core.Windows.MediaFoundation;
|
||
using Chronos.Core.Windows.Wic;
|
||
using Chronos.Data;
|
||
using Chronos.Data.Archiving;
|
||
using Chronos.Data.Copies;
|
||
using Chronos.Data.Homes;
|
||
using Chronos.Data.Jobs;
|
||
using Chronos.Data.Processing;
|
||
using Chronos.Data.Projects;
|
||
using Chronos.Data.Scheduling;
|
||
using Chronos.Data.Sync;
|
||
using Chronos.Data.Workspaces;
|
||
using Chronos.Protocol.Client;
|
||
|
||
namespace Chronos.App.Infrastructure;
|
||
|
||
/// <summary>
|
||
/// Composition root of local mode (BRIEF §4.4): the SQLite index under the data folder (migrated on open, a restore
|
||
/// staged in Settings › Advanced carried out first), the archive engine (WIC first, then managed decoders), the analysis
|
||
/// and render engine (the same decoders; Media Foundation, then managed encoders), the one <see cref="JobEngine"/> of
|
||
/// this PC with the handlers of <see cref="CreateJobHandlers"/>, the <see cref="Data.Scheduling.Scheduler"/> that queues the
|
||
/// recurring work (owner review 2, R10: the archive scan, the daily index backup and the housekeeping are its system
|
||
/// schedules, <see cref="DesktopScheduleHost"/>), thumbnails, the server session, the render node, the encoder and format
|
||
/// inventories, the update check and the sync with the signed-in server (<see cref="SyncHost"/>). Started after the
|
||
/// window shows, stopped when the app exits. The archive at Settings › Archive's folder is reopened at start before the
|
||
/// scheduler starts, and while it is unplugged every <see cref="ReopenInterval"/> until it is back; the schedules follow
|
||
/// the folder, also 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 <see cref="ProcessingTuning"/>; parallelism
|
||
/// as the engine's workers). The render node runs while the user offers this PC (<see cref="AppSettings.RenderNodeOptIn"/>)
|
||
/// and a server session is open (never in capture mode). Renders and the render node use hardware encoders as
|
||
/// Settings › Rendering allows (<see cref="AllowsHardware(HardwarePreference, string)"/>). Disposal never needs the UI
|
||
/// thread, which waits for it.
|
||
/// </summary>
|
||
public sealed class AppServices : IAsyncDisposable
|
||
{
|
||
/// <summary>The quick preview preset: the one render "Hardware for previews only" leaves on the GPU.</summary>
|
||
private const string PreviewPresetId = "720p-preview";
|
||
|
||
private static readonly Lazy<HttpClient> Http = new(() => new HttpClient { Timeout = TimeSpan.FromSeconds(20) });
|
||
|
||
/// <summary>Update packages are large: their download fails when it stalls (<see cref="UpdateDownload.IdleTimeout"/>), never after a fixed total time.</summary>
|
||
private static readonly Lazy<HttpClient> DownloadHttp = new(() => new HttpClient { Timeout = Timeout.InfiniteTimeSpan });
|
||
private readonly CancellationTokenSource _stopping = new();
|
||
private readonly Dictionary<Guid, Guid> _scans = [];
|
||
private readonly TaskCompletionSource<bool> _schedulerReady = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||
private Task _schedules = Task.CompletedTask;
|
||
private WicDecoderProvider? _wic;
|
||
private readonly RenderNodeHost _renderNode;
|
||
private ServerSession _server = null!;
|
||
private CopyEngine? _copies;
|
||
private CopyWatch? _copyWatch;
|
||
|
||
public AppServices(AppEnvironment environment, AppSettings settings, Action saveSettings, IAppDialogs dialogs)
|
||
{
|
||
Environment = environment;
|
||
Settings = settings;
|
||
SaveSettingsAction = saveSettings;
|
||
Dialogs = dialogs;
|
||
// The editor's open and closed groups survive a restart (owner review 1, R3).
|
||
Chronos.App.Editor.EditorSettings.Groups.Bind(settings.EditorGroups, saveSettings);
|
||
_renderNode = new RenderNodeHost(StartRenderNode);
|
||
Share = new LibraryShare(this);
|
||
ArchiveSetup = new EngineArchiveSetup(this);
|
||
Sync = new SyncHost(this);
|
||
ScheduleHost = new DesktopScheduleHost(settings, () => Database, () => Archives);
|
||
Workspace = new WorkspaceHost(environment, settings, saveSettings, () => Database, () => Archives, () => Engine);
|
||
// A server that keeps projects is the home of the workspace projects of its sequences (owner review 2, R8, phase 5b).
|
||
Workspace.ServerHomes = Sync.ProjectHome;
|
||
// A server home's folder is named by its server's id, whichever address reached it (owner review 5, #66).
|
||
Workspace.Identity = () => Sync.Identity;
|
||
Sync.ProjectChanged += Workspace.OnServerProjectChanged;
|
||
Server = new ServerSession(settings, saveSettings, new ChronosServerApiFactory(), new DpapiTokenProtector());
|
||
Encoders = new MediaFoundationInventory();
|
||
Formats = new WicFormatInventory(() => _wic);
|
||
CheckForUpdateAsync = ct => UpdateCheck.FetchAsync(Http.Value, ProductLinks.StableReleasesApi, ProductLinks.Repository, VersionText, ct);
|
||
DownloadUpdateAsync = (release, progress, ct) =>
|
||
UpdateDownload.DownloadAsync(DownloadHttp.Value, release, environment.IsPortable, UpdateDownload.Root(Path.GetTempPath()), VersionText, progress,
|
||
UpdateDownload.IdleTimeout, ct);
|
||
}
|
||
|
||
public AppEnvironment Environment { get; }
|
||
public AppSettings Settings { get; }
|
||
public IAppDialogs Dialogs { get; }
|
||
|
||
private Action SaveSettingsAction { get; }
|
||
|
||
/// <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; }
|
||
|
||
/// <summary>Analysis and render engine; null until the index is open.</summary>
|
||
public ProcessingService? Processing { get; private set; }
|
||
|
||
public ServerSession Server
|
||
{
|
||
get => _server;
|
||
set
|
||
{
|
||
if (_server is not null) _server.PropertyChanged -= OnServerChanged;
|
||
_server = value;
|
||
_server.IsOwnLibrary = Share.IsOwn;
|
||
_server.PropertyChanged += OnServerChanged;
|
||
UpdateRenderNode();
|
||
Sync.Update();
|
||
}
|
||
}
|
||
|
||
/// <summary>Sync with the signed-in server: engine, job connection, background comparison.</summary>
|
||
public SyncHost Sync { get; }
|
||
|
||
/// <summary>Settings › Server › Share this library: this PC's library served to the LAN while Chronos runs (owner review 5, #67).</summary>
|
||
public LibraryShare Share { get; }
|
||
|
||
/// <summary>
|
||
/// A job of this PC moved a sequence's folder or changed its title or file names (a Reorganize step), or replaced its
|
||
/// frames' files (a conversion), with the sequence's id (any thread): an open editor reads it again where and as it is now.
|
||
/// </summary>
|
||
public event Action<Guid>? SequenceReorganized;
|
||
|
||
/// <summary>The workspace where Edit opens projects (owner review 2, R8), with this PC's archive or the signed-in server as their home.</summary>
|
||
public WorkspaceHost Workspace { get; }
|
||
|
||
public IEncoderInventory Encoders { get; set; }
|
||
|
||
/// <summary>Decode tier per format on this PC and the missing codec extensions (Settings › Formats).</summary>
|
||
public IFormatInventory Formats { get; set; }
|
||
|
||
/// <summary>How often an archive that was unplugged at start is looked for again, until it is back.</summary>
|
||
public static TimeSpan ReopenInterval { get; } = TimeSpan.FromMinutes(1);
|
||
|
||
/// <summary>What the scheduler works with on this PC: its zone, the current archive, the system schedules.</summary>
|
||
public DesktopScheduleHost ScheduleHost { get; }
|
||
|
||
/// <summary>The schedules of this PC (Jobs › Scheduled); null until they run (<see cref="SchedulerReady"/>).</summary>
|
||
public Scheduler? Scheduler { get; private set; }
|
||
|
||
/// <summary>True once <see cref="Scheduler"/> runs; false when it never will (no index, no engine, it failed, the app stops).</summary>
|
||
public Task<bool> SchedulerReady => _schedulerReady.Task;
|
||
|
||
/// <summary>The clock of the schedules; the capture stops it at its sample moment.</summary>
|
||
public TimeProvider SchedulerTime { get; set; } = TimeProvider.System;
|
||
|
||
/// <summary>How the restore staged in Settings › Advanced went when the index opened; null when none was staged.</summary>
|
||
public StagedRestoreResult? StagedRestore { get; private set; }
|
||
|
||
/// <summary>What the sorting of the folders into homes did when the index opened (owner review 5, #66); null when it had nothing to do.</summary>
|
||
public HomeLayoutResult? HomeLayout { get; private set; }
|
||
|
||
/// <summary>Why the folders could not be sorted into homes when the index opened; null when they were, or had nothing to sort.</summary>
|
||
public Exception? HomeLayoutError { get; private set; }
|
||
|
||
/// <summary>Card detection, eject and format (BRIEF §13); started with the index, replaced by a fake in capture.</summary>
|
||
public IDeviceMonitor Devices { get; set; } = new WindowsDeviceMonitor();
|
||
|
||
/// <summary>Latest public release (Gitea releases API of the releases repository, BRIEF §18).</summary>
|
||
public Func<CancellationToken, Task<ReleaseInfo?>> CheckForUpdateAsync { get; set; }
|
||
|
||
/// <summary>Downloads and verifies the package of a release for this copy (setup, or portable zip; BRIEF §18 P1) and returns its path.</summary>
|
||
public Func<ReleaseInfo, IProgress<double>?, CancellationToken, Task<string>> DownloadUpdateAsync { get; set; }
|
||
|
||
/// <summary>The index; null until <see cref="OpenAsync"/> succeeded.</summary>
|
||
public ChronosDatabase? Database { get; private set; }
|
||
|
||
public JobEngine? Engine { get; private set; }
|
||
|
||
public IJobQueue? Jobs { get; private set; }
|
||
|
||
public ThumbnailService? Thumbnails { get; private set; }
|
||
|
||
/// <summary>Why the index could not be opened; null when it is open.</summary>
|
||
public Exception? OpenError { get; private set; }
|
||
|
||
/// <summary>Node id of this PC's workers.</summary>
|
||
public string WorkerId { get; init; } = System.Environment.MachineName;
|
||
|
||
/// <summary>"0.1.0" (informational version).</summary>
|
||
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; a kind without a handler stays
|
||
/// queued (it is never claimed here). The workspace's jobs are the desktop's alone: they write into this PC's workspace
|
||
/// (a copy from this PC's archive or, through the signed-in session, from a server; a local render with this PC's
|
||
/// encoders; the sending of its renders home; the move of its folder, which Settings › Workspace then names).</summary>
|
||
public static IReadOnlyList<IJobHandler> CreateJobHandlers(AppServices services) =>
|
||
[
|
||
.. services.Archives?.CreateJobHandlers() ?? [], .. services.Processing?.CreateJobHandlers() ?? [],
|
||
.. services.Sync.Service?.CreateJobHandlers(services.Sync) ?? [],
|
||
.. services.Database is { } db && services.Archives is { } archives
|
||
? WorkspaceJobs.CreateHandlers(db, archives, services.Sync, WorkspaceOptions(services, archives))
|
||
: [],
|
||
.. CopyHandlers(services),
|
||
];
|
||
|
||
/// <summary>
|
||
/// The matches' jobs (owner review 6, #73): the integration of a pair with a server sequence, over the signed-in session,
|
||
/// and the merge of two local sequences, which waits for workspace work not sent home.
|
||
/// </summary>
|
||
private static IReadOnlyList<IJobHandler> CopyHandlers(AppServices services)
|
||
{
|
||
if (services.Database is not { } db || services.Archives is not { } archives || services.Sync.Service is not { } sync) return [];
|
||
return
|
||
[
|
||
CopyJobs.CreateIntegrateHandler(sync, archives, services.Sync, sync.Copies),
|
||
SequenceMerger.CreateHandler(db, new SequenceMerger(db, archives, new CopySides(db)), archives, new ProjectTransplant(),
|
||
new WorkspaceCopyGuard(services.Workspace)),
|
||
];
|
||
}
|
||
|
||
/// <summary>
|
||
/// The matches engine over the open index (owner review 6, #73): the planners, the workspace guard of merges, the
|
||
/// signed-in server's comparisons through its agent, and the names the servers are known by here.
|
||
/// </summary>
|
||
private CopyEngine CreateCopyEngine(ChronosDatabase database, ArchiveService archives, SyncService sync)
|
||
{
|
||
var sides = new CopySides(database);
|
||
var homes = CopyJobs.ServerHomes(Sync);
|
||
return new CopyEngine(sync, archives, new IntegratePlanner(sync, sides, archives.Projects, homes), new SequenceMerger(database, archives, sides),
|
||
new CopyReviewBuilder(database, archives.Projects, sides, homes), CopyContext, new WorkspaceCopyGuard(Workspace),
|
||
refresh: async (remote, ct) =>
|
||
{
|
||
// The signed-in server compares through its agent, one comparison at a time; another (a job's session) directly.
|
||
if (Sync.Agent is { } agent && string.Equals(agent.Server, remote.Server, StringComparison.Ordinal)) await agent.RefreshAsync(ct).ConfigureAwait(false);
|
||
else await sync.RefreshAsync(remote, Sync.Policy, ct).ConfigureAwait(false);
|
||
},
|
||
serverNames: ServerNameOf,
|
||
followUps: () => Engine,
|
||
workspaceProjects: async ct => Workspace.IsAvailable
|
||
? [.. (await Workspace.ListAsync(ct).ConfigureAwait(false)).Where(e => e.IsReadable).Select(e => (e.Home, e.Name))]
|
||
: []);
|
||
}
|
||
|
||
/// <summary>
|
||
/// What the planners know of this PC and the signed-in account. The account's rights stand for its rights over the
|
||
/// server's sequence: the server enforces ownership itself, so OwnsOrAdmin reads Delete or an admin right.
|
||
/// </summary>
|
||
private CopyPlanContext CopyContext(ISyncRemote? remote)
|
||
{
|
||
var user = remote is not null ? Server.User : null;
|
||
var permissions = user?.Permissions ?? 0;
|
||
var rights = SyncRights.Of(permissions);
|
||
bool Has(Permissions p) => (permissions & (long)p) != 0;
|
||
var originals = Settings.KnownServers.Select(k => k.OriginalsArchiveId).OfType<Guid>().ToHashSet();
|
||
return new CopyPlanContext(System.Environment.MachineName, remote is null ? null : Server.ServerName, rights.CanUpload, rights.CanFetchOriginals,
|
||
rights.CanEdit, Has(Permissions.Delete), Has(Permissions.Delete) || Has(Permissions.ManageArchive) || Has(Permissions.ManageUsers),
|
||
remote is not null && Sync.HasProjects, user is { QuotaBytes: > 0 } ? Math.Max(0, user.QuotaBytes - user.UsedBytes) : null, new HashSet<Guid>(),
|
||
originals);
|
||
}
|
||
|
||
/// <summary>The name a server key is shown by: the known server's name (by its id), else none (the key is shown).</summary>
|
||
private string? ServerNameOf(string key) =>
|
||
ServerIdentity.IdOf(Sync.Identity.Canonical(key)) is { } id && Settings.FindKnownServer(id) is { Name: { Length: > 0 } name } ? name : null;
|
||
|
||
/// <summary>
|
||
/// The workspace jobs' share of the desktop (phase 5c): its processing options for local renders, the metered hold of
|
||
/// Settings › Server over transfers with a server, the workspace that follows a moved folder, and the folder in use
|
||
/// (a job queued before a move finds its project there).
|
||
/// </summary>
|
||
private static WorkspaceJobOptions WorkspaceOptions(AppServices services, ArchiveService archives) =>
|
||
new(services.Processing?.Options ?? WorkspaceJobOptions.For(archives).Processing,
|
||
() => services.Settings.PauseUploadsOnMetered && MeteredNetwork.IsMetered(),
|
||
services.Workspace.MovedAsync,
|
||
() => services.Workspace.Root,
|
||
services.Workspace.SameServer);
|
||
|
||
/// <summary>
|
||
/// Queues a scan of <paramref name="archive"/> unless one is already waiting or running; the sequences it indexes get
|
||
/// a background deep analysis when Settings › Analysis asks for it.
|
||
/// </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, Settings.DeepAnalysisAfterScan), cancellationToken: ct);
|
||
lock (_scans)
|
||
{
|
||
_scans[archive.Id] = job.Id;
|
||
}
|
||
}
|
||
|
||
public void SaveSettings()
|
||
{
|
||
SaveSettingsAction();
|
||
UpdateRenderNode();
|
||
ApplyJobSettings();
|
||
if (!Environment.IsCapture) Share.Apply();
|
||
}
|
||
|
||
/// <summary>Writes the settings file and nothing else: for choices that change only what is shown (the rail, the sequence edited last).</summary>
|
||
public void SavePreferences() => SaveSettingsAction();
|
||
|
||
/// <summary>
|
||
/// A review of the priority queue (Settings › Rendering): turned on, it may make the running job yield at once; and a
|
||
/// pass of the schedules, whose runs read the settings. The parallelism applies from the next sequence or job.
|
||
/// </summary>
|
||
public void ApplyJobSettings()
|
||
{
|
||
Engine?.Wake();
|
||
Scheduler?.Wake();
|
||
}
|
||
|
||
/// <summary>Photos the running job of this PC processes at once (Settings › Rendering › Parallelism, Auto: half the logical cores).</summary>
|
||
public int Parallelism => ProcessingTuning.Parallelism(Settings, System.Environment.ProcessorCount);
|
||
|
||
/// <summary>The priority queue of Settings › Rendering, as the job engine reads it.</summary>
|
||
public JobPriorityQueue PriorityQueue => JobPriorityQueue.From(Settings.PriorityQueue, Settings.JobPriorities);
|
||
|
||
/// <summary>Starts, restarts or stops the render node to match the opt-in, the server session and the trusted certificates (BRIEF §14.7).</summary>
|
||
public void UpdateRenderNode() =>
|
||
_renderNode.Update(RenderNodeSession.For(Settings.RenderNodeOptIn && Processing is not null && !Environment.IsCapture, _server.IsConnected,
|
||
_server.Flow.Address, _server.Flow.Token, _server.Fingerprints, AllowsHardware(Settings.HardwareEncoding, ExportResolver.CustomId)));
|
||
|
||
/// <summary>Settings › Rendering: whether a render of <paramref name="presetId"/> may use hardware encoders.</summary>
|
||
public static bool AllowsHardware(HardwarePreference preference, string presetId) => preference switch
|
||
{
|
||
HardwarePreference.PreferHardware => true,
|
||
HardwarePreference.HardwareForPreviews => presetId.Equals(PreviewPresetId, StringComparison.OrdinalIgnoreCase),
|
||
_ => false,
|
||
};
|
||
|
||
/// <summary>
|
||
/// Opens (or creates) the index at <paramref name="databasePath"/>, migrates it (after a restore staged in Settings ›
|
||
/// Advanced replaced it, which needs no open index: it also recovers one that no longer opens), starts the job engine,
|
||
/// then in the background reopens the archive and starts the schedules (<see cref="SchedulerReady"/>, false at once
|
||
/// when the index or the engine does not open).
|
||
/// </summary>
|
||
public async Task OpenAsync(string databasePath, CancellationToken ct = default)
|
||
{
|
||
await RestoreStagedAsync(databasePath, ct);
|
||
try
|
||
{
|
||
Database = await Task.Run(() => ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(databasePath), ct), ct);
|
||
Log.Info("app", $"Index open (schema {Database.SchemaVersion}) at {databasePath}");
|
||
}
|
||
catch (Exception ex) when (ex is DatabaseException or System.IO.IOException or UnauthorizedAccessException or DllNotFoundException)
|
||
{
|
||
OpenError = ex;
|
||
Log.Error("app", "The index could not be opened", ex);
|
||
// No index, no schedules: Jobs › Scheduled says so instead of waiting for them.
|
||
_schedulerReady.TrySetResult(false);
|
||
return;
|
||
}
|
||
// Before any job runs: the folders of the workspace and the cache go to their homes (owner review 5, #66).
|
||
await SortHomesAsync(Database, ct);
|
||
try
|
||
{
|
||
await StartJobsAsync(Database, startEngine: true, ct);
|
||
Thumbnails = new ThumbnailService(Database, Settings.ThumbnailMemoryItems);
|
||
Devices.Start();
|
||
}
|
||
catch
|
||
{
|
||
_schedulerReady.TrySetResult(false);
|
||
throw;
|
||
}
|
||
_schedules = RunSchedulesAsync(_stopping.Token);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Uses an already open index (capture, tests) instead of opening one, and starts the schedules (no archive to reopen);
|
||
/// sample data keeps the engine stopped.
|
||
/// </summary>
|
||
public async Task AttachAsync(ChronosDatabase database, bool startEngine = true, CancellationToken ct = default)
|
||
{
|
||
Database = database;
|
||
await StartJobsAsync(database, startEngine, ct);
|
||
Thumbnails = new ThumbnailService(database, Settings.ThumbnailMemoryItems);
|
||
await StartSchedulerAsync(ct);
|
||
}
|
||
|
||
/// <summary>Stops the schedules, the render node and the engine, then closes the index. The app's exit waits for this on
|
||
/// the UI thread, so nothing here continues on it.</summary>
|
||
public async ValueTask DisposeAsync()
|
||
{
|
||
await _stopping.CancelAsync().ConfigureAwait(false);
|
||
await _schedules.ConfigureAwait(false);
|
||
// First: the shared library's clients and its subscriptions to the engine and the scheduler go before they do.
|
||
await Share.DisposeAsync().ConfigureAwait(false);
|
||
_schedulerReady.TrySetResult(false);
|
||
if (Scheduler is not null) await Scheduler.DisposeAsync().ConfigureAwait(false);
|
||
_stopping.Dispose();
|
||
_server.PropertyChanged -= OnServerChanged;
|
||
await _renderNode.DisposeAsync().ConfigureAwait(false);
|
||
if (_copyWatch is not null) await _copyWatch.DisposeAsync().ConfigureAwait(false);
|
||
await Sync.DisposeAsync().ConfigureAwait(false);
|
||
_copies?.Dispose();
|
||
(Jobs as IDisposable)?.Dispose();
|
||
if (Engine is not null) await Engine.DisposeAsync().ConfigureAwait(false);
|
||
Thumbnails?.Dispose();
|
||
Devices.Dispose();
|
||
Server.Dispose();
|
||
if (Database is not null) await Database.DisposeAsync().ConfigureAwait(false);
|
||
}
|
||
|
||
/// <summary>
|
||
/// At start, off the UI thread: reopens the archive at Settings › Archive's folder (a moved one relinked, interrupted
|
||
/// renames finished), then starts the schedules, whose first pass settles the runs missed while Chronos was closed, and
|
||
/// runs the archive scan once, as the rescans did at start (<see cref="ScanNowAsync"/>). An archive whose archive.json is
|
||
/// not there (an unplugged drive) is looked for every <see cref="ReopenInterval"/> until it is back, then scanned and the
|
||
/// schedules look at once for a due run; unless the folder changed meanwhile: whatever changed it opened the new
|
||
/// archive. The app's exit waits for this on the UI thread.
|
||
/// </summary>
|
||
private async Task RunSchedulesAsync(CancellationToken ct)
|
||
{
|
||
var reopen = Settings.LastArchivePath is { Length: > 0 } start ? start : null;
|
||
try
|
||
{
|
||
if (reopen is not null && await ReopenAsync(reopen, ct).ConfigureAwait(false)) reopen = null;
|
||
var started = SchedulerTime.GetUtcNow();
|
||
await StartSchedulerAsync(ct).ConfigureAwait(false);
|
||
if (reopen is null)
|
||
{
|
||
await Share.StartAsync(ct).ConfigureAwait(false);
|
||
await ScanNowAsync(started, ct).ConfigureAwait(false);
|
||
return;
|
||
}
|
||
using var timer = new PeriodicTimer(ReopenInterval);
|
||
while (await timer.WaitForNextTickAsync(ct).ConfigureAwait(false))
|
||
{
|
||
if (!string.Equals(reopen, Settings.LastArchivePath, StringComparison.OrdinalIgnoreCase)) return;
|
||
if (!await ReopenAsync(reopen, ct).ConfigureAwait(false)) continue;
|
||
await Share.StartAsync(ct).ConfigureAwait(false);
|
||
await ScanNowAsync(null, ct).ConfigureAwait(false);
|
||
Scheduler?.Wake();
|
||
return;
|
||
}
|
||
}
|
||
catch (OperationCanceledException) when (ct.IsCancellationRequested)
|
||
{
|
||
}
|
||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||
{
|
||
// The exit waits for this task: nothing it throws may stop the app from closing.
|
||
Log.Error("schedules", "The archive could not be reopened or the schedules started", ex);
|
||
}
|
||
finally
|
||
{
|
||
_schedulerReady.TrySetResult(false);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Runs the archive scan schedule once now when it is enabled (owner review 2, R10: the archive was opened or is back),
|
||
/// unless it ran since <paramref name="since"/> (the first pass fired it) or its scan is still pending.
|
||
/// </summary>
|
||
private async Task ScanNowAsync(DateTimeOffset? since, CancellationToken ct)
|
||
{
|
||
if (Scheduler is not { } scheduler) return;
|
||
try
|
||
{
|
||
await scheduler.RunSystemNowAsync(DesktopScheduleHost.ArchiveScan, since, ct).ConfigureAwait(false);
|
||
}
|
||
catch (Exception ex) when (ex is DatabaseException or IOException or InvalidOperationException)
|
||
{
|
||
Log.Warning("schedules", "The archive scan could not be run now; its schedule runs the next one", ex);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Starts <see cref="Scheduler"/> on this PC's engine: it seeds the system schedules and runs its first pass before
|
||
/// <see cref="SchedulerReady"/> turns true. Without an engine, or when the schedules cannot be read, there is none.
|
||
/// </summary>
|
||
private async Task StartSchedulerAsync(CancellationToken ct)
|
||
{
|
||
if (Database is not { } db || Engine is not { } engine || Scheduler is not null)
|
||
{
|
||
_schedulerReady.TrySetResult(Scheduler is not null);
|
||
return;
|
||
}
|
||
var scheduler = new Scheduler(db, engine, new SchedulerOptions { Time = SchedulerTime });
|
||
try
|
||
{
|
||
await Task.Run(() => scheduler.StartAsync(ScheduleHost, ct), ct).ConfigureAwait(false);
|
||
}
|
||
catch (Exception ex) when (ex is DatabaseException or IOException or InvalidOperationException or ArgumentException)
|
||
{
|
||
Log.Error("schedules", "The schedules could not be started; nothing recurring runs until the next start", ex);
|
||
await scheduler.DisposeAsync().ConfigureAwait(false);
|
||
_schedulerReady.TrySetResult(false);
|
||
return;
|
||
}
|
||
catch (OperationCanceledException)
|
||
{
|
||
await scheduler.DisposeAsync().ConfigureAwait(false);
|
||
throw;
|
||
}
|
||
Scheduler = scheduler;
|
||
Log.Info("schedules", "Schedules started");
|
||
_schedulerReady.TrySetResult(true);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Registers the archive at <paramref name="root"/> 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.
|
||
/// </summary>
|
||
private async Task<bool> 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;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 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 (<see cref="StagedRestore"/>).
|
||
/// </summary>
|
||
private async Task RestoreStagedAsync(string databasePath, CancellationToken ct)
|
||
{
|
||
try
|
||
{
|
||
if (await Task.Run(() => DatabaseBackups.RestoreStagedAsync(databasePath, Environment.BackupDirectory, ct), ct))
|
||
{
|
||
StagedRestore = StagedRestoreResult.Restored;
|
||
Log.Info("app", $"Index at {databasePath} restored from the staged copy");
|
||
}
|
||
}
|
||
catch (InvalidDataException ex)
|
||
{
|
||
StagedRestore = StagedRestoreResult.Unreadable;
|
||
Log.Error("app", "The staged index copy failed its checks; it was set aside and the index kept", ex);
|
||
}
|
||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or DatabaseException or DllNotFoundException)
|
||
{
|
||
StagedRestore = StagedRestoreResult.Deferred;
|
||
Log.Error("app", "The staged index restore could not run; the index was kept and the restore stays staged", ex);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Sorts the workspace's projects and the cache's server folders into their homes (owner review 5, #66:
|
||
/// <see cref="HomeLayoutMigration"/>; renames only, a clash to the trash, never deleted), or takes up a sorting a previous
|
||
/// start left half done. Projects in use or a workspace being moved wait for a later start; a failure is logged and kept
|
||
/// for Settings to tell (<see cref="HomeLayoutError"/>), and the start goes on: the folders not sorted read as before.
|
||
/// </summary>
|
||
private async Task SortHomesAsync(ChronosDatabase database, CancellationToken ct)
|
||
{
|
||
try
|
||
{
|
||
var migration = new HomeLayoutMigration(new HomeLayoutOptions
|
||
{
|
||
DataDirectory = Environment.DataDirectory,
|
||
WorkspaceRoot = Workspace.IsEnabled ? Workspace.Root : null,
|
||
CacheRoot = CacheStore.Folder(Environment, Settings),
|
||
Identity = Sync.Identity,
|
||
CurrentServer = Sync.LastServer,
|
||
Database = database,
|
||
});
|
||
var result = await Task.Run(() => migration.ApplyAsync(ct), ct);
|
||
if (result != HomeLayoutResult.Nothing)
|
||
{
|
||
HomeLayout = result;
|
||
Log.Info("app", $"Folders sorted into homes: {result.ProjectsMoved} project(s), {result.EntriesMoved} entr(ies), {result.InUse} in use, "
|
||
+ $"{result.Clashes} clash(es){(result.Deferred is { } deferred ? $", the workspace waits ({deferred})" : string.Empty)}");
|
||
}
|
||
}
|
||
catch (Exception ex) when (ex is HomeLayoutException or IOException or UnauthorizedAccessException or InvalidDataException or DatabaseException)
|
||
{
|
||
HomeLayoutError = ex;
|
||
Log.Error("app", "The folders could not be sorted into homes; the next start tries again", ex);
|
||
}
|
||
finally
|
||
{
|
||
Workspace.Reopen();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// The scan schedule scanned this PC's archive (any thread): the archives of the servers' downloaded originals are scanned
|
||
/// after it (owner review 5, Q1: they are archives of their own, which the schedule, made for one archive, does not name).
|
||
/// </summary>
|
||
private void OnScheduledScanDone(Job job)
|
||
{
|
||
if (job is not { Kind: JobKind.Scan, State: JobState.Done, ScheduleId: not null, TargetIds: [var scanned] }) return;
|
||
_ = Task.Run(async () =>
|
||
{
|
||
try
|
||
{
|
||
var archives = await Sync.OriginalsArchivesAsync(_stopping.Token).ConfigureAwait(false);
|
||
if (archives.Any(a => a.Id == scanned)) return;
|
||
foreach (var originals in archives) await QueueScanAsync(originals, full: false, _stopping.Token).ConfigureAwait(false);
|
||
}
|
||
catch (Exception ex) when (ex is DatabaseException or InvalidOperationException or OperationCanceledException)
|
||
{
|
||
Log.Warning("app", "The archives of the servers' originals could not be queued to be scanned", ex);
|
||
}
|
||
});
|
||
}
|
||
|
||
private async Task StartJobsAsync(ChronosDatabase database, bool startEngine, CancellationToken ct)
|
||
{
|
||
_wic = new WicDecoderProvider();
|
||
var archive = new ArchiveServiceOptions
|
||
{
|
||
Decoder = DecoderFactory.CreateClient(_wic),
|
||
// Measured by Windows itself, so an archive on a network share (UNC) is measured too: a conversion checks its space.
|
||
FreeSpace = Chronos.Core.Windows.Devices.VolumeInfo.FreeBytesAt,
|
||
Parallelism = () => Parallelism,
|
||
MemoryBudget = () => ProcessingTuning.MemoryBudget(Settings, GC.GetGCMemoryInfo().TotalAvailableMemoryBytes),
|
||
// The housekeeping schedule trims the cache to Settings › Storage › Cache limit (owner review 2, R10).
|
||
// The workspace-cleanup schedule removes the media past Settings › Workspace's Clean after and Size limit, and reports its trash (phase 5c).
|
||
// The shared library's upload-cleanup schedule runs on this PC's scheduler (owner review 5, #67).
|
||
CleanupSteps = [new CacheTrimStep(() => CacheStore.Folder(Environment, Settings), () => Settings.CacheLimitGb * (1L << 30)),
|
||
new WorkspaceCleanStep(() => Workspace), new WorkspaceTrashStep(() => Workspace), new ShareUploadsStep(() => Share)],
|
||
BackupDirectory = Environment.BackupDirectory,
|
||
// While the library is shared, its clients see thumbnails and proxies of what a scan finds, as a server's do.
|
||
ImagesAfterScanWhen = () => Share.IsOn,
|
||
// Told after each step of a Reorganize, and each sequence a conversion replaced or made, while the rest of the job
|
||
// runs; the shared library's clients are told too.
|
||
SequenceChanged = (id, change, token) =>
|
||
{
|
||
SequenceReorganized?.Invoke(id);
|
||
return Share.SequenceChangedAsync(id, change, token);
|
||
},
|
||
ProjectsChanged = (changed, token) => Share.ProjectsChangedAsync(changed, token),
|
||
};
|
||
Archives = new ArchiveService(database, archive);
|
||
// The videos folder may not lie in the workspace either, and a copy of a server's renders downloads through its connection (phase 5).
|
||
Archives.Videos.ProtectedFolders = () => [Workspace.Root, Share.Folder];
|
||
Archives.Videos.Servers = Sync;
|
||
Processing = new ProcessingService(database, ProcessingOptions(archive));
|
||
var sync = Sync.Open(database, Archives, archive);
|
||
_copies = CreateCopyEngine(database, Archives, sync);
|
||
// Sample data puts its own engine in place in capture.
|
||
Sync.Copies = _copies;
|
||
var handlers = CreateJobHandlers(this);
|
||
var kinds = handlers.Select(h => h.Kind).Distinct().ToArray();
|
||
// One job at a time (owner review 1): the parallelism works inside it, the priority queue decides which runs.
|
||
Engine = new JobEngine(database, handlers, new JobEngineOptions { WorkerId = WorkerId, Kinds = kinds, PriorityQueue = () => PriorityQueue });
|
||
// The workspace's size limit runs after each copy of media into it (phase 5c).
|
||
Engine.JobChanged += Workspace.OnJobChanged;
|
||
Engine.JobChanged += OnScheduledScanDone;
|
||
// Sync follows the engine's jobs before it runs any: a render resumed at start that finishes at once is still sent (Export "Sync").
|
||
Sync.Update();
|
||
// With no handler the engine must not run: an empty kind list would let workers claim (and fail) every job.
|
||
var started = startEngine && handlers.Count > 0;
|
||
if (started) await Engine.StartAsync(ct);
|
||
Jobs = new EngineJobQueue(database, Engine, started);
|
||
// Two copies of a sequence among this PC's archives are looked for by themselves (owner review 6, #73).
|
||
if (!Environment.IsCapture)
|
||
{
|
||
var copies = _copies;
|
||
_copyWatch = new CopyWatch(ct => copies.FindLocalAsync(ct));
|
||
_copyWatch.Start(Engine);
|
||
}
|
||
Log.Info("jobs", started ? $"Job engine started for {string.Join(", ", kinds)}" : "No job handlers registered; jobs stay queued");
|
||
UpdateRenderNode();
|
||
}
|
||
|
||
/// <summary>Media Foundation (hardware first, software H.264; hardware as Settings › Rendering allows) then managed
|
||
/// encoders; at the start of each job the options take the current Settings › Rendering and Analysis (memory budget,
|
||
/// Auto a quarter of the installed memory; cache folder; quality profile; parallelism; analysis width and thresholds).</summary>
|
||
private ProcessingOptions ProcessingOptions(ArchiveServiceOptions archive) => new()
|
||
{
|
||
Archive = archive,
|
||
Encoders = [new MediaFoundationEncoderProvider(), new ManagedEncoderProvider()],
|
||
AllowsHardware = presetId => AllowsHardware(Settings.HardwareEncoding, presetId),
|
||
Host = EncoderHost.Client,
|
||
ProducedBy = System.Environment.MachineName,
|
||
// A render or an advice recorded on this PC reaches the shared library's clients (owner review 5, #67).
|
||
SequenceChanged = (id, change, token) => Share.SequenceChangedAsync(id, change, token),
|
||
Tune = basis => ProcessingTuning.Apply(basis, Settings, GC.GetGCMemoryInfo().TotalAvailableMemoryBytes, System.Environment.ProcessorCount,
|
||
CacheStore.Folder(Environment, Settings)),
|
||
};
|
||
|
||
private void OnServerChanged(object? sender, PropertyChangedEventArgs e)
|
||
{
|
||
UpdateRenderNode();
|
||
Sync.Update();
|
||
}
|
||
|
||
/// <summary>
|
||
/// The render node of <paramref name="session"/>: this PC's encoders (Media Foundation, then managed), downloads and
|
||
/// outputs in the cache folder within its size limit.
|
||
/// </summary>
|
||
private IRenderNode StartRenderNode(RenderNodeSession session)
|
||
{
|
||
var processing = Processing ?? throw new InvalidOperationException("The index is not open.");
|
||
var cache = CacheStore.Folder(Environment, Settings);
|
||
CacheStore.Prepare(cache);
|
||
var client = new ChronosClient(session.Address, new CertificatePinning(session.TrustedFingerprints), $"Chronos ({System.Environment.MachineName})")
|
||
{
|
||
Token = session.Token,
|
||
};
|
||
var node = new RenderNodeClient(client, processing.Options, new RenderNodeOptions
|
||
{
|
||
ClientName = System.Environment.MachineName,
|
||
// Each server keeps its own downloads (owner review 5, #66: the render-node folder of its home in the cache).
|
||
CacheDirectory = Path.Combine(CacheStore.ServerFolder(cache, Sync.KeyOf(session.Address), Sync.Identity), HomeFolders.RenderNode),
|
||
CacheLimitBytes = Settings.CacheLimitGb * (1L << 30),
|
||
});
|
||
node.Start();
|
||
return new RunningNode(node, client);
|
||
}
|
||
|
||
/// <summary>A started node and the client it owns.</summary>
|
||
private sealed class RunningNode(RenderNodeClient node, ChronosClient client) : IRenderNode
|
||
{
|
||
public Task Completion => node.Completion;
|
||
|
||
public async ValueTask DisposeAsync()
|
||
{
|
||
await node.DisposeAsync().ConfigureAwait(false);
|
||
client.Dispose();
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>How a restore staged in Settings › Advanced went at start.</summary>
|
||
public enum StagedRestoreResult
|
||
{
|
||
/// <summary>The index is the chosen backup now; the one it replaced was backed up first.</summary>
|
||
Restored,
|
||
|
||
/// <summary>The staged copy failed its checks: it was set aside and the index kept.</summary>
|
||
Unreadable,
|
||
|
||
/// <summary>The index could not be backed up or replaced: it was kept and the restore runs at the next start.</summary>
|
||
Deferred,
|
||
}
|