Files
slp-modular-cms/src/SlpModularCms.Modules.Hero/Services/HeroMediaCleanup.cs
T

112 lines
4.7 KiB
C#

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using SlpModularCms.Core.Media;
using SlpModularCms.Modules.Hero.Data.Entities;
using SlpModularCms.Modules.Hero.Repositories;
namespace SlpModularCms.Modules.Hero.Services;
/// <summary>
/// Removes files nothing uses any more, a retention time (7 days) after they stopped being used, so a site that still
/// holds older data keeps working for a while. Idempotent and safe to interrupt; a referenced file is never removed
/// (a retired set whose hash is used again, because the same photo and focus came back, is simply forgotten as retired).
/// </summary>
public class HeroMediaCleanup(
IHeroRepository repository,
IMediaStore store,
IOptions<MediaOptions> options,
ILogger<HeroMediaCleanup> logger,
TimeProvider? time = null)
{
private readonly TimeProvider _time = time ?? TimeProvider.System;
public async Task<int> RunOnceAsync(CancellationToken cancellationToken)
{
var now = _time.GetUtcNow();
var cutoff = now.AddDays(-Math.Max(0, options.Value.RetentionDays));
var removed = 0;
// 1. Images that were uploaded but never used by a slide: retire them (their files go on a later run).
foreach (var image in await repository.GetUnattachedImagesCreatedBeforeAsync(cutoff, cancellationToken))
{
repository.AddRetired(new HeroRetiredFile { Id = Guid.NewGuid(), Kind = "set", Key = image.SetHash, RetiredAt = cutoff.AddSeconds(-1) });
repository.AddRetired(new HeroRetiredFile { Id = Guid.NewGuid(), Kind = "original", Key = image.OriginalKey, RetiredAt = cutoff.AddSeconds(-1) });
repository.RemoveImage(image);
}
await repository.SaveChangesAsync(cancellationToken);
// 2. Retired files whose retention has passed, unless an image uses them again.
var referencedSets = await repository.GetReferencedSetHashesAsync(cancellationToken);
var referencedOriginals = await repository.GetReferencedOriginalKeysAsync(cancellationToken);
foreach (var file in await repository.GetRetiredBeforeAsync(cutoff, cancellationToken))
{
if (file.Kind == "set" && !referencedSets.Contains(file.Key))
{
await store.DeleteSetAsync(HeroMapping.Area, file.Key, cancellationToken);
removed++;
}
else if (file.Kind == "original" && !referencedOriginals.Contains(file.Key))
{
await store.DeleteAsync(file.Key, cancellationToken);
removed++;
}
repository.RemoveRetired(file);
}
await repository.SaveChangesAsync(cancellationToken);
// 3. Sets in the store that no row knows (a crash between writing the files and saving the row).
var retiredSets = await repository.GetRetiredSetHashesAsync(cancellationToken);
referencedSets = await repository.GetReferencedSetHashesAsync(cancellationToken);
foreach (var set in await store.ListSetsAsync(HeroMapping.Area, cancellationToken))
{
if (set.CreatedUtc < cutoff && !referencedSets.Contains(set.SetHash) && !retiredSets.Contains(set.SetHash))
{
await store.DeleteSetAsync(HeroMapping.Area, set.SetHash, cancellationToken);
removed++;
}
}
if (removed > 0)
{
logger.LogInformation("Hero media clean-up removed {Removed} unused sets or originals.", removed);
}
return removed;
}
}
/// <summary>Runs <see cref="HeroMediaCleanup"/> shortly after start-up and then once a day.</summary>
public sealed class HeroMediaCleanupService(IServiceScopeFactory scopes, ILogger<HeroMediaCleanupService> logger) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
await Task.Delay(TimeSpan.FromMinutes(2), stoppingToken);
while (!stoppingToken.IsCancellationRequested)
{
try
{
using var scope = scopes.CreateScope();
await scope.ServiceProvider.GetRequiredService<HeroMediaCleanup>().RunOnceAsync(stoppingToken);
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
logger.LogError(exception, "The hero media clean-up failed; it is tried again tomorrow.");
}
await Task.Delay(TimeSpan.FromHours(24), stoppingToken);
}
}
catch (OperationCanceledException)
{
// Shutting down.
}
}
}