Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use GetRequiredService instead of GetService to resolve jobs #24

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/LinkDotNet.NCronJob/Execution/JobExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ public void 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);

ExecuteJob(run, job, scope, stoppingToken);
}
Expand Down
38 changes: 38 additions & 0 deletions tests/NCronJob.Tests/NCronJobIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using LinkDotNet.NCronJob;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging.Abstractions;
using Shouldly;
using TimeProviderExtensions;

Expand Down Expand Up @@ -154,6 +155,7 @@ public async Task NotRegisteredJobShouldNotAbortOtherRuns()
var fakeTimer = TimeProviderFactory.GetTimeProvider();
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 @@ -163,6 +165,36 @@ 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);

});
}

[Fact]
public async Task NotThrowIfJobWithDependenciesRegistered()
skarum marked this conversation as resolved.
Show resolved Hide resolved
{
ServiceCollection
.AddSingleton<GuidGenerator>()
.AddNCronJob(n => n.AddJob<JobWithDependency>(p => p.WithCronExpression("* * * * *")));
var provider = CreateServiceProvider();

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

var jobFinished = await WaitForJobsOrTimeout(1);
jobFinished.ShouldBeTrue();
}

private sealed class GuidGenerator
{
public Guid NewGuid { get; } = Guid.NewGuid();
Expand Down Expand Up @@ -202,4 +234,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);
}
}