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

Prevent InstantJobRegistry from altering the content of JobRegistry #131

Merged
merged 3 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ All notable changes to **NCronJob** will be documented in this file. The project

## [Unreleased]

### Changed

- Prevent `InstantJobRegistry` from altering the content of `JobRegistry`. Fixed in [#131](https://github.com/NCronJob-Dev/NCronJob/issues/131), by [@nulltoken](https://github.com/nulltoken).

## [3.3.4] - 2024-11-03

### Fixes

- Ensure multiples schedules of the same job with different chains of dependent jobs are properly processed. Identified in [#108](https://github.com/NCronJob-Dev/NCronJob/issues/107), by [@nulltoken](https://github.com/nulltoken).
- Ensure multiples schedules of the same job with different chains of dependent jobs are properly processed. Identified in [#108](https://github.com/NCronJob-Dev/NCronJob/issues/108), by [@nulltoken](https://github.com/nulltoken).

- Teach `IRuntimeJobRegistry.RemoveJob()` to clean up potential dependent jobs. Fixes [#107](https://github.com/NCronJob-Dev/NCronJob/issues/107), by [@nulltoken](https://github.com/nulltoken).

Expand Down
11 changes: 4 additions & 7 deletions src/NCronJob/Registry/IInstantJobRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,14 @@ private void RunJob<TJob>(DateTimeOffset startDate, object? parameter = null, bo
{
using (logger.BeginScope("Triggering RunScheduledJob:"))
{
if (!jobRegistry.IsJobRegistered<TJob>())
var jobDefinition = jobRegistry.FindJobDefinition(typeof(TJob));

if (jobDefinition is null)
{
LogJobNotRegistered(typeof(TJob).Name);
var newJobDefinition = new JobDefinition(typeof(TJob), parameter, null, null);
jobRegistry.Add(newJobDefinition);
jobDefinition = new JobDefinition(typeof(TJob), parameter, null, null);
}

var jobDefinition = jobRegistry.FindJobDefinition(typeof(TJob));

Debug.Assert(jobDefinition != null);

token.Register(() => LogCancellationRequested(parameter));

RunInternal(jobDefinition, parameter, startDate, forceExecution, token);
Expand Down
4 changes: 0 additions & 4 deletions src/NCronJob/Registry/JobRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ private readonly Dictionary<JobDefinition, List<DependentJobRegistryEntry>> depe

public IReadOnlyCollection<JobDefinition> GetAllOneTimeJobs() => allJobs.Where(c => c.IsStartupJob).ToList();

public bool IsJobRegistered<T>() => allJobs.Any(j => j.Type == typeof(T));

public JobDefinition GetJobDefinition<T>() => allJobs.First(j => j.Type == typeof(T));

public JobDefinition? FindJobDefinition(Type type)
=> allJobs.FirstOrDefault(j => j.Type == type);

Expand Down
17 changes: 17 additions & 0 deletions tests/NCronJob.Tests/NCronJobIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ public async Task EachJobRunHasItsOwnScope()
storage.Guids.Distinct().Count().ShouldBe(storage.Guids.Count);
}

[Fact]
public async Task ExecuteAnInstantJobWithoutPreviousRegistration()
{
ServiceCollection.AddNCronJob();

var provider = CreateServiceProvider();
await provider.GetRequiredService<IHostedService>().StartAsync(CancellationToken);

provider.GetRequiredService<IInstantJobRegistry>().RunInstantJob<SimpleJob>();
linkdotnet marked this conversation as resolved.
Show resolved Hide resolved

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

var jobRegistry = provider.GetRequiredService<JobRegistry>();
jobRegistry.FindJobDefinition(typeof(SimpleJob)).ShouldBeNull();
}

[Fact]
public async Task ExecuteAnInstantJob()
{
Expand Down