1
- using Cronos ;
1
+ using Cronos ;
2
2
using Microsoft . Extensions . DependencyInjection ;
3
3
using Microsoft . Extensions . Hosting ;
4
4
using Microsoft . Extensions . Logging ;
@@ -42,16 +42,7 @@ public CronBackgroundService(
42
42
_options = options ;
43
43
_serviceScopeFactory = serviceScopeFactory ;
44
44
_logger = logger ;
45
-
46
- // Lookup CronJob to get it's schedule without compromising its lifecycle
47
- // If lifetime is set to Singleton, the CronJob remains un-disposed.
48
- using var scope = _serviceScopeFactory . CreateScope ( ) ;
49
- var scopedCronJob = scope . ServiceProvider . GetService ( cronJob . GetType ( ) ) ;
50
-
51
- _cronJob = ( ICronJob ? ) scopedCronJob ?? throw new ArgumentNullException (
52
- nameof ( cronJob ) ,
53
- $ "Failed to GetService of type { cronJob . GetType ( ) . FullName } from ServiceProvider. " +
54
- "Remember to register it in the ServiceCollection." ) ;
45
+ _cronJob = cronJob ;
55
46
_cronJobName = _cronJob . GetType ( ) . Name ;
56
47
_cronSchedule = _cronJob . CronSchedule ;
57
48
@@ -61,12 +52,13 @@ public CronBackgroundService(
61
52
protected override async Task ExecuteAsync ( CancellationToken stoppingToken )
62
53
{
63
54
var nextTaskOccurrence = GetNextOccurrence ( ) ;
55
+
64
56
while ( nextTaskOccurrence is not null &&
65
57
stoppingToken . IsCancellationRequested is false )
66
58
{
67
59
_logger . LogDebug ( "The next time {cronJobName} will execute is {nextTaskOccurrence}" , _cronJobName , nextTaskOccurrence ) ;
68
60
69
- await PerformTaskOnNextOccurrence ( nextTaskOccurrence , stoppingToken ) ;
61
+ await PerformTaskOnNextOccurrenceAsync ( nextTaskOccurrence , stoppingToken ) ;
70
62
71
63
nextTaskOccurrence = GetNextOccurrence ( ) ;
72
64
}
@@ -77,7 +69,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
77
69
/// </summary>
78
70
/// <param name="nextTaskExecution">The next task execution.</param>
79
71
/// <param name="stoppingToken">The stopping token.</param>
80
- private async Task PerformTaskOnNextOccurrence (
72
+ private async Task PerformTaskOnNextOccurrenceAsync (
81
73
DateTime ? nextTaskExecution ,
82
74
CancellationToken stoppingToken )
83
75
{
@@ -95,7 +87,7 @@ private async Task PerformTaskOnNextOccurrence(
95
87
// CronJob from the ServiceProvider on every execution.
96
88
if ( _options . ServiceLifetime is not ServiceLifetime . Singleton )
97
89
{
98
- await GetScopedJobAndExecute ( stoppingToken ) ;
90
+ await GetScopedJobAndExecuteAsync ( stoppingToken ) ;
99
91
return ;
100
92
}
101
93
@@ -111,15 +103,13 @@ private async Task PerformTaskOnNextOccurrence(
111
103
/// <see cref="ICronJob.ExecuteAsync"/> should trigger.
112
104
/// </returns>
113
105
private DateTime ? GetNextOccurrence ( )
114
- {
115
- return _cronSchedule . GetNextOccurrence ( DateTime . UtcNow , _options . TimeZoneInfo ) ;
116
- }
106
+ => _cronSchedule . GetNextOccurrence ( DateTime . UtcNow , _options . TimeZoneInfo ) ;
117
107
118
108
/// <summary>
119
109
/// Gets the scoped <see cref="ICronJob"/> and executes it.
120
110
/// </summary>
121
111
/// <param name="stoppingToken">The stopping token.</param>
122
- private async Task GetScopedJobAndExecute ( CancellationToken stoppingToken )
112
+ private async Task GetScopedJobAndExecuteAsync ( CancellationToken stoppingToken )
123
113
{
124
114
_logger . LogDebug (
125
115
"Fetching a {serviceLifetime} instance of {cronJobName} from the ServiceProvider." ,
@@ -144,9 +134,7 @@ private async Task GetScopedJobAndExecute(CancellationToken stoppingToken)
144
134
/// is before <see cref="DateTime.UtcNow"/>, otherwise <c>false</c>
145
135
/// </returns>
146
136
private static bool NextOccurrenceIsInThePast ( DateTime ? nextTaskExecution )
147
- {
148
- return DateTime . UtcNow > nextTaskExecution . GetValueOrDefault ( ) ;
149
- }
137
+ => DateTime . UtcNow > nextTaskExecution . GetValueOrDefault ( ) ;
150
138
151
139
/// <summary>
152
140
/// Gets the <see cref="TimeSpan"/> until the next
@@ -158,7 +146,5 @@ private static bool NextOccurrenceIsInThePast(DateTime? nextTaskExecution)
158
146
/// <see cref="ICronJob.ExecuteAsync"/> should be triggered.
159
147
/// </returns>
160
148
private static TimeSpan TimeUntilNextOccurrence ( DateTime ? nextTaskExecutionTime )
161
- {
162
- return nextTaskExecutionTime . GetValueOrDefault ( ) - DateTime . UtcNow ;
163
- }
164
- }
149
+ => nextTaskExecutionTime . GetValueOrDefault ( ) - DateTime . UtcNow ;
150
+ }
0 commit comments