From 26d3178e93ffd7498ca7380128a0e487161ae52f Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Thu, 18 Jan 2018 12:45:39 +0900 Subject: [PATCH] Fix expiration transitioning with a delay --- BTCPayServer/BTCPayServer.csproj | 2 +- BTCPayServer/HostedServices/InvoiceWatcher.cs | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/BTCPayServer/BTCPayServer.csproj b/BTCPayServer/BTCPayServer.csproj index 4462ab7ad..da0db7209 100644 --- a/BTCPayServer/BTCPayServer.csproj +++ b/BTCPayServer/BTCPayServer.csproj @@ -2,7 +2,7 @@ Exe netcoreapp2.0 - 1.0.1.2 + 1.0.1.3 diff --git a/BTCPayServer/HostedServices/InvoiceWatcher.cs b/BTCPayServer/HostedServices/InvoiceWatcher.cs index ebaee1f13..96569e541 100644 --- a/BTCPayServer/HostedServices/InvoiceWatcher.cs +++ b/BTCPayServer/HostedServices/InvoiceWatcher.cs @@ -408,11 +408,23 @@ namespace BTCPayServer.HostedServices } } - private void Watch(string invoiceId) + private async Task Watch(string invoiceId) { if (invoiceId == null) throw new ArgumentNullException(nameof(invoiceId)); _WatchRequests.Add(invoiceId); + var invoice = await _InvoiceRepository.GetInvoice(null, invoiceId); + try + { + var now = DateTimeOffset.UtcNow; + if (invoice.ExpirationTime > now) + { + await Task.Delay(invoice.ExpirationTime - now, _Cts.Token); + _WatchRequests.Add(invoiceId); + } + } + catch when (_Cts.IsCancellationRequested) + { } } BlockingCollection _WatchRequests = new BlockingCollection(new ConcurrentQueue()); @@ -430,7 +442,7 @@ namespace BTCPayServer.HostedServices leases.Add(_EventAggregator.Subscribe(async b => { await NotifyBlock(); })); leases.Add(_EventAggregator.Subscribe(async b => { await NotifyReceived(b.ScriptPubKey, b.Network); })); - leases.Add(_EventAggregator.Subscribe(b => { Watch(b.InvoiceId); })); + leases.Add(_EventAggregator.Subscribe(async b => { await Watch(b.InvoiceId); })); return Task.CompletedTask; }