2020-06-28 21:44:35 -05:00
|
|
|
using System;
|
2020-06-28 17:55:27 +09:00
|
|
|
using System.Runtime.CompilerServices;
|
2018-04-23 17:40:36 -05:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2020-06-28 17:55:27 +09:00
|
|
|
using BTCPayServer.Logging;
|
2018-04-23 17:40:36 -05:00
|
|
|
using Microsoft.Extensions.Hosting;
|
2020-06-28 17:55:27 +09:00
|
|
|
using Microsoft.Extensions.Logging;
|
2018-04-23 17:40:36 -05:00
|
|
|
|
|
|
|
namespace BTCPayServer.HostedServices
|
|
|
|
{
|
|
|
|
public abstract class BaseAsyncService : IHostedService
|
|
|
|
{
|
2020-07-30 19:35:58 -05:00
|
|
|
private CancellationTokenSource _Cts = new CancellationTokenSource();
|
2018-04-23 17:40:36 -05:00
|
|
|
protected Task[] _Tasks;
|
2021-11-22 17:16:08 +09:00
|
|
|
public readonly Logs Logs;
|
2022-04-24 05:19:34 +02:00
|
|
|
|
2022-12-22 20:32:24 +09:00
|
|
|
public bool NoLogsOnExit { get; set; }
|
|
|
|
|
2022-04-24 05:19:34 +02:00
|
|
|
protected BaseAsyncService(Logs logs)
|
2021-11-22 17:16:08 +09:00
|
|
|
{
|
|
|
|
Logs = logs;
|
|
|
|
}
|
2018-04-23 17:40:36 -05:00
|
|
|
|
2022-04-24 05:19:34 +02:00
|
|
|
protected BaseAsyncService(ILogger logger)
|
|
|
|
{
|
2023-01-06 14:18:07 +01:00
|
|
|
Logs = new Logs() { PayServer = logger, Events = logger, Configuration = logger };
|
2022-04-24 05:19:34 +02:00
|
|
|
}
|
|
|
|
|
2019-12-26 14:22:36 +09:00
|
|
|
public virtual Task StartAsync(CancellationToken cancellationToken)
|
2018-04-23 17:40:36 -05:00
|
|
|
{
|
2018-04-26 21:52:04 -05:00
|
|
|
_Tasks = InitializeTasks();
|
2022-10-08 12:46:26 +09:00
|
|
|
foreach (var t in _Tasks)
|
|
|
|
t.ContinueWith(t =>
|
|
|
|
{
|
|
|
|
if (t.IsFaulted)
|
|
|
|
Logs.PayServer.LogWarning(t.Exception, $"Unhanded exception in {this.GetType().Name}");
|
|
|
|
}, TaskScheduler.Default);
|
2018-04-23 17:40:36 -05:00
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2018-04-26 21:52:04 -05:00
|
|
|
internal abstract Task[] InitializeTasks();
|
2018-04-23 17:40:36 -05:00
|
|
|
|
2018-04-26 21:52:04 -05:00
|
|
|
protected CancellationToken Cancellation
|
2018-04-26 21:44:21 -05:00
|
|
|
{
|
|
|
|
get { return _Cts.Token; }
|
|
|
|
}
|
|
|
|
|
2020-06-28 17:55:27 +09:00
|
|
|
protected async Task CreateLoopTask(Func<Task> act, [CallerMemberName] string caller = null)
|
2018-04-23 17:40:36 -05:00
|
|
|
{
|
|
|
|
await new SynchronizationContextRemover();
|
|
|
|
while (!_Cts.IsCancellationRequested)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await act();
|
|
|
|
}
|
|
|
|
catch (OperationCanceledException) when (_Cts.IsCancellationRequested)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Logs.PayServer.LogWarning(ex, caller + " failed");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await Task.Delay(TimeSpan.FromMinutes(1), _Cts.Token);
|
|
|
|
}
|
|
|
|
catch (OperationCanceledException) when (_Cts.IsCancellationRequested) { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 10:34:09 +09:00
|
|
|
public CancellationToken CancellationToken => _Cts.Token;
|
|
|
|
|
2019-12-26 14:22:36 +09:00
|
|
|
public virtual async Task StopAsync(CancellationToken cancellationToken)
|
2018-04-23 17:40:36 -05:00
|
|
|
{
|
2019-10-19 14:12:43 +09:00
|
|
|
if (_Cts != null)
|
|
|
|
{
|
|
|
|
_Cts.Cancel();
|
2021-10-12 17:39:48 +09:00
|
|
|
if (_Tasks != null)
|
|
|
|
await Task.WhenAll(_Tasks);
|
2019-10-19 14:12:43 +09:00
|
|
|
}
|
2022-12-22 20:32:24 +09:00
|
|
|
if (!NoLogsOnExit)
|
|
|
|
Logs.PayServer.LogInformation($"{this.GetType().Name} successfully exited...");
|
2018-04-23 17:40:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|