Skip to content

Commit eb03122

Browse files
committed
feat: Don't interrupt other jobs when one isn't registered
1 parent e0af2d3 commit eb03122

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ All notable changes to **NCronJob** will be documented in this file. The project
99
### Changed
1010

1111
- Moved `EnableSecondPrecision` from `AddNCronJob` to `AddCronJob` to allow for more granular control over the precision of the cron expression
12+
- When a job is not registered, a error is logged, but the execution of other jobs is not interrupted
1213

1314
## [0.12.0] - 2024-03-22
1415

src/LinkDotNet.NCronJob/Scheduler/CronScheduler.LogMessage.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ internal sealed partial class CronScheduler
2121

2222
[LoggerMessage(LogLevel.Trace, "End getting next job runs")]
2323
private partial void LogEndGetNextJobRuns();
24+
25+
[LoggerMessage(LogLevel.Error, "The type '{Type}' is not registered. Register the service via 'AddCronJob<{Type}>()'.")]
26+
private partial void LogJobNotRegistered(Type type);
2427
}

src/LinkDotNet.NCronJob/Scheduler/CronScheduler.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ private void RunActiveJobs(List<Run> runs, CancellationToken stoppingToken)
5050
foreach (var run in runs)
5151
{
5252
var scope = serviceProvider.CreateScope();
53-
var job = (IJob)scope.ServiceProvider.GetRequiredService(run.Type);
53+
if (scope.ServiceProvider.GetService(run.Type) is not IJob job)
54+
{
55+
LogJobNotRegistered(run.Type);
56+
continue;
57+
}
5458

5559
RunJob(run, job, scope, stoppingToken);
5660
}

tests/NCronJob.Tests/NCronJobIntegrationTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,23 @@ public async Task LongRunningJobBlocksSchedulerWithoutIsolationLevelTask()
199199
jobFinished.ShouldBeFalse();
200200
}
201201

202+
[Fact]
203+
public async Task NotRegisteredJobShouldNotAbortOtherRuns()
204+
{
205+
var fakeTimer = TimeProviderFactory.GetTimeProvider();
206+
ServiceCollection.AddSingleton<TimeProvider>(fakeTimer);
207+
ServiceCollection.AddNCronJob();
208+
ServiceCollection.AddCronJob<SimpleJob>(p => p.CronExpression = "* * * * *");
209+
await using var provider = ServiceCollection.BuildServiceProvider();
210+
provider.GetRequiredService<IInstantJobRegistry>().AddInstantJob<ParameterJob>();
211+
212+
await provider.GetRequiredService<IHostedService>().StartAsync(CancellationToken);
213+
214+
fakeTimer.Advance(TimeSpan.FromMinutes(1));
215+
var jobFinished = await WaitForJobsOrTimeout(1);
216+
jobFinished.ShouldBeTrue();
217+
}
218+
202219
private sealed class GuidGenerator
203220
{
204221
public Guid NewGuid { get; } = Guid.NewGuid();

0 commit comments

Comments
 (0)