feat(wizard): add WizardDraftCleanupService (1-min tick)
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using GmRelay.Shared.Features.Sessions.CreateSession.Wizard;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace GmRelay.Bot.Features.Sessions.CreateSession.Wizard;
|
||||
|
||||
public sealed class WizardDraftCleanupService : BackgroundService
|
||||
{
|
||||
private static readonly TimeSpan TickInterval = TimeSpan.FromMinutes(1);
|
||||
|
||||
private readonly WizardDraftRepository _drafts;
|
||||
private readonly ILogger<WizardDraftCleanupService> _log;
|
||||
|
||||
public WizardDraftCleanupService(
|
||||
WizardDraftRepository drafts,
|
||||
ILogger<WizardDraftCleanupService> log)
|
||||
{
|
||||
_drafts = drafts;
|
||||
_log = log;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
using var timer = new PeriodicTimer(TickInterval);
|
||||
try
|
||||
{
|
||||
while (await timer.WaitForNextTickAsync(stoppingToken))
|
||||
{
|
||||
await RunOnceAsync(stoppingToken);
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// graceful shutdown
|
||||
}
|
||||
}
|
||||
|
||||
internal async Task RunOnceAsync(CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
var deleted = await _drafts.DeleteExpiredAsync(ct);
|
||||
if (deleted > 0)
|
||||
{
|
||||
_log.LogInformation("Wizard cleanup deleted {Count} expired drafts", deleted);
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException) when (ct.IsCancellationRequested)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.LogError(ex, "Wizard cleanup tick failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user