From 6d284b4124ba5a220cd31befd58fc8816c2a0a3e Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Fri, 27 Sep 2024 15:48:16 +0900 Subject: [PATCH] Give time for pollers to detect payments after server restart --- BTCPayServer/HostedServices/InvoiceWatcher.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/BTCPayServer/HostedServices/InvoiceWatcher.cs b/BTCPayServer/HostedServices/InvoiceWatcher.cs index cc094a534..fceda6d82 100644 --- a/BTCPayServer/HostedServices/InvoiceWatcher.cs +++ b/BTCPayServer/HostedServices/InvoiceWatcher.cs @@ -192,13 +192,20 @@ namespace BTCPayServer.HostedServices } } - private async Task Wait(string invoiceId) => await Wait(await _invoiceRepository.GetInvoice(invoiceId)); - private async Task Wait(InvoiceEntity invoice) + private async Task Wait(string invoiceId, bool startup) => await Wait(await _invoiceRepository.GetInvoice(invoiceId), startup); + private async Task Wait(InvoiceEntity invoice, bool startup) { + var startupOffset = TimeSpan.Zero; + + // This give some times for the pollers in the listeners to catch payments which happened + // while the server was down. + if (startup) + startupOffset += TimeSpan.FromMinutes(2.0); + try { // add 1 second to ensure watch won't trigger moments before invoice expires - var delay = invoice.ExpirationTime.AddSeconds(1) - DateTimeOffset.UtcNow; + var delay = (invoice.ExpirationTime.AddSeconds(1) + startupOffset) - DateTimeOffset.UtcNow; if (delay > TimeSpan.Zero) { await Task.Delay(delay, _Cts.Token); @@ -243,7 +250,7 @@ namespace BTCPayServer.HostedServices if (b.Name == InvoiceEvent.Created) { Watch(b.Invoice.Id); - _ = Wait(b.Invoice.Id); + _ = Wait(b.Invoice.Id, false); } if (b.Name == InvoiceEvent.ReceivedPayment) @@ -257,7 +264,7 @@ namespace BTCPayServer.HostedServices private async Task WaitPendingInvoices() { await Task.WhenAll((await GetPendingInvoices(_Cts.Token)) - .Select(i => Wait(i)).ToArray()); + .Select(i => Wait(i, true)).ToArray()); } private async Task GetPendingInvoices(CancellationToken cancellationToken)