-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scheduler.cs
40 lines (37 loc) · 1.29 KB
/
Scheduler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System.Threading;
using System.Threading.Tasks;
public class Scheduler : BackgroundService
{
private readonly IYrApi YrApi;
private readonly IWeatherCache WeatherCache;
private readonly ILogger<Scheduler> Log;
public Scheduler(IYrApi yrApi, IWeatherCache weatherCache, ILogger<Scheduler> log)
{
YrApi = yrApi;
WeatherCache = weatherCache;
Log = log;
}
protected override async Task ExecuteAsync(CancellationToken stopToken)
{
Log.LogInformation("Starting up scheduler...");
while (!stopToken.IsCancellationRequested)
{
try
{
Log.LogInformation("Scheduler activated.");
var weather = await YrApi.GetWeatherData();
WeatherCache.Set(weather.temp, weather.windSpeed, weather.windDirection, weather.precipitation, weather.humidity, weather.windGust);
await Task.Delay(TimeSpan.FromMinutes(15), stopToken);
}
catch (TaskCanceledException)
{
Log.LogInformation("Scheduler stopped");
}
catch (Exception ex)
{
Log.LogWarning(ex, "Scheduler failed");
await Task.Delay(TimeSpan.FromMinutes(3), stopToken);
}
}
}
}