Hero: grote foto's in de achtergrond robuust en sneller, voortgang en opnieuw proberen; admin zonder inline script
- Een decode voor de hele set, per breedte eenmaal geschaald voor WebP en AVIF (6000x4000: 21,1 s -> 6,4 s per set) - Eigen limiet per set (Media:RenderSetTimeoutSeconds 600), harde limiet rond een foto, de renderer stopt nooit door een uitzondering, een mislukte foto wacht tot de volgende ronde en houdt de rij niet op - Logging per foto (start, per variant, einde, fout); voortgang per variant in de admin; nieuwe uitsnede na focuswijziging zichtbaar (oude afbeelding blijft, shimmer); knop Opnieuw proberen - Thema-script als /theme-init.js (CSP script-src 'self'), test en smoke-controle op inline scripts Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SlpModularCms.Core.Media;
|
||||
|
||||
namespace SlpModularCms.Modules.Hero.Services;
|
||||
|
||||
@@ -32,6 +34,14 @@ public sealed class HeroRenderSignal
|
||||
|
||||
public IReadOnlySet<Guid> GivenUp() => _failures.Where(f => f.Value >= MaxAttempts).Select(f => f.Key).ToHashSet();
|
||||
|
||||
private readonly System.Collections.Concurrent.ConcurrentDictionary<Guid, (int Done, int Total)> _progress = new();
|
||||
|
||||
public void SetProgress(Guid imageId, int done, int total) => _progress[imageId] = (done, total);
|
||||
|
||||
public (int Done, int Total)? GetProgress(Guid imageId) => _progress.TryGetValue(imageId, out var p) ? p : null;
|
||||
|
||||
public void ClearProgress(Guid imageId) => _progress.TryRemove(imageId, out _);
|
||||
|
||||
/// <summary>Forgets the failures of an image (after a success, a retry or a new upload of the same photo).</summary>
|
||||
public void Reset(Guid imageId) => _failures.TryRemove(imageId, out _);
|
||||
|
||||
@@ -45,18 +55,28 @@ public sealed class HeroRenderSignal
|
||||
/// 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
|
||||
public sealed class HeroRenderService(IServiceScopeFactory scopes, HeroRenderSignal signal, IOptions<MediaOptions> media, ILogger<HeroRenderService> logger) : BackgroundService
|
||||
{
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Let the start-up (migrations, health check) finish first.
|
||||
// Let the start-up (migrations, health check) finish first; whatever was still pending is picked up then.
|
||||
await Task.Delay(TimeSpan.FromSeconds(10), stoppingToken);
|
||||
logger.LogInformation("Hero image renderer started.");
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
await RunPassAsync(stoppingToken);
|
||||
try
|
||||
{
|
||||
await RunPassAsync(stoppingToken);
|
||||
}
|
||||
catch (Exception exception) when (exception is not OperationCanceledException)
|
||||
{
|
||||
// Whatever happens, the renderer does not stop: it logs and looks again at the next wake-up.
|
||||
logger.LogError(exception, "A round of the hero image renderer failed; it goes on.");
|
||||
}
|
||||
|
||||
await signal.WaitAsync(TimeSpan.FromMinutes(1), stoppingToken);
|
||||
}
|
||||
}
|
||||
@@ -68,20 +88,32 @@ public sealed class HeroRenderService(IServiceScopeFactory scopes, HeroRenderSig
|
||||
|
||||
private async Task RunPassAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
// An image that failed in this pass waits for the next pass (a minute later or the next wake-up): one difficult photo
|
||||
// must not hold up the others, and it is not retried straight away.
|
||||
var failedInThisPass = new HashSet<Guid>();
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
var skip = signal.GivenUp();
|
||||
var skip = signal.GivenUp().Concat(failedInThisPass).ToHashSet();
|
||||
Guid? handled = null;
|
||||
try
|
||||
{
|
||||
using var scope = scopes.CreateScope();
|
||||
handled = await scope.ServiceProvider.GetRequiredService<IHeroImageService>().RenderNextPendingAsync(skip, stoppingToken);
|
||||
// A hard limit around one image, a little above the time the image library itself is allowed: nothing can
|
||||
// keep the renderer waiting for ever.
|
||||
using var limit = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken);
|
||||
limit.CancelAfter(TimeSpan.FromSeconds(Math.Max(30, media.Value.RenderSetTimeoutSeconds) + 60));
|
||||
handled = await scope.ServiceProvider.GetRequiredService<IHeroImageService>().RenderNextPendingAsync(skip, limit.Token);
|
||||
}
|
||||
catch (OperationCanceledException) when (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
logger.LogError("Cutting a hero image took longer than the hard limit; the round is stopped and tried again at the next wake-up.");
|
||||
return;
|
||||
}
|
||||
catch (HeroImageRenderException failure)
|
||||
{
|
||||
failedInThisPass.Add(failure.ImageId);
|
||||
var attempts = signal.RecordFailure(failure.ImageId);
|
||||
logger.LogError(failure.InnerException, "Cutting the variants of hero image {ImageId} failed (attempt {Attempt} of {Max}).", failure.ImageId, attempts, HeroRenderSignal.MaxAttempts);
|
||||
await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
|
||||
continue;
|
||||
}
|
||||
catch (Exception exception) when (exception is not OperationCanceledException)
|
||||
|
||||
Reference in New Issue
Block a user