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:
2026-11-16 22:23:15 +01:00
co-authored by Claude Sonnet 5.5
parent 9d647bb049
commit 1b542dee98
22 changed files with 567 additions and 54 deletions
@@ -38,8 +38,11 @@ public sealed class MagickImageProcessor : IImageProcessor
public Task<RenderedImage> RenderAsync(ReadOnlyMemory<byte> data, ImageFormat declared, VariantSpec variant, CancellationToken cancellationToken)
=> RunAsync(() => Render(data, declared, variant), cancellationToken);
public Task<IReadOnlyList<RenderedImage>> RenderSetAsync(ReadOnlyMemory<byte> data, ImageFormat declared, IReadOnlyList<VariantSpec> variants, Action<int, int>? progress, CancellationToken cancellationToken)
=> RunAsync(() => RenderSet(data, declared, variants, progress, cancellationToken), cancellationToken, _options.RenderSetTimeoutSeconds);
public Task<byte[]?> StripMetadataAsync(ReadOnlyMemory<byte> data, ImageFormat declared, CancellationToken cancellationToken)
=> RunAsync(() => StripMetadata(data, declared), cancellationToken);
=> RunAsync(() => StripMetadata(data, declared), cancellationToken, _options.StripTimeoutSeconds);
public async Task SelfTestAsync(CancellationToken cancellationToken)
{
@@ -75,12 +78,12 @@ public sealed class MagickImageProcessor : IImageProcessor
// ----- work done on a pool thread with a time limit (the native limit below stops a runaway decode) ------------
private async Task<T> RunAsync<T>(Func<T> work, CancellationToken cancellationToken)
private async Task<T> RunAsync<T>(Func<T> work, CancellationToken cancellationToken, int? timeoutSeconds = null)
{
try
{
return await Task.Run(work, cancellationToken)
.WaitAsync(TimeSpan.FromSeconds(Math.Max(1, _options.DecodeTimeoutSeconds)), cancellationToken);
.WaitAsync(TimeSpan.FromSeconds(Math.Max(1, timeoutSeconds ?? _options.DecodeTimeoutSeconds)), cancellationToken);
}
catch (TimeoutException)
{
@@ -133,33 +136,115 @@ public sealed class MagickImageProcessor : IImageProcessor
private RenderedImage Render(ReadOnlyMemory<byte> data, ImageFormat declared, VariantSpec variant)
{
var settings = new MagickReadSettings { Format = ToMagick(declared), FrameIndex = 0, FrameCount = 1 };
using var image = ReadStrict(data, settings);
using var image = DecodeOriented(data, declared);
return Cut(image, variant);
}
/// <summary>
/// One decode for the whole set, and as little repeated work as possible: per crop the widths are cut from large to
/// small, each from the previous (smaller input than the full picture), and a width is resized once for all of its
/// formats (WebP and AVIF are encoded from the same pixels).
/// </summary>
private IReadOnlyList<RenderedImage> RenderSet(ReadOnlyMemory<byte> data, ImageFormat declared, IReadOnlyList<VariantSpec> variants, Action<int, int>? progress, CancellationToken cancellationToken)
{
using var image = DecodeOriented(data, declared);
var results = new RenderedImage?[variants.Count];
var done = 0;
foreach (var group in variants.Select((spec, index) => (spec, index)).GroupBy(x => (x.spec.Ratio.Name, x.spec.Crop)))
{
var crop = group.Key.Crop;
if (crop.X < 0 || crop.Y < 0 || crop.Width < 1 || crop.Height < 1
|| crop.X + crop.Width > image.Width || crop.Y + crop.Height > image.Height)
{
throw new ImageRejectedException(ImageRejection.Corrupt, "The planned crop does not fit the decoded image.");
}
// A copy of the decoded pixels is a plain memory copy, cheap next to decoding or encoding.
IMagickImage<byte> current = image.Clone();
try
{
current.Crop(new MagickGeometry(crop.X, crop.Y, (uint)crop.Width, (uint)crop.Height));
current.ResetPage();
ToSrgb(current);
current.Strip();
foreach (var width in group.GroupBy(x => x.spec.Width).OrderByDescending(g => g.Key))
{
// A cancelled or timed-out job stops at the next width; the native library itself cannot be interrupted.
cancellationToken.ThrowIfCancellationRequested();
var first = width.First().spec;
if (current.Width != (uint)first.Width || current.Height != (uint)first.Height)
{
current.Resize(new MagickGeometry((uint)first.Width, (uint)first.Height) { IgnoreAspectRatio = true });
}
foreach (var (spec, index) in width)
{
results[index] = Encode(current, spec);
progress?.Invoke(++done, variants.Count);
}
}
}
finally
{
current.Dispose();
}
}
return results.Select(r => r!).ToList();
}
/// <summary>Writes already cut and resized pixels in the format and quality of the variant.</summary>
private static RenderedImage Encode(IMagickImage<byte> pixels, VariantSpec variant)
{
pixels.Quality = (uint)variant.Quality;
if (variant.Format == ImageFormat.Avif)
{
pixels.Settings.SetDefine(MagickFormat.Avif, "speed", "6");
}
using var stream = new MemoryStream();
pixels.Write(stream, ToMagick(variant.Format));
return new RenderedImage(stream.ToArray(), (int)pixels.Width, (int)pixels.Height, variant.Format);
}
private static MagickImage DecodeOriented(ReadOnlyMemory<byte> data, ImageFormat declared)
{
var settings = new MagickReadSettings { Format = ToMagick(declared), FrameIndex = 0, FrameCount = 1 };
var image = ReadStrict(data, settings);
image.AutoOrient();
return image;
}
/// <summary>Crops a copy of the decoded picture, resizes and encodes it.</summary>
private static RenderedImage Cut(IMagickImage<byte> source, VariantSpec variant)
{
var crop = variant.Crop;
if (crop.X < 0 || crop.Y < 0 || crop.Width < 1 || crop.Height < 1
|| crop.X + crop.Width > image.Width || crop.Y + crop.Height > image.Height)
|| crop.X + crop.Width > source.Width || crop.Y + crop.Height > source.Height)
{
throw new ImageRejectedException(ImageRejection.Corrupt, "The planned crop does not fit the decoded image.");
}
image.Crop(new MagickGeometry(crop.X, crop.Y, (uint)crop.Width, (uint)crop.Height));
image.ResetPage();
image.Resize(new MagickGeometry((uint)variant.Width, (uint)variant.Height) { IgnoreAspectRatio = true });
ToSrgb(image);
image.Strip();
image.Quality = (uint)variant.Quality;
// A copy of the decoded pixels is a plain memory copy, cheap next to decoding or encoding.
using var part = source.Clone();
part.Crop(new MagickGeometry(crop.X, crop.Y, (uint)crop.Width, (uint)crop.Height));
part.ResetPage();
part.Resize(new MagickGeometry((uint)variant.Width, (uint)variant.Height) { IgnoreAspectRatio = true });
ToSrgb(part);
part.Strip();
part.Quality = (uint)variant.Quality;
var format = ToMagick(variant.Format);
if (variant.Format == ImageFormat.Avif)
{
image.Settings.SetDefine(MagickFormat.Avif, "speed", "6");
part.Settings.SetDefine(MagickFormat.Avif, "speed", "6");
}
using var stream = new MemoryStream();
image.Write(stream, format);
return new RenderedImage(stream.ToArray(), (int)image.Width, (int)image.Height, variant.Format);
part.Write(stream, format);
return new RenderedImage(stream.ToArray(), (int)part.Width, (int)part.Height, variant.Format);
}
private static byte[]? StripMetadata(ReadOnlyMemory<byte> data, ImageFormat declared)
@@ -227,7 +312,7 @@ public sealed class MagickImageProcessor : IImageProcessor
return image;
}
private static void ToSrgb(MagickImage image)
private static void ToSrgb(IMagickImage<byte> image)
{
try
{
@@ -257,7 +342,8 @@ public sealed class MagickImageProcessor : IImageProcessor
ResourceLimits.Width = (ulong)Math.Max(1, options.MaxSide);
ResourceLimits.Height = (ulong)Math.Max(1, options.MaxSide);
ResourceLimits.Memory = 1UL * 1024 * 1024 * 1024;
ResourceLimits.Time = (ulong)Math.Max(1, options.DecodeTimeoutSeconds);
// The library's own clock runs per picture from its creation: it must not end a set that the job timeout allows.
ResourceLimits.Time = (ulong)Math.Max(1, Math.Max(options.DecodeTimeoutSeconds, Math.Max(options.StripTimeoutSeconds, options.RenderSetTimeoutSeconds)) + 30);
_limitsSet = true;
}
}
@@ -50,6 +50,15 @@ public sealed class MediaOptions
public int DecodeTimeoutSeconds { get; set; } = 20;
/// <summary>The time allowed to remove metadata from an upload (it decodes and writes the whole picture again).</summary>
public int StripTimeoutSeconds { get; set; } = 60;
/// <summary>
/// The time allowed to cut ALL variants of one photo in the background (one decode, then every variant). Generous on
/// purpose: a large photo on a Raspberry Pi needs minutes; a runaway job is stopped after this.
/// </summary>
public int RenderSetTimeoutSeconds { get; set; } = 600;
/// <summary>How long a free processing slot is awaited before the answer is "busy".</summary>
public int BusyWaitSeconds { get; set; } = 3;
@@ -81,6 +90,13 @@ public interface IImageProcessor
/// </summary>
Task<RenderedImage> RenderAsync(ReadOnlyMemory<byte> data, ImageFormat declared, VariantSpec variant, CancellationToken cancellationToken);
/// <summary>
/// Cuts all variants of a photo from ONE decode (the picture is decoded and oriented once, then every variant is
/// cropped, resized and encoded from it). <paramref name="progress"/> is called with (variants done, variants in total)
/// after every variant. Stops between variants when cancelled. The result has the order of <paramref name="variants"/>.
/// </summary>
Task<IReadOnlyList<RenderedImage>> RenderSetAsync(ReadOnlyMemory<byte> data, ImageFormat declared, IReadOnlyList<VariantSpec> variants, Action<int, int>? progress, CancellationToken cancellationToken);
/// <summary>
/// Removes metadata from an original that carries any, keeping the pixels as they are as far as the format
/// allows. Returns null when there is nothing to remove (the original is stored as received).
@@ -0,0 +1,84 @@
using System.Diagnostics;
using ImageMagick;
using SlpModularCms.Core.Media;
using Xunit.Abstractions;
namespace SlpModularCms.Modules.Hero.Tests;
/// <summary>
/// A measurement, not a check: where the time goes when a large photo (about 6000x4000, a few MB) is cut into its set.
/// Runs only when HERO_TIMING_BIG=1; otherwise it passes at once. Set MAGICK_THREAD_LIMIT=1 to look at a single core.
/// </summary>
public class HeroBigPhotoTimingTests(ITestOutputHelper output)
{
private static byte[] BigPhoto(int width, int height)
{
// Smooth gradients with a little noise: compresses like a photo (a few MB), unlike pure noise.
using var image = new MagickImage(MagickColors.SteelBlue, (uint)width, (uint)height);
image.AddNoise(NoiseType.Gaussian, 0.4);
using var clouds = new MagickImage("plasma:fractal", (uint)(width / 4), (uint)(height / 4));
clouds.Resize((uint)width, (uint)height);
image.Composite(clouds, CompositeOperator.Blend, "40");
image.Quality = 88;
using var stream = new MemoryStream();
image.Write(stream, MagickFormat.Jpeg);
return stream.ToArray();
}
[Fact]
public async Task MeasureASetOfABigPhoto()
{
if (Environment.GetEnvironmentVariable("HERO_TIMING_BIG") != "1")
{
return;
}
var width = int.Parse(Environment.GetEnvironmentVariable("HERO_TIMING_W") ?? "6000");
var height = int.Parse(Environment.GetEnvironmentVariable("HERO_TIMING_H") ?? "4000");
// Made before the processor exists: its resource limits (20 s) would stop the making of the fixture itself.
var bytes = BigPhoto(width, height);
using var rig = new HeroTestRig();
if (int.TryParse(Environment.GetEnvironmentVariable("HERO_TIMING_THREADS"), out var threads))
{
ResourceLimits.Thread = (ulong)threads;
output.WriteLine($"image library threads: {ResourceLimits.Thread}");
}
output.WriteLine($"photo {width}x{height}, {bytes.Length / 1024.0 / 1024.0:0.0} MB");
var watch = Stopwatch.StartNew();
var info = await rig.Processor.InspectAsync(bytes, ImageFormat.Jpeg, default);
output.WriteLine($"inspect: {watch.ElapsedMilliseconds} ms ({info.Width}x{info.Height})");
var ratios = new[] { Ratio.Wide, Ratio.Tall }
.Select(r => new RatioPlan(r, new FocusSetting(0.5, 0.5, 1).Normalize(), VariantPlanner.DefaultWidths(r)))
.ToList();
var specs = VariantPlanner.Plan(info.Width, info.Height, ratios, rig.Processor.SupportedOutput);
var byFormat = new Dictionary<ImageFormat, List<long>>();
var total = Stopwatch.StartNew();
foreach (var spec in specs)
{
watch.Restart();
var rendered = await rig.Processor.RenderAsync(bytes, ImageFormat.Jpeg, spec, default);
var ms = watch.ElapsedMilliseconds;
if (!byFormat.TryGetValue(spec.Format, out var list))
{
byFormat[spec.Format] = list = [];
}
list.Add(ms);
output.WriteLine($" {spec.Ratio.Name,-4} {spec.Width,5} {spec.Format,-5}: {ms,6} ms, {rendered.Bytes.Length / 1024} KB");
}
output.WriteLine($"per variant, one decode each: {specs.Count} variants in {total.ElapsedMilliseconds} ms");
foreach (var (format, list) in byFormat)
{
output.WriteLine($" {format}: {list.Count} variants, mean {list.Average():0} ms, total {list.Sum()} ms");
}
// The whole set from one decode.
watch.Restart();
var steps = 0;
var set = await rig.Processor.RenderSetAsync(bytes, ImageFormat.Jpeg, specs, (_, _) => steps++, default);
output.WriteLine($"whole set from one decode: {specs.Count} variants in {watch.ElapsedMilliseconds} ms ({steps} progress steps, {set.Sum(r => r.Bytes.Length) / 1024} KB)");
}
}
@@ -202,4 +202,66 @@ public class HeroFocusSaveTests
limits.MaxUploadBytes.Should().Be(rig.Media.MaxUploadBytes);
limits.MaxMegapixels.Should().Be((int)(rig.Media.MaxPixels / 1_000_000));
}
[Fact]
public async Task AnImageThatCannotBeCut_FailsWithItsId_AndTheOthersAreStillCut()
{
using var rig = new HeroTestRig();
var broken = await rig.UploadWithoutRendering(HeroTestRig.Photo(seed: 1));
var fine = await rig.UploadWithoutRendering(HeroTestRig.Photo(seed: 2));
// The original of the first photo is gone (a lost file): cutting it cannot work.
await rig.Store.DeleteAsync(rig.Db.HeroImages.Single(i => i.Id == Guid.Parse(broken.Id)).OriginalKey, default);
var failure = await Assert.ThrowsAsync<HeroImageRenderException>(() => rig.Images.RenderNextPendingAsync(new HashSet<Guid>(), default));
failure.ImageId.Should().Be(Guid.Parse(broken.Id));
var handled = await rig.Images.RenderNextPendingAsync(new HashSet<Guid> { failure.ImageId }, default);
handled.Should().Be(Guid.Parse(fine.Id));
HeroMapping.NeedsRender(rig.Db.HeroImages.Single(i => i.Id == Guid.Parse(fine.Id))).Should().BeFalse();
HeroMapping.NeedsRender(rig.Db.HeroImages.Single(i => i.Id == Guid.Parse(broken.Id))).Should().BeTrue();
}
[Fact]
public async Task ASetFromOneDecode_GivesTheSameSizesAndFormats_AsCuttingEveryVariantOnItsOwn_AndReportsEveryStep()
{
using var rig = new HeroTestRig();
var photo = HeroTestRig.Photo(2400, 1600);
var info = await rig.Processor.InspectAsync(photo, SlpModularCms.Core.Media.ImageFormat.Jpeg, default);
var ratios = new[] { SlpModularCms.Core.Media.Ratio.Wide, SlpModularCms.Core.Media.Ratio.Tall }
.Select(r => new SlpModularCms.Core.Media.RatioPlan(r, new SlpModularCms.Core.Media.FocusSetting(0.3, 0.6, 1.5).Normalize(), SlpModularCms.Core.Media.VariantPlanner.DefaultWidths(r)))
.ToList();
var specs = SlpModularCms.Core.Media.VariantPlanner.Plan(info.Width, info.Height, ratios, rig.Processor.SupportedOutput);
var steps = new List<(int Done, int Total)>();
var set = await rig.Processor.RenderSetAsync(photo, SlpModularCms.Core.Media.ImageFormat.Jpeg, specs, (done, total) => steps.Add((done, total)), default);
set.Should().HaveCount(specs.Count);
for (var i = 0; i < specs.Count; i++)
{
var single = await rig.Processor.RenderAsync(photo, SlpModularCms.Core.Media.ImageFormat.Jpeg, specs[i], default);
(set[i].Width, set[i].Height, set[i].Format).Should().Be((single.Width, single.Height, single.Format), $"variant {i}");
}
steps.Should().HaveCount(specs.Count);
steps.Select(x => x.Done).Should().BeInAscendingOrder();
steps.Last().Should().Be((specs.Count, specs.Count));
}
[Fact]
public async Task ACancelledSet_StopsBetweenVariants()
{
using var rig = new HeroTestRig();
var photo = HeroTestRig.Photo(2400, 1600);
var info = await rig.Processor.InspectAsync(photo, SlpModularCms.Core.Media.ImageFormat.Jpeg, default);
var ratios = new[] { new SlpModularCms.Core.Media.RatioPlan(SlpModularCms.Core.Media.Ratio.Wide, new SlpModularCms.Core.Media.FocusSetting(0.5, 0.5, 1).Normalize(), SlpModularCms.Core.Media.VariantPlanner.DefaultWidths(SlpModularCms.Core.Media.Ratio.Wide)) };
var specs = SlpModularCms.Core.Media.VariantPlanner.Plan(info.Width, info.Height, ratios, rig.Processor.SupportedOutput);
using var cts = new CancellationTokenSource();
var done = 0;
var act = async () => await rig.Processor.RenderSetAsync(photo, SlpModularCms.Core.Media.ImageFormat.Jpeg, specs, (_, _) => { done++; cts.Cancel(); }, cts.Token);
await act.Should().ThrowAsync<OperationCanceledException>();
done.Should().BeLessThan(specs.Count);
}
}
@@ -25,7 +25,9 @@ public record HeroImageDto(
string Src,
IReadOnlyList<HeroVariantDto> Variants,
bool Processing = false,
bool Failed = false);
bool Failed = false,
int? RenderDone = null,
int? RenderTotal = null);
public record HeroButtonDto(string Label, string Target);
@@ -40,6 +40,9 @@ public interface IHeroImageService
/// </summary>
void ApplyFocus(HeroImage image, HeroFocusRequest request);
/// <summary>The variant the renderer is busy with: (done, total), or null when the image is not being cut right now.</summary>
(int Done, int Total)? RenderProgress(Guid imageId);
/// <summary>True when the background renderer gave up on the image after a few failed tries (until a retry or a restart).</summary>
bool IsRenderFailed(Guid imageId);
@@ -160,6 +163,8 @@ public class HeroImageService(
public bool IsRenderFailed(Guid imageId) => signal?.IsGivenUp(imageId) ?? false;
public (int Done, int Total)? RenderProgress(Guid imageId) => signal?.GetProgress(imageId);
public async Task RetryRenderAsync(Guid imageId, CancellationToken cancellationToken)
{
_ = await repository.GetImageAsync(imageId, cancellationToken) ?? throw new HeroImageNotFoundException();
@@ -181,8 +186,13 @@ public class HeroImageService(
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
logger.LogError(exception, "Hero image {ImageId}: cutting the variants failed.", image.Id);
throw new HeroImageRenderException(image.Id, exception);
}
finally
{
signal?.ClearProgress(image.Id);
}
}
private async Task<Guid?> RenderAsync(HeroImage image, CancellationToken cancellationToken)
@@ -193,6 +203,7 @@ public class HeroImageService(
Dictionary<string, byte[]> files;
List<HeroVariant> variants;
var watch = System.Diagnostics.Stopwatch.StartNew();
logger.LogInformation("Hero image {ImageId}: cutting the variants starts ({Width}x{Height}, {Bytes} bytes, {Reason}).", image.Id, image.Width, image.Height, image.Bytes, image.Variants.Count == 0 ? "new" : "focus changed");
using (await gate.EnterAsync(cancellationToken))
{
var file = await store.OpenReadAsync(image.OriginalKey, cancellationToken)
@@ -204,7 +215,13 @@ public class HeroImageService(
}
var storedFormat = ImageFormatExtensions.FromExtension(Path.GetExtension(image.OriginalKey).TrimStart('.'));
(files, variants) = await RenderSetAsync(image, memory.ToArray(), storedFormat, cancellationToken);
var lastStep = watch.Elapsed;
(files, variants) = await RenderSetAsync(image, memory.ToArray(), storedFormat, (done, total) =>
{
signal?.SetProgress(image.Id, done, total);
logger.LogInformation("Hero image {ImageId}: variant {Done} of {Total} in {Milliseconds:0} ms.", image.Id, done, total, (watch.Elapsed - lastStep).TotalMilliseconds);
lastStep = watch.Elapsed;
}, cancellationToken);
}
await store.PutSetAsync(HeroMapping.Area, target, files, cancellationToken);
@@ -260,22 +277,25 @@ public class HeroImageService(
assign((f.X, f.Y, f.Zoom));
}
/// <summary>Cuts and encodes every planned variant of the image at its current focus. Returns the files by name and the variant list.</summary>
private async Task<(Dictionary<string, byte[]> Files, List<HeroVariant> Variants)> RenderSetAsync(HeroImage image, byte[] original, ImageFormat storedFormat, CancellationToken cancellationToken)
/// <summary>Cuts and encodes every planned variant of the image at its current focus (one decode for the whole set).</summary>
private async Task<(Dictionary<string, byte[]> Files, List<HeroVariant> Variants)> RenderSetAsync(HeroImage image, byte[] original, ImageFormat storedFormat, Action<int, int>? progress, CancellationToken cancellationToken)
{
var ratios = new[] { Ratio.Wide, Ratio.Tall }
.Select(r => new RatioPlan(r, image.FocusFor(r.Name), VariantPlanner.DefaultWidths(r)))
.ToList();
var specs = VariantPlanner.Plan(image.Width, image.Height, ratios, processor.SupportedOutput);
progress?.Invoke(0, specs.Count);
var rendered = await processor.RenderSetAsync(original, storedFormat, specs, progress, cancellationToken);
var files = new Dictionary<string, byte[]>();
var variants = new List<HeroVariant>();
foreach (var spec in specs)
for (var i = 0; i < specs.Count; i++)
{
var rendered = await processor.RenderAsync(original, storedFormat, spec, cancellationToken);
var spec = specs[i];
var name = MediaPaths.FileName(spec.Ratio.Name, spec.Width, spec.Format);
files[name] = rendered.Bytes;
variants.Add(new HeroVariant(spec.Ratio.Name, rendered.Width, rendered.Height, spec.Format.MimeType(), name, rendered.Bytes.LongLength));
files[name] = rendered[i].Bytes;
variants.Add(new HeroVariant(spec.Ratio.Name, rendered[i].Width, rendered[i].Height, spec.Format.MimeType(), name, rendered[i].Bytes.LongLength));
}
return (files, variants);
@@ -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)
@@ -38,13 +38,19 @@ public class HeroService(
public async Task<HeroAdminDto> GetAsync(CancellationToken cancellationToken)
=> WithFailures(HeroMapping.ToAdmin(await repository.GetReadOnlyAsync(HeroLanguages.Default, cancellationToken), _time.GetUtcNow(), options.Value.MaxPublishedSlides));
/// <summary>Marks the images the background renderer gave up on (that state lives in memory, not in the database).</summary>
/// <summary>Adds what only the running renderer knows (gave up on the image, which variant it is cutting); it lives in memory, not in the database.</summary>
private HeroAdminDto WithFailures(HeroAdminDto dto)
=> dto with
{
Slides = dto.Slides
.Select(s => imageService.IsRenderFailed(Guid.Parse(s.ImageId)) ? s with { Image = s.Image with { Failed = true } } : s)
.ToList(),
Slides = dto.Slides.Select(s =>
{
var id = Guid.Parse(s.ImageId);
var progress = imageService.RenderProgress(id);
var failed = imageService.IsRenderFailed(id);
return failed || progress is not null
? s with { Image = s.Image with { Failed = failed, RenderDone = progress?.Done, RenderTotal = progress?.Total } }
: s;
}).ToList(),
};
public async Task<HeroAdminDto> SaveAsync(HeroSaveRequest request, Guid callerId, CancellationToken cancellationToken)