|
1 | | -using System; |
2 | | -using System.Linq; |
3 | | -using System.Threading; |
4 | | -using System.Threading.Tasks; |
5 | 1 | using Microsoft.Extensions.Hosting; |
6 | 2 | using Microsoft.Extensions.Logging; |
7 | 3 |
|
8 | | -namespace CronBackgroundServices |
| 4 | +namespace CronBackgroundServices; |
| 5 | + |
| 6 | +internal class CronBackgroundService : BackgroundService |
9 | 7 | { |
10 | | - internal class CronBackgroundService : BackgroundService |
| 8 | + protected readonly IRecurringAction Action; |
| 9 | + private readonly ILogger _logger; |
| 10 | + private readonly Timing _timing; |
| 11 | + |
| 12 | + public CronBackgroundService(IRecurringAction action, ILogger logger) |
11 | 13 | { |
12 | | - protected readonly IRecurringAction Action; |
13 | | - private readonly ILogger _logger; |
14 | | - private readonly Timing _timing; |
| 14 | + _timing = new Timing(action.GetTimeZoneId()); |
| 15 | + Action = action; |
| 16 | + _logger = logger; |
| 17 | + Cron = action.Cron; |
| 18 | + _logger.LogTrace($"Using {Cron} and timezone '{_timing.TimeZoneInfo.Id}. The time in this timezone: {_timing.RelativeNow()}'"); |
| 19 | + } |
15 | 20 |
|
16 | | - public CronBackgroundService(IRecurringAction action, ILogger logger) |
17 | | - { |
18 | | - _timing = new Timing(action.GetTimeZoneId()); |
19 | | - Action = action; |
20 | | - _logger = logger; |
21 | | - Cron = action.Cron; |
22 | | - _logger.LogTrace($"Using {Cron} and timezone '{_timing.TimeZoneInfo.Id}. The time in this timezone: {_timing.RelativeNow()}'"); |
23 | | - } |
| 21 | + private string Cron { get; } |
24 | 22 |
|
25 | | - private string Cron { get; } |
| 23 | + protected override async Task ExecuteAsync(CancellationToken stoppingToken) |
| 24 | + { |
| 25 | + DateTimeOffset? next = null; |
26 | 26 |
|
27 | | - protected override async Task ExecuteAsync(CancellationToken stoppingToken) |
| 27 | + do |
28 | 28 | { |
29 | | - DateTimeOffset? next = null; |
| 29 | + var now = _timing.RelativeNow(); |
30 | 30 |
|
31 | | - do |
| 31 | + if (next == null) |
32 | 32 | { |
33 | | - var now = _timing.RelativeNow(); |
| 33 | + next = _timing.GetNextOccurenceInRelativeTime(Cron); |
| 34 | + var uText = _timing.Get10NextOccurrences(Cron); |
| 35 | + var logText = $"Ten next occurrences :\n{uText.Aggregate((x, y) => x + "\n" + y)}"; |
| 36 | + _logger.LogTrace(logText); |
| 37 | + } |
34 | 38 |
|
35 | | - if (next == null) |
36 | | - { |
37 | | - next = _timing.GetNextOccurenceInRelativeTime(Cron); |
38 | | - var uText = _timing.Get10NextOccurrences(Cron); |
39 | | - var logText = $"Ten next occurrences :\n{uText.Aggregate((x, y) => x + "\n" + y)}"; |
40 | | - _logger.LogTrace(logText); |
41 | | - } |
42 | | - |
43 | | - if (now > next) |
| 39 | + if (now > next) |
| 40 | + { |
| 41 | + try |
44 | 42 | { |
45 | | - try |
46 | | - { |
47 | | - await Action.Process(stoppingToken); |
48 | | - } |
49 | | - catch (Exception e) |
50 | | - { |
51 | | - _logger.LogError(e, e.Message); |
52 | | - } |
53 | | - |
54 | | - next = _timing.GetNextOccurenceInRelativeTime(Cron); |
55 | | - _logger.LogTrace($"Next at {next.Value.DateTime.ToLongDateString()} {next.Value.DateTime.ToLongTimeString()}"); |
| 43 | + await Action.Process(stoppingToken); |
56 | 44 | } |
57 | | - else |
| 45 | + catch (Exception e) |
58 | 46 | { |
59 | | - // needed for graceful shutdown for some reason. |
60 | | - // 100ms chosen so it doesn't affect calculating the next |
61 | | - // cron occurence (lowest possible: every second) |
62 | | - await Task.Delay(100); |
| 47 | + _logger.LogError(e, e.Message); |
63 | 48 | } |
64 | 49 |
|
65 | | - } while (!stoppingToken.IsCancellationRequested); |
66 | | - } |
| 50 | + next = _timing.GetNextOccurenceInRelativeTime(Cron); |
| 51 | + _logger.LogTrace($"Next at {next.Value.DateTime.ToLongDateString()} {next.Value.DateTime.ToLongTimeString()}"); |
| 52 | + } |
| 53 | + else |
| 54 | + { |
| 55 | + // needed for graceful shutdown for some reason. |
| 56 | + // 100ms chosen so it doesn't affect calculating the next |
| 57 | + // cron occurence (lowest possible: every second) |
| 58 | + await Task.Delay(100); |
| 59 | + } |
67 | 60 |
|
| 61 | + } while (!stoppingToken.IsCancellationRequested); |
68 | 62 | } |
69 | 63 | } |
0 commit comments