feat: clear timeouts that never get logged
This commit is contained in:
parent
0564206bf7
commit
254a50da4d
2 changed files with 18 additions and 3 deletions
|
|
@ -5,7 +5,9 @@ using Remora.Rest.Core;
|
||||||
|
|
||||||
namespace Catalogger.Backend.Database.Repositories;
|
namespace Catalogger.Backend.Database.Repositories;
|
||||||
|
|
||||||
public class TimeoutRepository(DatabaseConnection conn) : IDisposable, IAsyncDisposable
|
public class TimeoutRepository(DatabaseConnection conn, IClock clock)
|
||||||
|
: IDisposable,
|
||||||
|
IAsyncDisposable
|
||||||
{
|
{
|
||||||
public async Task<DiscordTimeout?> GetAsync(int id) =>
|
public async Task<DiscordTimeout?> GetAsync(int id) =>
|
||||||
await conn.QueryFirstOrDefaultAsync<DiscordTimeout>(
|
await conn.QueryFirstOrDefaultAsync<DiscordTimeout>(
|
||||||
|
|
@ -62,6 +64,12 @@ public class TimeoutRepository(DatabaseConnection conn) : IDisposable, IAsyncDis
|
||||||
new { GuildId = guildId.Value, UserId = userId.Value }
|
new { GuildId = guildId.Value, UserId = userId.Value }
|
||||||
);
|
);
|
||||||
|
|
||||||
|
public async Task<int> RemoveExpiredTimeoutsAsync() =>
|
||||||
|
await conn.ExecuteAsync(
|
||||||
|
"delete from timeouts where until < @Expiry",
|
||||||
|
new { Expiry = clock.GetCurrentInstant() - Duration.FromMinutes(5) }
|
||||||
|
);
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
conn.Dispose();
|
conn.Dispose();
|
||||||
|
|
|
||||||
|
|
@ -35,16 +35,23 @@ public class BackgroundTasksService(ILogger logger, IServiceProvider services) :
|
||||||
await using var scope = services.CreateAsyncScope();
|
await using var scope = services.CreateAsyncScope();
|
||||||
await using var messageRepository =
|
await using var messageRepository =
|
||||||
scope.ServiceProvider.GetRequiredService<MessageRepository>();
|
scope.ServiceProvider.GetRequiredService<MessageRepository>();
|
||||||
|
await using var timeoutRepository =
|
||||||
|
scope.ServiceProvider.GetRequiredService<TimeoutRepository>();
|
||||||
|
|
||||||
var (msgCount, ignoredCount) = await messageRepository.DeleteExpiredMessagesAsync();
|
var (msgCount, ignoredCount) = await messageRepository.DeleteExpiredMessagesAsync();
|
||||||
if (msgCount != 0 || ignoredCount != 0)
|
if (msgCount != 0 || ignoredCount != 0)
|
||||||
{
|
|
||||||
_logger.Information(
|
_logger.Information(
|
||||||
"Deleted {Count} messages and {IgnoredCount} ignored message IDs older than {MaxDays} days old",
|
"Deleted {Count} messages and {IgnoredCount} ignored message IDs older than {MaxDays} days old",
|
||||||
msgCount,
|
msgCount,
|
||||||
ignoredCount,
|
ignoredCount,
|
||||||
MessageRepository.MaxMessageAgeDays
|
MessageRepository.MaxMessageAgeDays
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
var timeoutCount = await timeoutRepository.RemoveExpiredTimeoutsAsync();
|
||||||
|
if (timeoutCount != 0)
|
||||||
|
_logger.Information(
|
||||||
|
"Deleted {Count} expired timeouts that were never logged",
|
||||||
|
timeoutCount
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue