OpenDataVisualizer/Schaad.OpenData.Watercraft/WatercraftRefreshService.cs
Claudio Schaad 39a62255af Add Thurgau watercraft map (#2)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Claudio Schaad <c.schaad@pog.ch>
Co-committed-by: Claudio Schaad <c.schaad@pog.ch>
2026-11-06 21:10:01 +00:00

42 lines
1.3 KiB
C#

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Schaad.OpenData.Watercraft;
public sealed class WatercraftRefreshService(
WatercraftRefresher refresher,
IOptions<WatercraftOptions> options,
ILogger<WatercraftRefreshService> logger) : BackgroundService
{
private static readonly TimeSpan CheckInterval = TimeSpan.FromMinutes(30);
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
await refresher.LoadCacheAsync(stoppingToken);
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
logger.LogError(exception, "Could not load the watercraft cache");
}
while (!stoppingToken.IsCancellationRequested)
{
try
{
if (refresher.IsRefreshDue(options.Value.RefreshInterval))
{
await refresher.RefreshAsync(stoppingToken);
}
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
logger.LogError(exception, "Watercraft refresh failed");
}
await Task.Delay(CheckInterval, stoppingToken);
}
}
}