Skip to content

Commit 766adb1

Browse files
skarumlinkdotnet
andauthored
feat: Use GetRequiredService instead of GetService to resolve jobs (#24)
* Use GetRequiredService instead of GetService and added tests * Removed redundant test * Added entry in changelog * Remove empty line in NCronJobIntegrationTests.cs * Update CHANGELOG.md Co-authored-by: Steven Giesel <[email protected]> --------- Co-authored-by: Steven Giesel <[email protected]>
1 parent 9b3fe21 commit 766adb1

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
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
- Implementation of the scheduling. Better performance and closed some memory leaks
12+
- Throw exception if job cannot be resolved with dependencies from the DI container. Reported and implemented by [@skarum](https://github.com/skarum) in [#23)(https://github.com/linkdotnet/NCronJob/issues/23)
1213

1314
## [2.0.4] - 2024-04-16
1415

src/LinkDotNet.NCronJob/Execution/JobExecutor.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ public void RunJob(RegistryEntry run, CancellationToken stoppingToken)
2727
}
2828

2929
var scope = serviceProvider.CreateScope();
30-
if (scope.ServiceProvider.GetService(run.Type) is not IJob job)
31-
{
32-
LogJobNotRegistered(run.Type);
33-
return;
34-
}
30+
var job = (IJob)scope.ServiceProvider.GetRequiredService(run.Type);
3531

3632
ExecuteJob(run, job, scope, stoppingToken);
3733
}

tests/NCronJob.Tests/NCronJobIntegrationTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using LinkDotNet.NCronJob;
44
using Microsoft.Extensions.DependencyInjection;
55
using Microsoft.Extensions.Hosting;
6+
using Microsoft.Extensions.Logging.Abstractions;
67
using Shouldly;
78
using TimeProviderExtensions;
89

@@ -154,6 +155,7 @@ public async Task NotRegisteredJobShouldNotAbortOtherRuns()
154155
var fakeTimer = TimeProviderFactory.GetTimeProvider();
155156
ServiceCollection.AddSingleton<TimeProvider>(fakeTimer);
156157
ServiceCollection.AddNCronJob(n => n.AddJob<SimpleJob>(p => p.WithCronExpression("* * * * *")));
158+
ServiceCollection.AddTransient<ParameterJob>();
157159
var provider = CreateServiceProvider();
158160
provider.GetRequiredService<IInstantJobRegistry>().RunInstantJob<ParameterJob>();
159161

@@ -163,6 +165,20 @@ public async Task NotRegisteredJobShouldNotAbortOtherRuns()
163165
jobFinished.ShouldBeTrue();
164166
}
165167

168+
[Fact]
169+
public void ThrowIfJobWithDependenciesIsNotRegistered()
170+
{
171+
ServiceCollection
172+
.AddNCronJob(n => n.AddJob<JobWithDependency>(p => p.WithCronExpression("* * * * *")));
173+
var provider = CreateServiceProvider();
174+
175+
Assert.Throws<InvalidOperationException>(() =>
176+
{
177+
using var executor = new JobExecutor(provider, NullLogger<JobExecutor>.Instance);
178+
executor.RunJob(new RegistryEntry(typeof(JobWithDependency), new JobExecutionContext(null), null), CancellationToken.None);
179+
});
180+
}
181+
166182
private sealed class GuidGenerator
167183
{
168184
public Guid NewGuid { get; } = Guid.NewGuid();
@@ -202,4 +218,10 @@ private sealed class ParameterJob(ChannelWriter<object> writer) : IJob
202218
public async Task RunAsync(JobExecutionContext context, CancellationToken token)
203219
=> await writer.WriteAsync(context.Parameter!, token);
204220
}
221+
222+
private sealed class JobWithDependency(ChannelWriter<object> writer, GuidGenerator guidGenerator) : IJob
223+
{
224+
public async Task RunAsync(JobExecutionContext context, CancellationToken token)
225+
=> await writer.WriteAsync(guidGenerator.NewGuid, token);
226+
}
205227
}

0 commit comments

Comments
 (0)