Make sure the process doesn't crash if exception raised in Subscribe

This commit is contained in:
nicolas.dorier
2021-10-06 13:22:55 +09:00
parent b970f64639
commit 150e4b842c
7 changed files with 13 additions and 9 deletions

View File

@@ -665,9 +665,9 @@ namespace BTCPayServer.Controllers
CompositeDisposable leases = new CompositeDisposable(); CompositeDisposable leases = new CompositeDisposable();
try try
{ {
leases.Add(_EventAggregator.Subscribe<Events.InvoiceDataChangedEvent>(async o => await NotifySocket(webSocket, o.InvoiceId, invoiceId))); leases.Add(_EventAggregator.SubscribeAsync<Events.InvoiceDataChangedEvent>(async o => await NotifySocket(webSocket, o.InvoiceId, invoiceId)));
leases.Add(_EventAggregator.Subscribe<Events.InvoiceNewPaymentDetailsEvent>(async o => await NotifySocket(webSocket, o.InvoiceId, invoiceId))); leases.Add(_EventAggregator.SubscribeAsync<Events.InvoiceNewPaymentDetailsEvent>(async o => await NotifySocket(webSocket, o.InvoiceId, invoiceId)));
leases.Add(_EventAggregator.Subscribe<Events.InvoiceEvent>(async o => await NotifySocket(webSocket, o.Invoice.Id, invoiceId))); leases.Add(_EventAggregator.SubscribeAsync<Events.InvoiceEvent>(async o => await NotifySocket(webSocket, o.Invoice.Id, invoiceId)));
while (true) while (true)
{ {
var message = await webSocket.ReceiveAndPingAsync(DummyBuffer, default(CancellationToken)); var message = await webSocket.ReceiveAndPingAsync(DummyBuffer, default(CancellationToken));

View File

@@ -62,7 +62,7 @@ namespace BTCPayServer.Controllers
IEventAggregatorSubscription subscription = null; IEventAggregatorSubscription subscription = null;
try try
{ {
subscription = _eventAggregator.Subscribe<UserNotificationsUpdatedEvent>(async evt => subscription = _eventAggregator.SubscribeAsync<UserNotificationsUpdatedEvent>(async evt =>
{ {
if (evt.UserId == userId) if (evt.UserId == userId)
{ {

View File

@@ -145,6 +145,10 @@ namespace BTCPayServer
return Subscribe(new Action<IEventAggregatorSubscription, T>((sub, t) => subscription(sub, t))); return Subscribe(new Action<IEventAggregatorSubscription, T>((sub, t) => subscription(sub, t)));
} }
public IEventAggregatorSubscription SubscribeAsync<T>(Func<T, Task> subscription)
{
return Subscribe(new Action<IEventAggregatorSubscription, T>((sub, t) => _ = subscription(t)));
}
public IEventAggregatorSubscription Subscribe<T>(Action<T> subscription) public IEventAggregatorSubscription Subscribe<T>(Action<T> subscription)
{ {
return Subscribe(new Action<IEventAggregatorSubscription, T>((sub, t) => subscription(t))); return Subscribe(new Action<IEventAggregatorSubscription, T>((sub, t) => subscription(t)));

View File

@@ -309,7 +309,7 @@ namespace BTCPayServer.HostedServices
readonly CompositeDisposable leases = new CompositeDisposable(); readonly CompositeDisposable leases = new CompositeDisposable();
public Task StartAsync(CancellationToken cancellationToken) public Task StartAsync(CancellationToken cancellationToken)
{ {
leases.Add(_EventAggregator.Subscribe<InvoiceEvent>(async e => leases.Add(_EventAggregator.SubscribeAsync<InvoiceEvent>(async e =>
{ {
if (e.EventCode == InvoiceEventCode.PaymentSettled) if (e.EventCode == InvoiceEventCode.PaymentSettled)
{ {

View File

@@ -281,7 +281,7 @@ namespace BTCPayServer.HostedServices
{ {
Watch(b.InvoiceId); Watch(b.InvoiceId);
})); }));
leases.Add(_eventAggregator.Subscribe<Events.InvoiceEvent>(async b => leases.Add(_eventAggregator.SubscribeAsync<Events.InvoiceEvent>(async b =>
{ {
if (InvoiceEventNotification.HandlesEvent(b.Name)) if (InvoiceEventNotification.HandlesEvent(b.Name))
{ {

View File

@@ -75,7 +75,7 @@ namespace BTCPayServer.Payments.Bitcoin
{ {
_RunningTask = new TaskCompletionSource<bool>(); _RunningTask = new TaskCompletionSource<bool>();
_Cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); _Cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
leases.Add(_Aggregator.Subscribe<Events.NBXplorerStateChangedEvent>(async nbxplorerEvent => leases.Add(_Aggregator.SubscribeAsync<Events.NBXplorerStateChangedEvent>(async nbxplorerEvent =>
{ {
if (nbxplorerEvent.NewState == NBXplorerState.Ready) if (nbxplorerEvent.NewState == NBXplorerState.Ready)
{ {

View File

@@ -143,7 +143,7 @@ namespace BTCPayServer.Payments.Lightning
readonly CompositeDisposable leases = new CompositeDisposable(); readonly CompositeDisposable leases = new CompositeDisposable();
public Task StartAsync(CancellationToken cancellationToken) public Task StartAsync(CancellationToken cancellationToken)
{ {
leases.Add(_Aggregator.Subscribe<Events.InvoiceEvent>(async inv => leases.Add(_Aggregator.SubscribeAsync<Events.InvoiceEvent>(async inv =>
{ {
if (inv.Name == InvoiceEvent.Created) if (inv.Name == InvoiceEvent.Created)
{ {
@@ -159,7 +159,7 @@ namespace BTCPayServer.Payments.Lightning
} }
} }
})); }));
leases.Add(_Aggregator.Subscribe<Events.InvoiceDataChangedEvent>(async inv => leases.Add(_Aggregator.SubscribeAsync<Events.InvoiceDataChangedEvent>(async inv =>
{ {
if (inv.State.Status == InvoiceStatusLegacy.New && if (inv.State.Status == InvoiceStatusLegacy.New &&
inv.State.ExceptionStatus == InvoiceExceptionStatus.PaidPartial) inv.State.ExceptionStatus == InvoiceExceptionStatus.PaidPartial)