Remove references to hangfire

This commit is contained in:
nicolas.dorier 2019-01-16 19:30:03 +09:00
parent 2aaa2544bd
commit 7bcf1cbdd5
3 changed files with 8 additions and 52 deletions

View file

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

View file

@ -7,6 +7,6 @@ namespace BTCPayServer.Views.Server
{ {
public enum ServerNavPages public enum ServerNavPages
{ {
Index, Users, Rates, Emails, Policies, Theme, Hangfire, Services, Maintenance, Logs Index, Users, Rates, Emails, Policies, Theme, Services, Maintenance, Logs
} }
} }

View file

@ -7,6 +7,5 @@
<a class="nav-link @ViewData.IsActivePage(ServerNavPages.Theme)" asp-action="Theme">Theme</a> <a class="nav-link @ViewData.IsActivePage(ServerNavPages.Theme)" asp-action="Theme">Theme</a>
<a class="nav-link @ViewData.IsActivePage(ServerNavPages.Maintenance)" asp-action="Maintenance">Maintenance</a> <a class="nav-link @ViewData.IsActivePage(ServerNavPages.Maintenance)" asp-action="Maintenance">Maintenance</a>
<a class="nav-link @ViewData.IsActivePage(ServerNavPages.Logs)" asp-action="Logs">Logs</a> <a class="nav-link @ViewData.IsActivePage(ServerNavPages.Logs)" asp-action="Logs">Logs</a>
<a class="nav-link @ViewData.IsActivePage(ServerNavPages.Hangfire)" href="~/hangfire" target="_blank">Hangfire</a>
</div> </div>