mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-18 06:24:24 +01:00
Refactoring of Webhooks and Email Rules (#6954)
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using Dapper;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using BTCPayServer.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using BTCPayServer.HostedServices;
|
||||
|
||||
namespace BTCPayServer.Plugins.Webhooks.HostedServices
|
||||
{
|
||||
public class CleanupWebhookDeliveriesTask : IPeriodicTask
|
||||
{
|
||||
public CleanupWebhookDeliveriesTask(ApplicationDbContextFactory dbContextFactory)
|
||||
{
|
||||
DbContextFactory = dbContextFactory;
|
||||
}
|
||||
|
||||
public ApplicationDbContextFactory DbContextFactory { get; }
|
||||
public int BatchSize { get; set; } = 500;
|
||||
public TimeSpan PruneAfter { get; set; } = TimeSpan.FromDays(60);
|
||||
|
||||
public async Task Do(CancellationToken cancellationToken)
|
||||
{
|
||||
await using var ctx = DbContextFactory.CreateContext();
|
||||
var conn = ctx.Database.GetDbConnection();
|
||||
bool pruned = false;
|
||||
int offset = 0;
|
||||
retry:
|
||||
var rows = await conn.QueryAsync<WebhookDeliveryData>(@"
|
||||
SELECT ""Id"", ""Blob""
|
||||
FROM ""WebhookDeliveries""
|
||||
WHERE ((now() - ""Timestamp"") > @PruneAfter) AND ""Pruned"" IS FALSE
|
||||
ORDER BY ""Timestamp""
|
||||
LIMIT @BatchSize OFFSET @offset
|
||||
", new { PruneAfter, BatchSize, offset });
|
||||
|
||||
foreach (var d in rows)
|
||||
{
|
||||
var blob = d.GetBlob();
|
||||
blob.Prune();
|
||||
d.SetBlob(blob);
|
||||
d.Pruned = true;
|
||||
pruned = true;
|
||||
}
|
||||
if (pruned)
|
||||
{
|
||||
pruned = false;
|
||||
await conn.ExecuteAsync("UPDATE \"WebhookDeliveries\" SET \"Blob\"=@Blob::JSONB, \"Pruned\"=@Pruned WHERE \"Id\"=@Id", rows);
|
||||
if (rows.Count() == BatchSize)
|
||||
{
|
||||
offset += BatchSize;
|
||||
goto retry;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user