First runnable prototype.

This commit is contained in:
2025-07-05 18:02:25 +02:00
parent cc00c4da08
commit 908774c5aa
10 changed files with 385 additions and 219 deletions

View File

@ -0,0 +1,41 @@
using MessengerBroker.Configuration.Model;
using MessengerBroker.Factories;
using Microsoft.EntityFrameworkCore;
namespace MessengerBroker.Handlers
{
public class HousekeepingHandler
{
private readonly BrokerConfiguration brokerConfiguration;
private readonly BrokerDbContextFactory brokerDbContextFactory;
public HousekeepingHandler(BrokerConfiguration brokerConfiguration)
{
this.brokerConfiguration = brokerConfiguration;
}
public Task BeginHousekeeping(CancellationToken ct = default)
{
if(this.brokerConfiguration.HousekeepingEnabled == false)
{
return Task.CompletedTask;
}
return Task.Run(async () =>
{
while (!ct.IsCancellationRequested)
{
await Task.Delay(TimeSpan.FromMinutes(1));
using var context = this.brokerDbContextFactory.CreateDbContext();
var oldestAllowedTimestamp = DateTime.UtcNow.AddMinutes(-this.brokerConfiguration.HousekeepingMessageAgeInMinutes);
await context.Messages
.Where(x => x.TimestampUtc < oldestAllowedTimestamp)
.ExecuteDeleteAsync();
}
});
}
}
}