diff --git a/BTCPayServer/HostedServices/InvoiceNotificationManager.cs b/BTCPayServer/HostedServices/InvoiceNotificationManager.cs index e37cf8b41..dccc036af 100644 --- a/BTCPayServer/HostedServices/InvoiceNotificationManager.cs +++ b/BTCPayServer/HostedServices/InvoiceNotificationManager.cs @@ -42,11 +42,6 @@ namespace BTCPayServer.HostedServices public string Message { get; set; } } - public ILogger Logger - { - get; set; - } - IBackgroundJobClient _JobClient; EventAggregator _EventAggregator; InvoiceRepository _InvoiceRepository; @@ -58,10 +53,8 @@ namespace BTCPayServer.HostedServices EventAggregator eventAggregator, InvoiceRepository invoiceRepository, BTCPayNetworkProvider networkProvider, - ILogger logger, IEmailSender emailSender) { - Logger = logger as ILogger ?? NullLogger.Instance; _JobClient = jobClient; _EventAggregator = eventAggregator; _InvoiceRepository = invoiceRepository; @@ -69,7 +62,7 @@ namespace BTCPayServer.HostedServices _EmailSender = emailSender; } - async Task Notify(InvoiceEntity invoice, int? eventCode = null, string name = null) + void Notify(InvoiceEntity invoice, int? eventCode = null, string name = null) { CancellationTokenSource cts = new CancellationTokenSource(10000); @@ -89,53 +82,22 @@ namespace BTCPayServer.HostedServices _EmailSender.SendEmailAsync(invoice.NotificationEmail, $"BtcPayServer Invoice Notification - ${invoice.StoreId}", emailBody); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed } - - try - { - if (string.IsNullOrEmpty(invoice.NotificationURL)) - return; - _EventAggregator.Publish(new InvoiceIPNEvent(invoice.Id, eventCode, name)); - var response = await SendNotification(invoice, eventCode, name, cts.Token); - response.EnsureSuccessStatusCode(); + if (string.IsNullOrEmpty(invoice.NotificationURL)) return; - } - catch (OperationCanceledException) when (cts.IsCancellationRequested) - { - _EventAggregator.Publish(new InvoiceIPNEvent(invoice.Id, eventCode, name) - { - Error = "Timeout" - }); - } - catch (Exception ex) // It fails, it is OK because we try with hangfire after - { - _EventAggregator.Publish(new InvoiceIPNEvent(invoice.Id, eventCode, name) - { - Error = ex.Message - }); - } var invoiceStr = NBitcoin.JsonConverters.Serializer.ToString(new ScheduledJob() { TryCount = 0, Invoice = invoice, EventCode = eventCode, Message = name }); if (!string.IsNullOrEmpty(invoice.NotificationURL)) _JobClient.Schedule(() => NotifyHttp(invoiceStr), TimeSpan.Zero); } - ConcurrentDictionary _Executing = new ConcurrentDictionary(); public async Task NotifyHttp(string invoiceData) { var job = NBitcoin.JsonConverters.Serializer.ToObject(invoiceData); - var jobId = GetHttpJobId(job.Invoice); - - if (!_Executing.TryAdd(jobId, jobId)) - return; //For some reason, Hangfire fire the job several time - - Logger.LogInformation("Running " + jobId); bool reschedule = false; CancellationTokenSource cts = new CancellationTokenSource(10000); try { HttpResponseMessage response = await SendNotification(job.Invoice, job.EventCode, job.Message, cts.Token); reschedule = !response.IsSuccessStatusCode; - Logger.LogInformation("Job " + jobId + " returned " + response.StatusCode); - _EventAggregator.Publish(new InvoiceIPNEvent(job.Invoice.Id, job.EventCode, job.Message) { Error = reschedule ? $"Unexpected return code: {(int)response.StatusCode}" : null @@ -148,9 +110,8 @@ namespace BTCPayServer.HostedServices Error = "Timeout" }); reschedule = true; - Logger.LogInformation("Job " + jobId + " timed out"); } - catch (Exception ex) // It fails, it is OK because we try with hangfire after + catch (Exception ex) { _EventAggregator.Publish(new InvoiceIPNEvent(job.Invoice.Id, job.EventCode, job.Message) { @@ -165,21 +126,18 @@ namespace BTCPayServer.HostedServices ex = ex.InnerException; } string message = String.Join(',', messages.ToArray()); - Logger.LogInformation("Job " + jobId + " threw exception " + message); _EventAggregator.Publish(new InvoiceIPNEvent(job.Invoice.Id, job.EventCode, job.Message) { Error = $"Unexpected error: {message}" }); } - finally { cts.Dispose(); _Executing.TryRemove(jobId, out jobId); } + finally { cts?.Dispose(); } job.TryCount++; if (job.TryCount < MaxTry && reschedule) { - Logger.LogInformation("Rescheduling " + jobId + " in 10 minutes, remaining try " + (MaxTry - job.TryCount)); - invoiceData = NBitcoin.JsonConverters.Serializer.ToString(job); _JobClient.Schedule(() => NotifyHttp(invoiceData), TimeSpan.FromMinutes(10.0)); } @@ -349,19 +307,18 @@ namespace BTCPayServer.HostedServices e.Name == InvoiceEvent.Completed || e.Name == InvoiceEvent.ExpiredPaidPartial ) - tasks.Add(Notify(invoice)); + Notify(invoice); } if (e.Name == "invoice_confirmed") { - tasks.Add(Notify(invoice)); + Notify(invoice); } if (invoice.ExtendedNotifications) { - tasks.Add(Notify(invoice, e.EventCode, e.Name)); + Notify(invoice, e.EventCode, e.Name); } - await Task.WhenAll(tasks.ToArray()); })); diff --git a/BTCPayServer/Views/Server/ServerNavPages.cs b/BTCPayServer/Views/Server/ServerNavPages.cs index 2772c9572..bb80f2a02 100644 --- a/BTCPayServer/Views/Server/ServerNavPages.cs +++ b/BTCPayServer/Views/Server/ServerNavPages.cs @@ -7,6 +7,6 @@ namespace BTCPayServer.Views.Server { public enum ServerNavPages { - Index, Users, Rates, Emails, Policies, Theme, Hangfire, Services, Maintenance, Logs + Index, Users, Rates, Emails, Policies, Theme, Services, Maintenance, Logs } } diff --git a/BTCPayServer/Views/Server/_Nav.cshtml b/BTCPayServer/Views/Server/_Nav.cshtml index 87a0ca29a..32c16abe2 100644 --- a/BTCPayServer/Views/Server/_Nav.cshtml +++ b/BTCPayServer/Views/Server/_Nav.cshtml @@ -7,6 +7,5 @@ Theme Maintenance Logs - Hangfire