Skip to content

Commit

Permalink
feat: Use GetRequiredService instead of GetService to resolve jobs (N…
Browse files Browse the repository at this point in the history
…CronJob-Dev#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 <stgiesel35@gmail.com>

---------

Co-authored-by: Steven Giesel <stgiesel35@gmail.com>
  • Loading branch information
2 people authored and falvarez1 committed Apr 23, 2024
1 parent 5ec0d21 commit 918f84b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ All notable changes to **NCronJob** will be documented in this file. The project
### Changed

- Implementation of the scheduling. Better performance and closed some memory leaks
- 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)

## [2.0.4] - 2024-04-16

Expand Down
6 changes: 1 addition & 5 deletions src/LinkDotNet.NCronJob/Execution/JobExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ public async Task RunJob(RegistryEntry run, CancellationToken stoppingToken)
}

var scope = serviceProvider.CreateScope();
if (scope.ServiceProvider.GetService(run.Type) is not IJob job)
{
LogJobNotRegistered(run.Type);
return;
}
var job = (IJob)scope.ServiceProvider.GetRequiredService(run.Type);

var jobExecutionInstance = new JobExecutionContext(run.Type, run.Output);
await ExecuteJob(jobExecutionInstance, job, scope, stopToken);
Expand Down
22 changes: 22 additions & 0 deletions tests/NCronJob.Tests/NCronJobIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Time.Testing;
using Microsoft.Extensions.Logging.Abstractions;
using Shouldly;

namespace NCronJob.Tests;
Expand Down Expand Up @@ -176,6 +177,7 @@ public async Task NotRegisteredJobShouldNotAbortOtherRuns()
var fakeTimer = new FakeTimeProvider();
ServiceCollection.AddSingleton<TimeProvider>(fakeTimer);
ServiceCollection.AddNCronJob(n => n.AddJob<SimpleJob>(p => p.WithCronExpression("* * * * *")));
ServiceCollection.AddTransient<ParameterJob>();
var provider = CreateServiceProvider();
provider.GetRequiredService<IInstantJobRegistry>().RunInstantJob<ParameterJob>();

Expand All @@ -185,6 +187,20 @@ public async Task NotRegisteredJobShouldNotAbortOtherRuns()
jobFinished.ShouldBeTrue();
}

[Fact]
public void ThrowIfJobWithDependenciesIsNotRegistered()
{
ServiceCollection
.AddNCronJob(n => n.AddJob<JobWithDependency>(p => p.WithCronExpression("* * * * *")));
var provider = CreateServiceProvider();

Assert.Throws<InvalidOperationException>(() =>
{
using var executor = new JobExecutor(provider, NullLogger<JobExecutor>.Instance);
executor.RunJob(new RegistryEntry(typeof(JobWithDependency), new JobExecutionContext(null), null), CancellationToken.None);
});
}

private sealed class GuidGenerator
{
public Guid NewGuid { get; } = Guid.NewGuid();
Expand Down Expand Up @@ -234,4 +250,10 @@ private sealed class ParameterJob(ChannelWriter<object> writer) : IJob
public async Task RunAsync(JobExecutionContext context, CancellationToken token)
=> await writer.WriteAsync(context.Parameter!, token);
}

private sealed class JobWithDependency(ChannelWriter<object> writer, GuidGenerator guidGenerator) : IJob
{
public async Task RunAsync(JobExecutionContext context, CancellationToken token)
=> await writer.WriteAsync(guidGenerator.NewGuid, token);
}
}

0 comments on commit 918f84b

Please sign in to comment.