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;
/// Wakes the background renderer after an upload or a save. A singleton shared by the request and the worker.
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.
}
}
/// How often the renderer may try an image before it gives up on it (until a retry or a restart).
public const int MaxAttempts = 3;
private readonly System.Collections.Concurrent.ConcurrentDictionary _failures = new();
public int RecordFailure(Guid imageId) => _failures.AddOrUpdate(imageId, 1, (_, n) => n + 1);
public bool IsGivenUp(Guid imageId) => _failures.TryGetValue(imageId, out var n) && n >= MaxAttempts;
public IReadOnlySet GivenUp() => _failures.Where(f => f.Value >= MaxAttempts).Select(f => f.Key).ToHashSet();
private readonly System.Collections.Concurrent.ConcurrentDictionary _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 _);
/// Forgets the failures of an image (after a success, a retry or a new upload of the same photo).
public void Reset(Guid imageId) => _failures.TryRemove(imageId, out _);
/// Waits for a signal or for the timeout, whichever comes first.
public Task WaitAsync(TimeSpan timeout, CancellationToken cancellationToken) => _signal.WaitAsync(timeout, cancellationToken);
}
///
/// 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.
///
public sealed class HeroRenderService(IServiceScopeFactory scopes, HeroRenderSignal signal, IOptions media, ILogger logger) : BackgroundService
{
private bool _loggedFirstPass;
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
// 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)
{
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);
}
}
catch (OperationCanceledException)
{
// Shutting down.
}
}
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();
try
{
using var scope = scopes.CreateScope();
var (total, needRender, needPreview) = await scope.ServiceProvider.GetRequiredService().GetRenderBacklogAsync(stoppingToken);
if (needRender + needPreview > 0 || !_loggedFirstPass)
{
logger.LogInformation("Hero image renderer: {Total} images, {NeedRender} waiting for their variants, {NeedPreview} lacking the full-frame preview.", total, needRender, needPreview);
}
_loggedFirstPass = true;
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
logger.LogWarning(exception, "Counting the hero images that wait for the renderer failed.");
}
while (!stoppingToken.IsCancellationRequested)
{
var skip = signal.GivenUp().Concat(failedInThisPass).ToHashSet();
Guid? handled = null;
try
{
using var scope = scopes.CreateScope();
// 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().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);
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;
}
signal.Reset(handled.Value);
}
}
}