Server reale e veste Docker (M5): composizione, collegamento, vista remota, salute, immagine e modello Unraid

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
Claude committed 2026-10-13 20:17:22 +00:00
1 parent 0515776b04
commit 550c220cd4
35 files changed
+6124 -10

No files matched your search

+14
View File
@@ -123,6 +123,20 @@ public static class Keyboard
public static Task KeyUpAsync(ICdpSession cdp, KeyStroke key, CancellationToken ct = default) =>
cdp.SendAsync("Input.dispatchKeyEvent", Build("keyUp", key), ct);
/// <summary>
/// Inserts text without key codes (<c>Input.dispatchKeyEvent</c> of type «char»): composed characters of the
/// remote view, such as «è» typed with a dead key. Never logged.
/// </summary>
public static Task CharAsync(ICdpSession cdp, string text, CancellationToken ct = default)
{
ArgumentNullException.ThrowIfNull(cdp);
ArgumentException.ThrowIfNullOrEmpty(text);
return cdp.SendAsync(
"Input.dispatchKeyEvent",
new JsonObject { ["type"] = "char", ["text"] = text, ["unmodifiedText"] = text },
ct);
}
/// <summary>Presses and releases one key, with a human hold time when a clock is given.</summary>
public static async Task PressAsync(ICdpSession cdp, KeyStroke key, IClock? clock = null, IRandomSource? random = null, CancellationToken ct = default)
{
@@ -27,12 +27,14 @@ public sealed class ChromiumProcessHost : IBrowserHost
private readonly IBrowserLog _log;
private readonly ReconnectingCdpSession _session = new();
private readonly SemaphoreSlim _connectLock = new(1, 1);
private readonly SemaphoreSlim _restartLock = new(1, 1);
// Not disposed on purpose: background reconnection may still read its token; it holds no timer or handle.
private readonly CancellationTokenSource _lifetime = new();
private SupervisedProcess? _supervisor;
private CdpClient? _client;
private BrowserOptions? _options;
private int _stopping;
private int _restarting;
public ChromiumProcessHost(ChromiumLaunchOptions? launch = null, IClock? clock = null, IBrowserLog? log = null)
{
@@ -62,6 +64,9 @@ public sealed class ChromiumProcessHost : IBrowserHost
public event EventHandler<SupervisedState>? StateChanged;
/// <summary>True while a page connection is attached (commands can be sent).</summary>
public bool IsAttached => _session.IsAttached;
public async Task StartAsync(BrowserOptions options, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(options);
@@ -70,6 +75,47 @@ public sealed class ChromiumProcessHost : IBrowserHost
throw new InvalidOperationException("Il browser è già avviato.");
}
await StartCoreAsync(options, ct).ConfigureAwait(false);
}
/// <summary>
/// Stops the browser (SIGINT, so the profile is written to disk), runs <paramref name="whileStopped"/> — «Scollega»
/// deletes the profile there — and starts it again with the same options. <see cref="Cdp"/> stays the same object:
/// subscriptions survive and <see cref="ICdpConnectionEvents.Reconnected"/> is raised once the new browser is
/// connected.
/// </summary>
public async Task RestartAsync(Action? whileStopped, CancellationToken ct)
{
var options = _options ?? throw new InvalidOperationException("Il browser non è mai partito.");
if (Volatile.Read(ref _stopping) == 1)
{
throw new InvalidOperationException("Il browser si sta fermando.");
}
await _restartLock.WaitAsync(ct).ConfigureAwait(false);
Interlocked.Exchange(ref _restarting, 1);
try
{
if (_supervisor is { } current)
{
await DropClientAsync().ConfigureAwait(false);
await current.StopAsync(StopGrace).ConfigureAwait(false);
_supervisor = null;
_log.Info("Chromium è fermo per un nuovo avvio.");
}
whileStopped?.Invoke();
await StartCoreAsync(options, ct).ConfigureAwait(false);
}
finally
{
Interlocked.Exchange(ref _restarting, 0);
_restartLock.Release();
}
}
private async Task StartCoreAsync(BrowserOptions options, CancellationToken ct)
{
var executable = ChromiumCommandLine.FindExecutable(options.ExecutablePath)
?? throw new FileNotFoundException("Chromium non è installato o non si trova (si può indicare con FEBE_CHROMIUM).");
var arguments = ChromiumCommandLine.Build(options, _launch);
@@ -131,10 +177,20 @@ public sealed class ChromiumProcessHost : IBrowserHost
}
await _lifetime.CancelAsync().ConfigureAwait(false);
await DropClientAsync().ConfigureAwait(false);
if (_supervisor is { } supervisor)
// A restart in progress («Scollega») finishes first, so that no browser is left running behind the stop.
await _restartLock.WaitAsync().ConfigureAwait(false);
try
{
await supervisor.StopAsync(StopGrace).ConfigureAwait(false);
await DropClientAsync().ConfigureAwait(false);
if (_supervisor is { } supervisor)
{
await supervisor.StopAsync(StopGrace).ConfigureAwait(false);
}
}
finally
{
_restartLock.Release();
}
_log.Info("Chromium è fermo.");
@@ -248,7 +304,7 @@ public sealed class ChromiumProcessHost : IBrowserHost
private void OnClientDisconnected(object? sender, Exception? reason)
{
if (Volatile.Read(ref _stopping) == 1 || !ReferenceEquals(sender, Volatile.Read(ref _client)))
if (Volatile.Read(ref _stopping) == 1 || Volatile.Read(ref _restarting) == 1 || !ReferenceEquals(sender, Volatile.Read(ref _client)))
{
return;
}
@@ -167,6 +167,13 @@ public sealed class RemoteViewSession : IAsyncDisposable
return e.Down ? Keyboard.KeyDownAsync(_cdp, e.Key, ct) : Keyboard.KeyUpAsync(_cdp, e.Key, ct);
}
/// <summary>Inserts composed text (a «char» event without key codes). Never logged.</summary>
public Task CharAsync(string text, CancellationToken ct = default)
{
EnsureActive();
return Keyboard.CharAsync(_cdp, text, ct);
}
/// <summary>Opens an address chosen by Alberto (e.g. the login page from the connection page).</summary>
public Task<NavigationResult> NavigateAsync(string url, CancellationToken ct = default)
{