wip(processing): analysis and render job handlers, render node client (interrupted)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
30 files changed
+2262
-21
No files matched your search
@@ -1,3 +1,4 @@
|
||||
using System.ComponentModel;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Reflection;
|
||||
@@ -8,21 +9,29 @@ using Titano.App.Server;
|
||||
using Titano.App.Settings;
|
||||
using Titano.Core.Archiving;
|
||||
using Titano.Core.Diagnostics;
|
||||
using Titano.Core.Export;
|
||||
using Titano.Core.Imaging;
|
||||
using Titano.Core.Imaging.Decoders;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Core.Video.Managed;
|
||||
using Titano.Core.Windows.MediaFoundation;
|
||||
using Titano.Core.Windows.Wic;
|
||||
using Titano.Data;
|
||||
using Titano.Data.Archiving;
|
||||
using Titano.Data.Jobs;
|
||||
using Titano.Data.Processing;
|
||||
using Titano.Protocol.Client;
|
||||
|
||||
namespace Titano.App.Infrastructure;
|
||||
|
||||
/// <summary>
|
||||
/// Composition root of local mode (BRIEF §4.4): the SQLite index under the data folder (migrated on open), the
|
||||
/// archive engine (WIC first, then managed decoders), the one <see cref="JobEngine"/> of this PC with the handlers of
|
||||
/// <see cref="CreateJobHandlers"/>, thumbnails, the server session, the encoder inventory and the update check.
|
||||
/// Started after the window shows, stopped when the app exits. The last archive is reopened at start and rescanned
|
||||
/// every <see cref="ScanInterval"/> (BRIEF §7: polling, no file-system watchers).
|
||||
/// 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"/>, thumbnails, the server session, the render node, the encoder inventory and the
|
||||
/// update check. Started after the window shows, stopped when the app exits. The last archive is reopened at start and
|
||||
/// rescanned every <see cref="ScanInterval"/> (BRIEF §7: polling, no file-system watchers). The render node runs while
|
||||
/// the user offers this PC (<see cref="AppSettings.RenderNodeOptIn"/>) and a server session is open.
|
||||
/// </summary>
|
||||
public sealed class AppServices : IAsyncDisposable
|
||||
{
|
||||
@@ -32,6 +41,9 @@ public sealed class AppServices : IAsyncDisposable
|
||||
private readonly CancellationTokenSource _stopping = new();
|
||||
private readonly Dictionary<Guid, Guid> _scans = [];
|
||||
private Task _polling = Task.CompletedTask;
|
||||
private ServerSession _server = null!;
|
||||
private Task _nodeSwitch = Task.CompletedTask;
|
||||
private (RenderNodeClient Node, TitanoClient Client, Uri Address, string Token)? _node;
|
||||
|
||||
public AppServices(AppEnvironment environment, AppSettings settings, Action saveSettings, IAppDialogs dialogs)
|
||||
{
|
||||
@@ -57,7 +69,20 @@ public sealed class AppServices : IAsyncDisposable
|
||||
/// <summary>The archive engine; null until the index is open.</summary>
|
||||
public ArchiveService? Archives { get; private set; }
|
||||
|
||||
public ServerSession Server { get; 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();
|
||||
}
|
||||
}
|
||||
|
||||
public IEncoderInventory Encoders { get; set; }
|
||||
|
||||
@@ -86,7 +111,7 @@ public sealed class AppServices : IAsyncDisposable
|
||||
/// <summary>The job handlers of this PC's engine — the single registration list; a kind without a handler stays
|
||||
/// queued (it is never claimed here).</summary>
|
||||
public static IReadOnlyList<IJobHandler> CreateJobHandlers(AppServices services) =>
|
||||
services.Archives?.CreateJobHandlers() ?? [];
|
||||
[.. services.Archives?.CreateJobHandlers() ?? [], .. services.Processing?.CreateJobHandlers() ?? []];
|
||||
|
||||
/// <summary>Queues a scan of <paramref name="archive"/> unless one is already waiting or running.</summary>
|
||||
public async Task QueueScanAsync(Archive archive, bool full, CancellationToken ct = default)
|
||||
@@ -105,7 +130,14 @@ public sealed class AppServices : IAsyncDisposable
|
||||
}
|
||||
}
|
||||
|
||||
public void SaveSettings() => SaveSettingsAction();
|
||||
public void SaveSettings()
|
||||
{
|
||||
SaveSettingsAction();
|
||||
UpdateRenderNode();
|
||||
}
|
||||
|
||||
/// <summary>Starts or stops the render node to match the opt-in and the server session (BRIEF §14.7); switches run in order.</summary>
|
||||
public void UpdateRenderNode() => _nodeSwitch = SwitchRenderNodeAsync(_nodeSwitch);
|
||||
|
||||
/// <summary>Opens (or creates) the index at <paramref name="databasePath"/>, migrates it and starts the job engine.</summary>
|
||||
public async Task OpenAsync(string databasePath, CancellationToken ct = default)
|
||||
@@ -139,6 +171,9 @@ public sealed class AppServices : IAsyncDisposable
|
||||
await _stopping.CancelAsync();
|
||||
await _polling;
|
||||
_stopping.Dispose();
|
||||
_server.PropertyChanged -= OnServerChanged;
|
||||
await _nodeSwitch;
|
||||
await StopRenderNodeAsync();
|
||||
(Jobs as IDisposable)?.Dispose();
|
||||
if (Engine is not null) await Engine.DisposeAsync();
|
||||
Thumbnails?.Dispose();
|
||||
@@ -171,7 +206,9 @@ public sealed class AppServices : IAsyncDisposable
|
||||
|
||||
private async Task StartJobsAsync(TitanoDatabase database, bool startEngine, CancellationToken ct)
|
||||
{
|
||||
Archives = new ArchiveService(database, new ArchiveServiceOptions { Decoder = DecoderFactory.CreateClient(new WicDecoderProvider()) });
|
||||
var archive = new ArchiveServiceOptions { Decoder = DecoderFactory.CreateClient(new WicDecoderProvider()) };
|
||||
Archives = new ArchiveService(database, archive);
|
||||
Processing = new ProcessingService(database, ProcessingOptions(archive));
|
||||
var handlers = CreateJobHandlers(this);
|
||||
var kinds = handlers.Select(h => h.Kind).Distinct().ToArray();
|
||||
Engine = new JobEngine(database, handlers, new JobEngineOptions { WorkerId = WorkerId, Kinds = kinds });
|
||||
@@ -180,5 +217,65 @@ public sealed class AppServices : IAsyncDisposable
|
||||
if (started) await Engine.StartAsync(ct);
|
||||
Jobs = new EngineJobQueue(database, Engine, started);
|
||||
Log.Info("jobs", started ? $"Job engine started with {Engine.Options.Workers} worker(s) for {string.Join(", ", kinds)}" : "No job handlers registered; jobs stay queued");
|
||||
UpdateRenderNode();
|
||||
}
|
||||
|
||||
/// <summary>Media Foundation (hardware first, software H.264) then managed encoders; frames spill into the cache folder
|
||||
/// within the memory budget of Settings › Rendering (Auto: a quarter of the installed memory).</summary>
|
||||
private ProcessingOptions ProcessingOptions(ArchiveServiceOptions archive) => new()
|
||||
{
|
||||
Archive = archive,
|
||||
Encoders = [new MediaFoundationEncoderProvider(), new ManagedEncoderProvider()],
|
||||
Host = EncoderHost.Client,
|
||||
ProducedBy = System.Environment.MachineName,
|
||||
FrameWindow = new FrameWindowOptions
|
||||
{
|
||||
MemoryBudget = Settings.MemoryBudgetGb is { } gb ? gb * (1L << 30) : GC.GetGCMemoryInfo().TotalAvailableMemoryBytes / 4,
|
||||
SpillDirectory = CacheStore.Folder(Environment, Settings),
|
||||
},
|
||||
};
|
||||
|
||||
private void OnServerChanged(object? sender, PropertyChangedEventArgs e) => UpdateRenderNode();
|
||||
|
||||
/// <summary>Waits for the previous switch, then runs the node for the current session, or stops it.</summary>
|
||||
private async Task SwitchRenderNodeAsync(Task previous)
|
||||
{
|
||||
await previous;
|
||||
var flow = _server.Flow;
|
||||
(ProcessingService Engine, Uri Address, string Token)? wanted =
|
||||
Settings.RenderNodeOptIn && _server.IsConnected && Processing is { } processing && flow.Address is { } address && flow.Token is { } token
|
||||
? (processing, address, token)
|
||||
: null;
|
||||
if (_node is { } running && wanted is { } same && running.Address == same.Address && running.Token == same.Token) return;
|
||||
await StopRenderNodeAsync();
|
||||
if (wanted is not { } start) return;
|
||||
try
|
||||
{
|
||||
var cache = CacheStore.Folder(Environment, Settings);
|
||||
CacheStore.Prepare(cache);
|
||||
var client = new TitanoClient(start.Address, new CertificatePinning(_server.Fingerprints), $"Titano ({System.Environment.MachineName})") { Token = start.Token };
|
||||
var node = new RenderNodeClient(client, start.Engine.Options, new RenderNodeOptions
|
||||
{
|
||||
ClientName = System.Environment.MachineName,
|
||||
CacheDirectory = Path.Combine(cache, "render-node"),
|
||||
CacheLimitBytes = Settings.CacheLimitGb * (1L << 30),
|
||||
});
|
||||
node.Start();
|
||||
_node = (node, client, start.Address, start.Token);
|
||||
Log.Info("render-node", $"Offering this PC as render node to {start.Address}");
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
Log.Warning("render-node", "The render node could not start", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task StopRenderNodeAsync()
|
||||
{
|
||||
if (_node is not { } running) return;
|
||||
_node = null;
|
||||
await running.Node.DisposeAsync();
|
||||
running.Client.Dispose();
|
||||
Log.Info("render-node", "Render node stopped");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user