OpenDataVisualizer/Schaad.OpenData.Vehicles/VehicleRefreshService.cs
Claudio Schaad 688775e742 Add Thurgau vehicle stock map (#3)
Choropleth of the 80 Thurgau municipalities from the road vehicle stock dataset (djs-stv-17), with population from sk-stat-56 and outlines from swisstopo. Also fixes the broken ENV line in the Dockerfile.

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:43:23 +00:00

42 lines
1.3 KiB
C#

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Schaad.OpenData.Vehicles;
public sealed class VehicleRefreshService(
VehicleRefresher refresher,
IOptions<VehicleOptions> options,
ILogger<VehicleRefreshService> 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 vehicle 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, "Vehicle refresh failed");
}
await Task.Delay(CheckInterval, stoppingToken);
}
}
}