2019-09-30 10:32:43 +02:00
|
|
|
using System;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using BTCPayServer.Logging;
|
2019-09-30 20:42:31 +09:00
|
|
|
using BTCPayServer.Altcoins.Monero.Configuration;
|
2019-09-30 10:32:43 +02:00
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
2019-09-30 20:42:31 +09:00
|
|
|
namespace BTCPayServer.Altcoins.Monero.Services
|
2019-09-30 10:32:43 +02:00
|
|
|
{
|
|
|
|
public class MoneroLikeSummaryUpdaterHostedService: IHostedService
|
|
|
|
{
|
|
|
|
private readonly MoneroRPCProvider _MoneroRpcProvider;
|
|
|
|
private readonly MoneroLikeConfiguration _moneroLikeConfiguration;
|
|
|
|
private CancellationTokenSource _Cts;
|
|
|
|
public MoneroLikeSummaryUpdaterHostedService(MoneroRPCProvider moneroRpcProvider, MoneroLikeConfiguration moneroLikeConfiguration)
|
|
|
|
{
|
|
|
|
_MoneroRpcProvider = moneroRpcProvider;
|
|
|
|
_moneroLikeConfiguration = moneroLikeConfiguration;
|
|
|
|
}
|
|
|
|
public Task StartAsync(CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
_Cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
|
|
|
foreach (var moneroLikeConfigurationItem in _moneroLikeConfiguration.MoneroLikeConfigurationItems)
|
|
|
|
{
|
|
|
|
_ = StartLoop(_Cts.Token, moneroLikeConfigurationItem.Key);
|
|
|
|
}
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task StartLoop(CancellationToken cancellation, string cryptoCode)
|
|
|
|
{
|
|
|
|
Logs.PayServer.LogInformation($"Starting listening Monero-like daemons ({cryptoCode})");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
while (!cancellation.IsCancellationRequested)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await _MoneroRpcProvider.UpdateSummary(cryptoCode);
|
|
|
|
if (_MoneroRpcProvider.IsAvailable(cryptoCode))
|
|
|
|
{
|
|
|
|
await Task.Delay(TimeSpan.FromMinutes(1), cancellation);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
await Task.Delay(TimeSpan.FromSeconds(10), cancellation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex) when (!cancellation.IsCancellationRequested)
|
|
|
|
{
|
|
|
|
Logs.PayServer.LogError(ex, $"Unhandled exception in Summary updater ({cryptoCode})");
|
|
|
|
await Task.Delay(TimeSpan.FromSeconds(10), cancellation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch when (cancellation.IsCancellationRequested) { }
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
_Cts.Cancel();
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
}
|
2019-09-30 17:51:47 +09:00
|
|
|
}
|