-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorker.cs
More file actions
59 lines (52 loc) · 1.42 KB
/
Worker.cs
File metadata and controls
59 lines (52 loc) · 1.42 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Polly;
namespace Vultar;
internal sealed class Worker(
ILogger<Worker> logger,
IOptionsMonitor<VultarSettings> options,
ResiliencePipeline pipeline
) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
var settings = options.CurrentValue;
logger.WorkerStarted(settings.Interval);
using var timer = new PeriodicTimer(settings.Interval);
options.OnChange(newSettings =>
{
logger.WorkerStarted(newSettings.Interval);
});
try
{
while (await timer.WaitForNextTickAsync(stoppingToken))
{
await ExecuteWorkAsync(stoppingToken);
}
}
catch (OperationCanceledException)
{
logger.WorkerStopping();
}
}
private async Task ExecuteWorkAsync(CancellationToken stoppingToken)
{
try
{
await pipeline.ExecuteAsync(async token =>
{
await RunAsync(token);
}, stoppingToken);
}
catch (Exception ex)
{
logger.ExecutionFailed(ex);
}
}
private async Task RunAsync(CancellationToken token)
{
logger.Executing(DateTimeOffset.Now);
await Task.Delay(100, token);
}
}