btcpayserver/BTCPayServer/HostedServices/DynamicDnsHostedService.cs

74 lines
2.9 KiB
C#
Raw Normal View History

2019-07-24 10:59:30 +02:00
using System;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using BTCPayServer.Logging;
using BTCPayServer.Services;
namespace BTCPayServer.HostedServices
{
public class DynamicDnsHostedService : BaseAsyncService
{
public DynamicDnsHostedService(IHttpClientFactory httpClientFactory, SettingsRepository settingsRepository)
{
HttpClientFactory = httpClientFactory;
SettingsRepository = settingsRepository;
}
public IHttpClientFactory HttpClientFactory { get; }
public SettingsRepository SettingsRepository { get; }
internal override Task[] InitializeTasks()
{
return new[]
{
CreateLoopTask(UpdateRecord)
};
}
TimeSpan Period = TimeSpan.FromMinutes(60);
async Task UpdateRecord()
{
using (var timeout = CancellationTokenSource.CreateLinkedTokenSource(Cancellation))
{
2019-07-25 16:26:56 +02:00
var settings = await SettingsRepository.GetSettingAsync<DynamicDnsSettings>() ?? new DynamicDnsSettings();
2019-07-25 11:29:18 +02:00
foreach (var service in settings.Services)
2019-07-24 10:59:30 +02:00
{
2019-07-25 11:29:18 +02:00
if (service?.Enabled is true && (service.LastUpdated is null ||
(DateTimeOffset.UtcNow - service.LastUpdated) > Period))
2019-07-24 10:59:30 +02:00
{
2019-07-25 11:29:18 +02:00
timeout.CancelAfter(TimeSpan.FromSeconds(20.0));
try
2019-07-24 10:59:30 +02:00
{
2019-07-25 11:29:18 +02:00
var errorMessage = await service.SendUpdateRequest(HttpClientFactory.CreateClient());
if (errorMessage == null)
{
Logs.PayServer.LogInformation("Dynamic DNS service successfully refresh the DNS record");
service.LastUpdated = DateTimeOffset.UtcNow;
await SettingsRepository.UpdateSetting(settings);
}
else
{
Logs.PayServer.LogWarning($"Dynamic DNS service is enabled but the request to the provider failed: {errorMessage}");
}
2019-07-24 10:59:30 +02:00
}
2019-07-25 11:29:18 +02:00
catch (OperationCanceledException) when (timeout.IsCancellationRequested)
2019-07-24 10:59:30 +02:00
{
}
}
}
}
using (var delayCancel = CancellationTokenSource.CreateLinkedTokenSource(Cancellation))
{
var delay = Task.Delay(Period, delayCancel.Token);
var changed = SettingsRepository.WaitSettingsChanged<DynamicDnsSettings>(Cancellation);
await Task.WhenAny(delay, changed);
delayCancel.Cancel();
}
}
}
}