152 lines
6.9 KiB
C#
152 lines
6.9 KiB
C#
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;
|
|
|
|
/// <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>How often the renderer may try an image before it gives up on it (until a retry or a restart).</summary>
|
|
public const int MaxAttempts = 3;
|
|
|
|
private readonly System.Collections.Concurrent.ConcurrentDictionary<Guid, int> _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<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 _);
|
|
|
|
/// <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, IOptions<MediaOptions> media, ILogger<HeroRenderService> 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<Guid>();
|
|
try
|
|
{
|
|
using var scope = scopes.CreateScope();
|
|
var (total, needRender, needPreview) = await scope.ServiceProvider.GetRequiredService<IHeroImageService>().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<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);
|
|
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);
|
|
}
|
|
}
|
|
}
|