Phase 5c (owner review 2, R8) is built by six packages in parallel. This commit settles everything they share, so each one touches its own files: - job kinds WorkspaceRender (21), WorkspaceSend (22) and WorkspaceMove (23) with their wire names, default priorities, notices and parameter records; their handlers are registered as skeletons that throw until their package lands - WorkspaceJobOptions and a new CreateHandlers overload; the earlier ones stay and default the options - originals in the media index (an index of phase 5b reads as proxies only), their file names, and the exports/renders.json ledger of local renders, written atomically - the optional project and version of an uploaded render - the workspace settings (size limit, default media, version on render, clean-up age, project to open) and when each project was opened, which Reset and Import keep and Export leaves out - WorkspaceHost split into partial files, with stubs for usage, trash and the folder move, removal hooks for unsent renders and last originals, and a guard that holds work while the workspace moves - every new string of the phase in English and Italian, with tooltips Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
578 lines
29 KiB
C#
578 lines
29 KiB
C#
using System.ComponentModel;
|
||
using System.IO;
|
||
using System.Net.Http;
|
||
using System.Reflection;
|
||
using Chronos.App.About;
|
||
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.Imaging;
|
||
using Chronos.Core.Imaging.Decoders;
|
||
using Chronos.Core.Model;
|
||
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.Jobs;
|
||
using Chronos.Data.Processing;
|
||
using Chronos.Data.Scheduling;
|
||
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!;
|
||
|
||
public AppServices(AppEnvironment environment, AppSettings settings, Action saveSettings, IAppDialogs dialogs)
|
||
{
|
||
Environment = environment;
|
||
Settings = settings;
|
||
SaveSettingsAction = saveSettings;
|
||
Dialogs = dialogs;
|
||
_renderNode = new RenderNodeHost(StartRenderNode);
|
||
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;
|
||
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.PropertyChanged += OnServerChanged;
|
||
UpdateRenderNode();
|
||
Sync.Update();
|
||
}
|
||
}
|
||
|
||
/// <summary>Sync with the signed-in server: engine, job connection, background comparison.</summary>
|
||
public SyncHost Sync { 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>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))
|
||
: [],
|
||
];
|
||
|
||
/// <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, and the workspace that follows a moved folder.
|
||
/// </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);
|
||
|
||
/// <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();
|
||
}
|
||
|
||
/// <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;
|
||
}
|
||
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);
|
||
_schedulerReady.TrySetResult(false);
|
||
if (Scheduler is not null) await Scheduler.DisposeAsync().ConfigureAwait(false);
|
||
_stopping.Dispose();
|
||
_server.PropertyChanged -= OnServerChanged;
|
||
await _renderNode.DisposeAsync().ConfigureAwait(false);
|
||
await Sync.DisposeAsync().ConfigureAwait(false);
|
||
(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 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 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);
|
||
}
|
||
}
|
||
|
||
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).
|
||
CleanupSteps = [new CacheTrimStep(() => CacheStore.Folder(Environment, Settings), () => Settings.CacheLimitGb * (1L << 30))],
|
||
BackupDirectory = Environment.BackupDirectory,
|
||
// Told after each step of a Reorganize, and each sequence a conversion replaced or made, while the rest of the job runs.
|
||
SequenceChanged = (id, _, _) =>
|
||
{
|
||
SequenceReorganized?.Invoke(id);
|
||
return Task.CompletedTask;
|
||
},
|
||
};
|
||
Archives = new ArchiveService(database, archive);
|
||
Processing = new ProcessingService(database, ProcessingOptions(archive));
|
||
Sync.Open(database, Archives, archive);
|
||
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 });
|
||
// 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);
|
||
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,
|
||
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,
|
||
CacheDirectory = Path.Combine(cache, "render-node"),
|
||
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,
|
||
}
|