prevent watcher loop to crash

This commit is contained in:
nicolas.dorier 2017-10-13 14:59:05 +09:00
parent 62e3f2d8e1
commit 76993d2532
2 changed files with 18 additions and 21 deletions

View File

@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<Version>1.0.0.9</Version>
<Version>1.0.0.10</Version>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Build\dockerfiles\**" />

View File

@ -281,12 +281,12 @@ namespace BTCPayServer.Servcices.Invoices
{
Logs.PayServer.LogInformation("Start watching invoices");
ConcurrentDictionary<string, Lazy<Task>> updating = new ConcurrentDictionary<string, Lazy<Task>>();
try
foreach(var item in _WatchRequests.GetConsumingEnumerable(_Cts.Token))
{
foreach(var item in _WatchRequests.GetConsumingEnumerable(_Cts.Token))
try
{
_Cts.Token.ThrowIfCancellationRequested();
var localItem = item;
// If the invoice is already updating, ignore
Lazy<Task> updateInvoice = new Lazy<Task>(() => UpdateInvoice(localItem), false);
if(updating.TryAdd(item, updateInvoice))
@ -294,26 +294,23 @@ namespace BTCPayServer.Servcices.Invoices
updateInvoice.Value.ContinueWith(i => updating.TryRemove(item, out updateInvoice));
}
}
}
catch(OperationCanceledException) when(_Cts.Token.IsCancellationRequested)
{
try
catch(OperationCanceledException) when(_Cts.Token.IsCancellationRequested)
{
Task.WaitAll(updating.Select(c => c.Value.Value).ToArray());
try
{
Task.WaitAll(updating.Select(c => c.Value.Value).ToArray());
}
catch(AggregateException) { }
_RunningTask.TrySetResult(true);
break;
}
catch(Exception ex)
{
Logs.PayServer.LogCritical(ex, "Error in the InvoiceWatcher loop");
_Cts.Token.WaitHandle.WaitOne(2000);
}
catch(AggregateException) { }
_RunningTask.TrySetResult(true);
}
catch(Exception ex)
{
_Cts.Cancel();
_RunningTask.TrySetException(ex);
Logs.PayServer.LogCritical(ex, "Error in the InvoiceWatcher loop");
}
finally
{
Logs.PayServer.LogInformation("Stop watching invoices");
}
Logs.PayServer.LogInformation("Stop watching invoices");
}