Hero: varianten op de achtergrond knippen; upload en opslaan antwoorden direct
Opslaan van 8 foto's met gewijzigde focus duurde 40 s op een snelle pc en ruim 3 minuten op de Pi (proxy-timeout 60 s gaf 504, hero niet bewaard). Upload bewaart het origineel en antwoordt; HeroRenderService knipt de set per afbeelding en schakelt over zodra de set compleet is (oude set blijft ondertussen live; afbeelding zonder set wordt niet gepubliceerd). Opslaan legt alleen de focus vast. Admin toont 'wordt verwerkt' en ververst zelf; Seed-Hero wacht tot alles klaar is. Metingen: HeroSaveTimingTests. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace SlpModularCms.Modules.Hero.Services;
|
||||
|
||||
/// <summary>Wakes the background renderer after an upload or a save. A singleton shared by the request and the worker.</summary>
|
||||
public sealed class HeroRenderSignal
|
||||
{
|
||||
private readonly SemaphoreSlim _signal = new(0, 1);
|
||||
|
||||
public void Notify()
|
||||
{
|
||||
try
|
||||
{
|
||||
_signal.Release();
|
||||
}
|
||||
catch (SemaphoreFullException)
|
||||
{
|
||||
// Already signalled; the worker will look at everything that is pending.
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Waits for a signal or for the timeout, whichever comes first.</summary>
|
||||
public Task<bool> WaitAsync(TimeSpan timeout, CancellationToken cancellationToken) => _signal.WaitAsync(timeout, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cuts the variants of images that have none yet (upload) or whose set no longer matches their focus (a saved focus
|
||||
/// change), one image at a time, so an upload or a save never waits for the slow part. An image goes live (or switches to
|
||||
/// its new set) the moment its set is complete; until then the old set, or nothing, is published. Looks again when woken
|
||||
/// and at least every minute, and at start-up, so work interrupted by a restart is picked up.
|
||||
/// </summary>
|
||||
public sealed class HeroRenderService(IServiceScopeFactory scopes, HeroRenderSignal signal, ILogger<HeroRenderService> logger) : BackgroundService
|
||||
{
|
||||
private const int MaxAttempts = 3;
|
||||
private readonly Dictionary<Guid, int> _failures = [];
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Let the start-up (migrations, health check) finish first.
|
||||
await Task.Delay(TimeSpan.FromSeconds(10), stoppingToken);
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
await RunPassAsync(stoppingToken);
|
||||
await signal.WaitAsync(TimeSpan.FromMinutes(1), stoppingToken);
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// Shutting down.
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RunPassAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
var skip = _failures.Where(f => f.Value >= MaxAttempts).Select(f => f.Key).ToHashSet();
|
||||
Guid? handled = null;
|
||||
try
|
||||
{
|
||||
using var scope = scopes.CreateScope();
|
||||
handled = await scope.ServiceProvider.GetRequiredService<IHeroImageService>().RenderNextPendingAsync(skip, stoppingToken);
|
||||
}
|
||||
catch (HeroImageRenderException failure)
|
||||
{
|
||||
var attempts = _failures[failure.ImageId] = _failures.GetValueOrDefault(failure.ImageId) + 1;
|
||||
logger.LogError(failure.InnerException, "Cutting the variants of hero image {ImageId} failed (attempt {Attempt} of {Max}).", failure.ImageId, attempts, MaxAttempts);
|
||||
await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
|
||||
continue;
|
||||
}
|
||||
catch (Exception exception) when (exception is not OperationCanceledException)
|
||||
{
|
||||
logger.LogError(exception, "Looking for hero images to cut failed; trying again at the next wake-up.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (handled is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_failures.Remove(handled.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user