Hero-admin: voortgangsindicator voor foto's die worden verwerkt, mislukt-melding met opnieuw proberen
Spinner per foto zonder varianten, totaal met voortgangsbalk (aria-valuenow/max), uitleg dat de hero al is opgeslagen, bij 3 mislukte pogingen een melding met 'Opnieuw proberen' (POST /hero/images/{id}/retry; foutstatus staat in het geheugen van de renderer). Test dat een refetch van 5 s onopgeslagen wijzigingen niet overschrijft.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -95,6 +95,23 @@ public class HeroImagesController(IHeroImageService service, IOptions<MediaOptio
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Lets the background renderer try an image it gave up on again.</summary>
|
||||
[HttpPost("{id:guid}/retry")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> Retry(Guid id, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
await service.RetryRenderAsync(id, cancellationToken);
|
||||
return NoContent();
|
||||
}
|
||||
catch (HeroImageNotFoundException)
|
||||
{
|
||||
return NotFound(new { error = "not_found", message = Texts().Get(HeroTexts.Image.NotFound) });
|
||||
}
|
||||
}
|
||||
|
||||
[HttpDelete("{id:guid}")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status409Conflict)]
|
||||
|
||||
@@ -24,7 +24,8 @@ public record HeroImageDto(
|
||||
FocusDto? TallFocus,
|
||||
string Src,
|
||||
IReadOnlyList<HeroVariantDto> Variants,
|
||||
bool Processing = false);
|
||||
bool Processing = false,
|
||||
bool Failed = false);
|
||||
|
||||
public record HeroButtonDto(string Label, string Target);
|
||||
|
||||
|
||||
@@ -40,6 +40,13 @@ public interface IHeroImageService
|
||||
/// </summary>
|
||||
void ApplyFocus(HeroImage image, HeroFocusRequest request);
|
||||
|
||||
/// <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);
|
||||
|
||||
/// <summary>Lets the background renderer try an image it gave up on again.</summary>
|
||||
/// <exception cref="HeroImageNotFoundException">There is no such image.</exception>
|
||||
Task RetryRenderAsync(Guid imageId, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>Wakes the background renderer: there is an image whose set is missing or does not match its focus.</summary>
|
||||
void NotifyPending();
|
||||
|
||||
@@ -77,6 +84,9 @@ public class HeroImageService(
|
||||
var existing = await repository.GetImageBySha256Async(sha, cancellationToken);
|
||||
if (existing is not null && await FilesExistAsync(existing, cancellationToken))
|
||||
{
|
||||
// The same photo again also gives up the failed state: the renderer tries it again when it is still pending.
|
||||
signal?.Reset(existing.Id);
|
||||
signal?.Notify();
|
||||
return HeroMapping.ToImageDto(existing);
|
||||
}
|
||||
|
||||
@@ -148,6 +158,15 @@ public class HeroImageService(
|
||||
|
||||
public void NotifyPending() => signal?.Notify();
|
||||
|
||||
public bool IsRenderFailed(Guid imageId) => signal?.IsGivenUp(imageId) ?? false;
|
||||
|
||||
public async Task RetryRenderAsync(Guid imageId, CancellationToken cancellationToken)
|
||||
{
|
||||
_ = await repository.GetImageAsync(imageId, cancellationToken) ?? throw new HeroImageNotFoundException();
|
||||
signal?.Reset(imageId);
|
||||
signal?.Notify();
|
||||
}
|
||||
|
||||
public async Task<Guid?> RenderNextPendingAsync(IReadOnlySet<Guid> skip, CancellationToken cancellationToken)
|
||||
{
|
||||
var image = (await repository.GetImagesNeedingRenderAsync(cancellationToken)).FirstOrDefault(i => !skip.Contains(i.Id));
|
||||
|
||||
@@ -21,6 +21,20 @@ public sealed class HeroRenderSignal
|
||||
}
|
||||
}
|
||||
|
||||
/// <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();
|
||||
|
||||
/// <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);
|
||||
}
|
||||
@@ -33,8 +47,6 @@ public sealed class HeroRenderSignal
|
||||
/// </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)
|
||||
{
|
||||
@@ -58,7 +70,7 @@ public sealed class HeroRenderService(IServiceScopeFactory scopes, HeroRenderSig
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
var skip = _failures.Where(f => f.Value >= MaxAttempts).Select(f => f.Key).ToHashSet();
|
||||
var skip = signal.GivenUp();
|
||||
Guid? handled = null;
|
||||
try
|
||||
{
|
||||
@@ -67,8 +79,8 @@ public sealed class HeroRenderService(IServiceScopeFactory scopes, HeroRenderSig
|
||||
}
|
||||
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);
|
||||
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;
|
||||
}
|
||||
@@ -83,7 +95,7 @@ public sealed class HeroRenderService(IServiceScopeFactory scopes, HeroRenderSig
|
||||
return;
|
||||
}
|
||||
|
||||
_failures.Remove(handled.Value);
|
||||
signal.Reset(handled.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,16 @@ public class HeroService(
|
||||
private readonly ITextCatalog _texts = texts ?? TextCatalog.Default;
|
||||
|
||||
public async Task<HeroAdminDto> GetAsync(CancellationToken cancellationToken)
|
||||
=> HeroMapping.ToAdmin(await repository.GetReadOnlyAsync(HeroLanguages.Default, cancellationToken), _time.GetUtcNow(), options.Value.MaxPublishedSlides);
|
||||
=> 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>
|
||||
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(),
|
||||
};
|
||||
|
||||
public async Task<HeroAdminDto> SaveAsync(HeroSaveRequest request, Guid callerId, CancellationToken cancellationToken)
|
||||
{
|
||||
@@ -128,7 +137,7 @@ public class HeroService(
|
||||
imageService.NotifyPending();
|
||||
logger.LogInformation("Hero {HeroId} {Action} by user {LastModifiedByUserId} ({Slides} slides)", hero.Id, created ? "Created" : "Updated", callerId, keep.Count);
|
||||
|
||||
return HeroMapping.ToAdmin(hero, now, options.Value.MaxPublishedSlides);
|
||||
return WithFailures(HeroMapping.ToAdmin(hero, now, options.Value.MaxPublishedSlides));
|
||||
}
|
||||
|
||||
private static void Apply(HeroContent hero, HeroSaveRequest r, DateTimeOffset now, Guid callerId)
|
||||
|
||||
Reference in New Issue
Block a user