private async Task UpdateInvoice(string invoiceId) { UTXOChanges changes = null; while (true) { try { var invoice = await _InvoiceRepository.GetInvoice(null, invoiceId).ConfigureAwait(false); if (invoice == null) { break; } var stateBefore = invoice.Status; var result = await UpdateInvoice(changes, invoice).ConfigureAwait(false); changes = result.Changes; if (result.NeedSave) { await _InvoiceRepository.UpdateInvoiceStatus(invoice.Id, invoice.Status, invoice.ExceptionStatus).ConfigureAwait(false); } var changed = stateBefore != invoice.Status; if (changed) { Logs.PayServer.LogInformation($"Invoice {invoice.Id}: {stateBefore} => {invoice.Status}"); } var expirationMonitoring = invoice.MonitoringExpiration.HasValue ? invoice.MonitoringExpiration.Value : invoice.InvoiceTime + TimeSpan.FromMinutes(60); if (invoice.Status == "complete" || ((invoice.Status == "invalid" || invoice.Status == "expired") && expirationMonitoring < DateTimeOffset.UtcNow)) { if (await _InvoiceRepository.RemovePendingInvoice(invoice.Id).ConfigureAwait(false)) { Logs.PayServer.LogInformation("Stopped watching invoice " + invoiceId); } break; } if (!changed || _Cts.Token.IsCancellationRequested) { break; } } catch (OperationCanceledException) when(_Cts.Token.IsCancellationRequested) { break; } catch (Exception ex) { Logs.PayServer.LogError(ex, "Unhandled error on watching invoice " + invoiceId); await Task.Delay(10000, _Cts.Token).ConfigureAwait(false); } } }
private async Task UpdateInvoice(string invoiceId) { UTXOChanges changes = null; while (true) { try { var invoice = await _InvoiceRepository.GetInvoice(null, invoiceId, true).ConfigureAwait(false); if (invoice == null) { break; } var stateBefore = invoice.Status; var postSaveActions = new List <Action>(); var result = await UpdateInvoice(changes, invoice, postSaveActions).ConfigureAwait(false); changes = result.Changes; if (result.NeedSave) { await _InvoiceRepository.UpdateInvoiceStatus(invoice.Id, invoice.Status, invoice.ExceptionStatus).ConfigureAwait(false); _EventAggregator.Publish(new InvoiceDataChangedEvent() { InvoiceId = invoice.Id }); } var changed = stateBefore != invoice.Status; foreach (var saveAction in postSaveActions) { saveAction(); } if (invoice.Status == "complete" || ((invoice.Status == "invalid" || invoice.Status == "expired") && invoice.MonitoringExpiration < DateTimeOffset.UtcNow)) { if (await _InvoiceRepository.RemovePendingInvoice(invoice.Id).ConfigureAwait(false)) { Logs.PayServer.LogInformation("Stopped watching invoice " + invoiceId); } break; } if (!changed || _Cts.Token.IsCancellationRequested) { break; } } catch (OperationCanceledException) when(_Cts.Token.IsCancellationRequested) { break; } catch (Exception ex) { Logs.PayServer.LogError(ex, "Unhandled error on watching invoice " + invoiceId); await Task.Delay(10000, _Cts.Token).ConfigureAwait(false); } } }