diff --git a/Lieb/Data/RaidService.cs b/Lieb/Data/RaidService.cs index e452fc1..a242398 100644 --- a/Lieb/Data/RaidService.cs +++ b/Lieb/Data/RaidService.cs @@ -424,7 +424,7 @@ namespace Lieb.Data using var context = _contextFactory.CreateDbContext(); await context.RaidLogs.AddAsync(log); await context.SaveChangesAsync(); - + await SendDiscordSignUpLogMessage(signUp, userName, signedUpBy); } @@ -485,6 +485,25 @@ namespace Lieb.Data } } + public async Task CleanUpRaids() + { + using var context = _contextFactory.CreateDbContext(); + List raids = GetRaids(); + + DateTimeOffset utcNow = DateTimeOffset.UtcNow; + foreach(Raid raid in raids.Where(r => r.EndTimeUTC < utcNow.AddYears(-1))) + { + await DeleteRaid(raid.RaidId); + } + foreach(Raid raid in raids.Where(r => r.EndTimeUTC < utcNow.AddHours(-1))) + { + await _discordService.DeleteRaidMessages(raid); + context.RaidReminders.RemoveRange(raid.Reminders); + context.DiscordRaidMessages.RemoveRange(raid.DiscordRaidMessages); + await context.SaveChangesAsync(); + } + } + public RaidRole CreateRandomSignUpRole(RaidType raidType) { return new RaidRole() diff --git a/Lieb/Data/TimerService.cs b/Lieb/Data/TimerService.cs index 57a05f3..7ec7aec 100644 --- a/Lieb/Data/TimerService.cs +++ b/Lieb/Data/TimerService.cs @@ -4,7 +4,8 @@ namespace Lieb.Data { public class TimerService : IHostedService, IDisposable { - private Timer _timer = null!; + private Timer _minuteTimer = null!; + private Timer _fiveMinuteTimer = null!; private IServiceProvider _services; public TimerService(IServiceProvider services) @@ -14,8 +15,10 @@ namespace Lieb.Data public Task StartAsync(CancellationToken stoppingToken) { - _timer = new Timer(CheckRaids, null, TimeSpan.Zero, + _minuteTimer = new Timer(CheckRaids, null, TimeSpan.Zero, TimeSpan.FromMinutes(1)); + _fiveMinuteTimer = new Timer(CleanUpRaids, null, TimeSpan.Zero, + TimeSpan.FromMinutes(5)); return Task.CompletedTask; } @@ -45,16 +48,29 @@ namespace Lieb.Data } } + private async void CleanUpRaids(object? state) + { + using (var scope = _services.CreateScope()) + { + var raidService = + scope.ServiceProvider + .GetRequiredService(); + await raidService.CleanUpRaids(); + } + } + public Task StopAsync(CancellationToken stoppingToken) { - _timer?.Change(Timeout.Infinite, 0); + _minuteTimer?.Change(Timeout.Infinite, 0); + _fiveMinuteTimer?.Change(Timeout.Infinite, 0); return Task.CompletedTask; } public void Dispose() { - _timer?.Dispose(); + _minuteTimer?.Dispose(); + _fiveMinuteTimer?.Dispose(); } } }