feat(jobs): jobs Hangfire rétention/cleanup + dashboard admin (Fixes #8)

- RetentionCheckJob (quotidien 3h) : purge enregistrements expirés par
  StoragePolicy via ExecuteDelete (1 requête, pas de boucle), défaut 30 j
- CleanupUnknownFacesJob (dim 5h) : personnes 'unknown' > 30 j
- CleanupLicensePlatesJob (dim 6h) : plaques non associées > 30 j
- DatabaseMaintenanceJob (dim 7h) : VACUUM events + REINDEX (VACUUM FULL
  1er dimanche)
- Dashboard Hangfire /hangfire protégé AdminJobs (filtre custom)
- S3CleanupJob dépend du ticket #7 (stockage S3) — non inclus ici
- Tests : 2 (rétention avec/sans politique)
This commit is contained in:
Arnaud Dartois committed 2026-08-12 09:43:45 +02:00
1 parent 2ee12a39f8
commit e3d14eec04
9 files changed
+366

No files matched your search

@@ -0,0 +1,109 @@
using Heimdall.Domain.Entities;
using Heimdall.Domain.Enums;
using Heimdall.Infrastructure.Data;
using Heimdall.Infrastructure.Data.Interceptors;
using Heimdall.Infrastructure.Jobs;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Shouldly;
namespace Heimdall.Infrastructure.Tests;
public class RetentionCheckJobTests : IDisposable
{
private readonly SqliteConnection _connection;
private readonly TestDbContextFactory _dbFactory;
private readonly ILogger<RetentionCheckJob> _logger = Substitute.For<ILogger<RetentionCheckJob>>();
public RetentionCheckJobTests()
{
_connection = new SqliteConnection("DataSource=:memory:");
_connection.Open();
_dbFactory = CreateFactory(_connection);
}
public void Dispose()
{
_connection?.Close();
_connection?.Dispose();
GC.SuppressFinalize(this);
}
private static TestDbContextFactory CreateFactory(SqliteConnection connection)
{
var opts = new DbContextOptionsBuilder<AppDbContext>()
.UseSqlite(connection)
.Options;
using var ctx = new AppDbContext(opts, new AuditInterceptor());
ctx.Database.EnsureCreated();
return new TestDbContextFactory(opts);
}
private async Task<(Guid CameraId, Guid PolicyId)> SeedAsync()
{
await using var db = await _dbFactory.CreateDbContextAsync();
var site = new Site("Site A");
db.Sites.Add(site);
await db.SaveChangesAsync();
var camera = new Camera(site.Id, "Cam 1", "rtsp://cam/stream");
db.Cameras.Add(camera);
await db.SaveChangesAsync();
var policy = new StoragePolicy("30 days", 30, StorageProvider.S3);
db.StoragePolicies.Add(policy);
await db.SaveChangesAsync();
return (camera.Id, policy.Id);
}
private async Task AddRecordingAsync(Guid cameraId, Guid? policyId, DateTime timestamp, int seq)
{
await using var db = await _dbFactory.CreateDbContextAsync();
db.Recordings.Add(new Recording(
cameraId, timestamp, 10.0, $"/recordings/rec-{seq}.mp4", 1000, StorageProvider.S3, policyId));
await db.SaveChangesAsync();
}
[Fact]
public async Task ExecuteAsync_ExpiredWithPolicy_IsDeleted()
{
var (cameraId, policyId) = await SeedAsync();
// 40 jours > rétention 30 j → expiré
await AddRecordingAsync(cameraId, policyId, DateTime.UtcNow.AddDays(-40), 1);
// 10 jours < 30 j → conservé
await AddRecordingAsync(cameraId, policyId, DateTime.UtcNow.AddDays(-10), 2);
await new RetentionCheckJob(_dbFactory, _logger).ExecuteAsync(CancellationToken.None);
await using var verify = await _dbFactory.CreateDbContextAsync();
var remaining = await verify.Recordings.ToListAsync();
remaining.Count.ShouldBe(1);
}
[Fact]
public async Task ExecuteAsync_ExpiredWithoutPolicy_IsDeletedAfterDefault30Days()
{
var (cameraId, _) = await SeedAsync();
await AddRecordingAsync(cameraId, null, DateTime.UtcNow.AddDays(-45), 3); // > 30 j défaut
await AddRecordingAsync(cameraId, null, DateTime.UtcNow.AddDays(-5), 4); // conservé
await new RetentionCheckJob(_dbFactory, _logger).ExecuteAsync(CancellationToken.None);
await using var verify = await _dbFactory.CreateDbContextAsync();
(await verify.Recordings.CountAsync()).ShouldBe(1);
}
private sealed class TestDbContextFactory(DbContextOptions<AppDbContext> opts)
: IDbContextFactory<AppDbContext>
{
private static readonly AuditInterceptor Audit = new();
public AppDbContext CreateDbContext() => new(opts, Audit);
public Task<AppDbContext> CreateDbContextAsync(CancellationToken ct = default)
=> Task.FromResult(new AppDbContext(opts, Audit));
}
}