From eeffae659f53041bca6330a78614520e064814b3 Mon Sep 17 00:00:00 2001 From: Toutsu Date: Thu, 4 Jun 2026 08:44:57 +0300 Subject: [PATCH] feat(wizard): add WizardDraftCleanupService (1-min tick) --- .../Wizard/WizardDraftCleanupService.cs | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/GmRelay.Bot/Features/Sessions/CreateSession/Wizard/WizardDraftCleanupService.cs diff --git a/src/GmRelay.Bot/Features/Sessions/CreateSession/Wizard/WizardDraftCleanupService.cs b/src/GmRelay.Bot/Features/Sessions/CreateSession/Wizard/WizardDraftCleanupService.cs new file mode 100644 index 0000000..03c2c12 --- /dev/null +++ b/src/GmRelay.Bot/Features/Sessions/CreateSession/Wizard/WizardDraftCleanupService.cs @@ -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 _log; + + public WizardDraftCleanupService( + WizardDraftRepository drafts, + ILogger 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"); + } + } +}