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>
69 lines
2.0 KiB
C#
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);
|
|
}
|