Skip to content

Commit

Permalink
Merge pull request #131 from NCronJob-Dev/ntk/instantregistry_does_no…
Browse files Browse the repository at this point in the history
…t_persist

Prevent InstantJobRegistry from altering the content of JobRegistry
  • Loading branch information
nulltoken authored Nov 4, 2024
2 parents db94b57 + 261de0f commit 9bbb452
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
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>();

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

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

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

0 comments on commit 9bbb452

Please sign in to comment.