Files
Inboxintel/src/InboxIntel.Infrastructure/Retention/RetentionWorker.cs
T
cesnimda 2b19bddf7b
CI / backend (push) Successful in 52s
CI / frontend (push) Successful in 11s
Deploy Staging / deploy (push) Successful in 24s
Security / secrets (push) Successful in 4s
Security / dependencies (push) Successful in 52s
CI / backend (pull_request) Successful in 49s
CI / frontend (pull_request) Successful in 10s
Security / secrets (pull_request) Successful in 3s
Security / dependencies (pull_request) Successful in 55s
feat(security): audit batch C — data posture, retention, DP keys (#22)
2026-07-02 10:13:07 +02:00

54 lines
2.0 KiB
C#

using InboxIntel.Infrastructure.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace InboxIntel.Infrastructure.Retention;
/// <summary>
/// Daily driver for <see cref="RetentionService"/> (AUDIT H-3). Exits immediately when both
/// retention knobs are 0 (the default) so existing deployments see no behaviour change.
/// </summary>
public class RetentionWorker : BackgroundService
{
private readonly IServiceScopeFactory _scopeFactory;
private readonly DataRetentionOptions _options;
private readonly ILogger<RetentionWorker> _logger;
public RetentionWorker(IServiceScopeFactory scopeFactory, IOptions<DataRetentionOptions> options, ILogger<RetentionWorker> logger)
{
_scopeFactory = scopeFactory;
_options = options.Value;
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
if (_options.PurgeTrashedAfterDays <= 0 && _options.PurgeAllAfterDays <= 0)
{
_logger.LogDebug("RetentionWorker idle: retention is disabled (all knobs 0).");
return;
}
_logger.LogInformation("RetentionWorker started; trashed>{Trashed}d, all>{All}d",
_options.PurgeTrashedAfterDays, _options.PurgeAllAfterDays);
while (!stoppingToken.IsCancellationRequested)
{
try
{
using var scope = _scopeFactory.CreateScope();
var service = scope.ServiceProvider.GetRequiredService<RetentionService>();
await service.PurgeAsync(stoppingToken);
}
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested) { break; }
catch (Exception ex)
{
_logger.LogError(ex, "Retention purge failed; will retry next cycle.");
}
await Task.Delay(TimeSpan.FromHours(24), stoppingToken);
}
}
}