Skip to content

Commit 4562321

Browse files
committed
Removed retrieval of scoped CronJob in constructor
Turns out it served no purpose
1 parent a78df49 commit 4562321

File tree

1 file changed

+11
-25
lines changed

1 file changed

+11
-25
lines changed

src/Pilgaard.CronJobs/CronBackgroundService.cs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Cronos;
1+
using Cronos;
22
using Microsoft.Extensions.DependencyInjection;
33
using Microsoft.Extensions.Hosting;
44
using Microsoft.Extensions.Logging;
@@ -42,16 +42,7 @@ public CronBackgroundService(
4242
_options = options;
4343
_serviceScopeFactory = serviceScopeFactory;
4444
_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;
5546
_cronJobName = _cronJob.GetType().Name;
5647
_cronSchedule = _cronJob.CronSchedule;
5748

@@ -61,12 +52,13 @@ public CronBackgroundService(
6152
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
6253
{
6354
var nextTaskOccurrence = GetNextOccurrence();
55+
6456
while (nextTaskOccurrence is not null &&
6557
stoppingToken.IsCancellationRequested is false)
6658
{
6759
_logger.LogDebug("The next time {cronJobName} will execute is {nextTaskOccurrence}", _cronJobName, nextTaskOccurrence);
6860

69-
await PerformTaskOnNextOccurrence(nextTaskOccurrence, stoppingToken);
61+
await PerformTaskOnNextOccurrenceAsync(nextTaskOccurrence, stoppingToken);
7062

7163
nextTaskOccurrence = GetNextOccurrence();
7264
}
@@ -77,7 +69,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
7769
/// </summary>
7870
/// <param name="nextTaskExecution">The next task execution.</param>
7971
/// <param name="stoppingToken">The stopping token.</param>
80-
private async Task PerformTaskOnNextOccurrence(
72+
private async Task PerformTaskOnNextOccurrenceAsync(
8173
DateTime? nextTaskExecution,
8274
CancellationToken stoppingToken)
8375
{
@@ -95,7 +87,7 @@ private async Task PerformTaskOnNextOccurrence(
9587
// CronJob from the ServiceProvider on every execution.
9688
if (_options.ServiceLifetime is not ServiceLifetime.Singleton)
9789
{
98-
await GetScopedJobAndExecute(stoppingToken);
90+
await GetScopedJobAndExecuteAsync(stoppingToken);
9991
return;
10092
}
10193

@@ -111,15 +103,13 @@ private async Task PerformTaskOnNextOccurrence(
111103
/// <see cref="ICronJob.ExecuteAsync"/> should trigger.
112104
/// </returns>
113105
private DateTime? GetNextOccurrence()
114-
{
115-
return _cronSchedule.GetNextOccurrence(DateTime.UtcNow, _options.TimeZoneInfo);
116-
}
106+
=> _cronSchedule.GetNextOccurrence(DateTime.UtcNow, _options.TimeZoneInfo);
117107

118108
/// <summary>
119109
/// Gets the scoped <see cref="ICronJob"/> and executes it.
120110
/// </summary>
121111
/// <param name="stoppingToken">The stopping token.</param>
122-
private async Task GetScopedJobAndExecute(CancellationToken stoppingToken)
112+
private async Task GetScopedJobAndExecuteAsync(CancellationToken stoppingToken)
123113
{
124114
_logger.LogDebug(
125115
"Fetching a {serviceLifetime} instance of {cronJobName} from the ServiceProvider.",
@@ -144,9 +134,7 @@ private async Task GetScopedJobAndExecute(CancellationToken stoppingToken)
144134
/// is before <see cref="DateTime.UtcNow"/>, otherwise <c>false</c>
145135
/// </returns>
146136
private static bool NextOccurrenceIsInThePast(DateTime? nextTaskExecution)
147-
{
148-
return DateTime.UtcNow > nextTaskExecution.GetValueOrDefault();
149-
}
137+
=> DateTime.UtcNow > nextTaskExecution.GetValueOrDefault();
150138

151139
/// <summary>
152140
/// Gets the <see cref="TimeSpan"/> until the next
@@ -158,7 +146,5 @@ private static bool NextOccurrenceIsInThePast(DateTime? nextTaskExecution)
158146
/// <see cref="ICronJob.ExecuteAsync"/> should be triggered.
159147
/// </returns>
160148
private static TimeSpan TimeUntilNextOccurrence(DateTime? nextTaskExecutionTime)
161-
{
162-
return nextTaskExecutionTime.GetValueOrDefault() - DateTime.UtcNow;
163-
}
164-
}
149+
=> nextTaskExecutionTime.GetValueOrDefault() - DateTime.UtcNow;
150+
}

0 commit comments

Comments
 (0)