Files
Flypaper/src/Flypaper.Storage/Maintenance/RetentionCleanup.cs
T
TomandClaude Sonnet 5.5 b559f83550
Build and deploy / build (push) Successful in 1m29s
Release / image (push) Successful in 1m53s
Build and deploy / deploy (push) Successful in 30s
Release / release (push) Successful in 6s
Record request headers and bodies and show them per catch
Headers and the first 8 KB of the body are stored unmasked for 30 days,
read alongside the response so the trap answers as before. MCP get_request
and the expanded catch row show them. Changelog prepared for 0.10.0.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-26 21:47:28 +01:00

69 lines
2.0 KiB
C#

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Flypaper.Storage.Maintenance;
internal sealed partial class RetentionCleanup(
TrapDataMaintenance maintenance,
IOptionsMonitor<RetentionOptions> options,
TimeProvider timeProvider,
ILogger<RetentionCleanup> logger) : BackgroundService
{
private const int VacuumThreshold = 5_000;
private static readonly TimeSpan Interval = TimeSpan.FromDays(1);
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (true)
{
try
{
await CleanupAsync(stoppingToken);
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
LogCleanupFailed(ex);
}
await Task.Delay(Interval, timeProvider, stoppingToken);
}
}
internal async Task<int> CleanupAsync(CancellationToken ct)
{
var requestDays = options.CurrentValue.RequestDays;
if (requestDays > 0)
{
await maintenance.DeleteRequestsOlderThanAsync(timeProvider.GetUtcNow().UtcDateTime.AddDays(-requestDays), ct);
}
var days = options.CurrentValue.Days;
if (days <= 0)
{
return 0;
}
var cutoff = timeProvider.GetUtcNow().UtcDateTime.AddDays(-days);
var deleted = await maintenance.DeleteOlderThanAsync(cutoff, ct);
if (deleted == 0)
{
return 0;
}
LogDeleted(deleted, days);
if (deleted >= VacuumThreshold)
{
await maintenance.VacuumAsync(ct);
}
return deleted;
}
[LoggerMessage(Level = LogLevel.Information, Message = "Deleted {Count} trap events older than {Days} days")]
private partial void LogDeleted(int count, int days);
[LoggerMessage(Level = LogLevel.Warning, Message = "Retention cleanup failed")]
private partial void LogCleanupFailed(Exception exception);
}