Hero: zelftest en mapcontrole van de media draaien op de achtergrond, zodat de host niet later gaat luisteren

De hosted services starten voor de webserver; de zelftest van Magick (laden van de native bibliotheek en een testcodering) kostte op de Pi ongeveer 9 s voordat de host luisterde.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 17:20:59 +01:00
co-authored by Claude Sonnet 5.5
parent bd283d629d
commit 01703ccddf
5 changed files with 102 additions and 34 deletions
@@ -56,36 +56,68 @@ public sealed class MediaDirectoryCheck(
Microsoft.Extensions.Options.IOptions<MediaOptions> options,
ILogger<MediaDirectoryCheck> logger) : IHostedService
{
public async Task StartAsync(CancellationToken cancellationToken)
/// <summary>The check in the background; completes when the status is known (tests await it).</summary>
public Task Completion { get; private set; } = Task.CompletedTask;
private readonly CancellationTokenSource _stopping = new();
public Task StartAsync(CancellationToken cancellationToken)
{
foreach (var registration in registrations)
{
areas.Add(registration.Area);
}
if (string.IsNullOrWhiteSpace(options.Value.Directory))
// Not awaited: a hosted service delays the moment the host starts listening, and a disk that is slow to answer must
// never keep the website from coming up. Until it is done, uploads are simply not available yet.
Completion = Task.Run(() => CheckAsync(_stopping.Token), CancellationToken.None);
return Task.CompletedTask;
}
private async Task CheckAsync(CancellationToken cancellationToken)
{
try
{
if (string.IsNullOrWhiteSpace(options.Value.Directory))
{
status.SetDirectory(false);
logger.LogWarning("Media:Directory is not configured: image uploads are unavailable on this host.");
return;
}
var writable = await store.ProbeAsync(cancellationToken);
status.SetDirectory(writable);
if (!writable)
{
logger.LogError("The media directory is missing or not writable: image uploads are unavailable until it is fixed.");
return;
}
var total = await store.GetTotalBytesAsync(cancellationToken);
if (options.Value.MaxTotalBytes > 0 && total > options.Value.MaxTotalBytes * 8 / 10)
{
logger.LogError("The media directory is over 80 percent of its safety limit ({Bytes} bytes).", total);
}
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
status.SetDirectory(false);
logger.LogWarning("Media:Directory is not configured: image uploads are unavailable on this host.");
return;
}
var writable = await store.ProbeAsync(cancellationToken);
status.SetDirectory(writable);
if (!writable)
{
logger.LogError("The media directory is missing or not writable: image uploads are unavailable until it is fixed.");
return;
}
var total = await store.GetTotalBytesAsync(cancellationToken);
if (options.Value.MaxTotalBytes > 0 && total > options.Value.MaxTotalBytes * 8 / 10)
{
logger.LogError("The media directory is over 80 percent of its safety limit ({Bytes} bytes).", total);
logger.LogError(exception, "The media directory check failed: image uploads are unavailable on this host.");
}
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
public async Task StopAsync(CancellationToken cancellationToken)
{
await _stopping.CancelAsync();
try
{
await Completion.WaitAsync(cancellationToken);
}
catch (OperationCanceledException)
{
// Shutting down.
}
}
}
/// <summary>
@@ -95,18 +127,31 @@ public sealed class MediaDirectoryCheck(
/// </summary>
public sealed class ImageProcessorSelfTest(IServiceProvider services, MediaStatus status, ILogger<ImageProcessorSelfTest> logger) : IHostedService
{
public async Task StartAsync(CancellationToken cancellationToken)
{
var processor = services.GetService<IImageProcessor>();
if (processor is null)
{
status.SetProcessor(false, new HashSet<ImageFormat>(), new HashSet<ImageFormat>());
logger.LogWarning("No image processor is registered: image uploads are unavailable.");
return;
}
/// <summary>The self-test in the background; completes when the status is known (tests await it).</summary>
public Task Completion { get; private set; } = Task.CompletedTask;
private readonly CancellationTokenSource _stopping = new();
public Task StartAsync(CancellationToken cancellationToken)
{
// Not awaited: loading the native library and encoding a test image can take seconds on a small machine, and a
// hosted service delays the moment the host starts listening (the health check of a deploy waits for it).
Completion = Task.Run(() => TestAsync(_stopping.Token), CancellationToken.None);
return Task.CompletedTask;
}
private async Task TestAsync(CancellationToken cancellationToken)
{
try
{
var processor = services.GetService<IImageProcessor>();
if (processor is null)
{
status.SetProcessor(false, new HashSet<ImageFormat>(), new HashSet<ImageFormat>());
logger.LogWarning("No image processor is registered: image uploads are unavailable.");
return;
}
await processor.SelfTestAsync(cancellationToken);
status.SetProcessor(true, processor.SupportedInput, processor.SupportedOutput);
}
@@ -117,5 +162,16 @@ public sealed class ImageProcessorSelfTest(IServiceProvider services, MediaStatu
}
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
public async Task StopAsync(CancellationToken cancellationToken)
{
await _stopping.CancelAsync();
try
{
await Completion.WaitAsync(cancellationToken);
}
catch (OperationCanceledException)
{
// Shutting down.
}
}
}