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;
///
/// 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 of
/// this PC with the handlers of , the that queues the
/// recurring work (owner review 2, R10: the archive scan, the daily index backup and the housekeeping are its system
/// schedules, ), 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 archive at Settings › Archive's folder is reopened at start before the
/// scheduler starts, and while it is unplugged every 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 ; 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
{
/// The quick preview preset: the one render "Hardware for previews only" leaves on the GPU.
private const string PreviewPresetId = "720p-preview";
private static readonly Lazy Http = new(() => new HttpClient { Timeout = TimeSpan.FromSeconds(20) });
/// Update packages are large: their download fails when it stalls (), never after a fixed total time.
private static readonly Lazy DownloadHttp = new(() => new HttpClient { Timeout = Timeout.InfiniteTimeSpan });
private readonly CancellationTokenSource _stopping = new();
private readonly Dictionary _scans = [];
private readonly TaskCompletionSource _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; }
/// First-run and Library archive actions.
public IArchiveSetup ArchiveSetup { get; set; }
/// The archive engine; null until the index is open.
public ArchiveService? Archives { get; private set; }
/// Analysis and render engine; null until the index is open.
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();
}
}
/// Sync with the signed-in server: engine, job connection, background comparison.
public SyncHost Sync { get; }
///
/// 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.
///
public event Action? SequenceReorganized;
/// The workspace where Edit opens projects (owner review 2, R8), with this PC's archive or the signed-in server as their home.
public WorkspaceHost Workspace { get; }
public IEncoderInventory Encoders { get; set; }
/// Decode tier per format on this PC and the missing codec extensions (Settings › Formats).
public IFormatInventory Formats { get; set; }
/// How often an archive that was unplugged at start is looked for again, until it is back.
public static TimeSpan ReopenInterval { get; } = TimeSpan.FromMinutes(1);
/// What the scheduler works with on this PC: its zone, the current archive, the system schedules.
public DesktopScheduleHost ScheduleHost { get; }
/// The schedules of this PC (Jobs › Scheduled); null until they run ().
public Scheduler? Scheduler { get; private set; }
/// True once runs; false when it never will (no index, no engine, it failed, the app stops).
public Task SchedulerReady => _schedulerReady.Task;
/// The clock of the schedules; the capture stops it at its sample moment.
public TimeProvider SchedulerTime { get; set; } = TimeProvider.System;
/// How the restore staged in Settings › Advanced went when the index opened; null when none was staged.
public StagedRestoreResult? StagedRestore { get; private set; }
/// Card detection, eject and format (BRIEF §13); started with the index, replaced by a fake in capture.
public IDeviceMonitor Devices { get; set; } = new WindowsDeviceMonitor();
/// Latest public release (Gitea releases API of the releases repository, BRIEF §18).
public Func> CheckForUpdateAsync { get; set; }
/// Downloads and verifies the package of a release for this copy (setup, or portable zip; BRIEF §18 P1) and returns its path.
public Func?, CancellationToken, Task> DownloadUpdateAsync { get; set; }
/// The index; null until succeeded.
public ChronosDatabase? Database { get; private set; }
public JobEngine? Engine { get; private set; }
public IJobQueue? Jobs { get; private set; }
public ThumbnailService? Thumbnails { get; private set; }
/// Why the index could not be opened; null when it is open.
public Exception? OpenError { get; private set; }
/// Node id of this PC's workers.
public string WorkerId { get; init; } = System.Environment.MachineName;
/// "0.1.0" (informational version).
public static string VersionText { get; } =
typeof(AppServices).Assembly.GetCustomAttribute()?.InformationalVersion ?? "0.0.0";
/// 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).
public static IReadOnlyList 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))
: [],
];
///
/// 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.
///
private static WorkspaceJobOptions WorkspaceOptions(AppServices services, ArchiveService archives) =>
new(services.Processing?.Options ?? WorkspaceJobOptions.For(archives).Processing,
() => services.Settings.PauseUploadsOnMetered && MeteredNetwork.IsMetered(),
services.Workspace.MovedAsync);
///
/// Queues a scan of unless one is already waiting or running; the sequences it indexes get
/// a background deep analysis when Settings › Analysis asks for it.
///
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();
}
/// Writes the settings file and nothing else: for choices that change only what is shown (the rail, the sequence edited last).
public void SavePreferences() => SaveSettingsAction();
///
/// 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.
///
public void ApplyJobSettings()
{
Engine?.Wake();
Scheduler?.Wake();
}
/// Photos the running job of this PC processes at once (Settings › Rendering › Parallelism, Auto: half the logical cores).
public int Parallelism => ProcessingTuning.Parallelism(Settings, System.Environment.ProcessorCount);
/// The priority queue of Settings › Rendering, as the job engine reads it.
public JobPriorityQueue PriorityQueue => JobPriorityQueue.From(Settings.PriorityQueue, Settings.JobPriorities);
/// Starts, restarts or stops the render node to match the opt-in, the server session and the trusted certificates (BRIEF §14.7).
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)));
/// Settings › Rendering: whether a render of may use hardware encoders.
public static bool AllowsHardware(HardwarePreference preference, string presetId) => preference switch
{
HardwarePreference.PreferHardware => true,
HardwarePreference.HardwareForPreviews => presetId.Equals(PreviewPresetId, StringComparison.OrdinalIgnoreCase),
_ => false,
};
///
/// Opens (or creates) the index at , 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 (, false at once
/// when the index or the engine does not open).
///
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);
}
///
/// 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.
///
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);
}
/// 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.
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);
}
///
/// 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 (). An archive whose archive.json is
/// not there (an unplugged drive) is looked for every 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.
///
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);
}
}
///
/// 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 (the first pass fired it) or its scan is still pending.
///
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);
}
}
///
/// Starts on this PC's engine: it seeds the system schedules and runs its first pass before
/// turns true. Without an engine, or when the schedules cannot be read, there is none.
///
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);
}
///
/// 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 ().
///
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();
}
/// 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).
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();
}
///
/// The render node of : this PC's encoders (Media Foundation, then managed), downloads and
/// outputs in the cache folder within its size limit.
///
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);
}
/// A started node and the client it owns.
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();
}
}
}
/// How a restore staged in Settings › Advanced went at start.
public enum StagedRestoreResult
{
/// The index is the chosen backup now; the one it replaced was backed up first.
Restored,
/// The staged copy failed its checks: it was set aside and the index kept.
Unreadable,
/// The index could not be backed up or replaced: it was kept and the restore runs at the next start.
Deferred,
}