diff --git a/CHANGELOG.md b/CHANGELOG.md index 3846159c..1011a3b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/src/NCronJob/Registry/IInstantJobRegistry.cs b/src/NCronJob/Registry/IInstantJobRegistry.cs index 3ddd03bb..fe7703ac 100644 --- a/src/NCronJob/Registry/IInstantJobRegistry.cs +++ b/src/NCronJob/Registry/IInstantJobRegistry.cs @@ -236,17 +236,14 @@ private void RunJob(DateTimeOffset startDate, object? parameter = null, bo { using (logger.BeginScope("Triggering RunScheduledJob:")) { - if (!jobRegistry.IsJobRegistered()) + 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); diff --git a/src/NCronJob/Registry/JobRegistry.cs b/src/NCronJob/Registry/JobRegistry.cs index 73f4fc97..dc1fdc6e 100644 --- a/src/NCronJob/Registry/JobRegistry.cs +++ b/src/NCronJob/Registry/JobRegistry.cs @@ -16,10 +16,6 @@ private readonly Dictionary> depe public IReadOnlyCollection GetAllOneTimeJobs() => allJobs.Where(c => c.IsStartupJob).ToList(); - public bool IsJobRegistered() => allJobs.Any(j => j.Type == typeof(T)); - - public JobDefinition GetJobDefinition() => allJobs.First(j => j.Type == typeof(T)); - public JobDefinition? FindJobDefinition(Type type) => allJobs.FirstOrDefault(j => j.Type == type); diff --git a/tests/NCronJob.Tests/NCronJobIntegrationTests.cs b/tests/NCronJob.Tests/NCronJobIntegrationTests.cs index aa7cef8a..65539ef0 100644 --- a/tests/NCronJob.Tests/NCronJobIntegrationTests.cs +++ b/tests/NCronJob.Tests/NCronJobIntegrationTests.cs @@ -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().StartAsync(CancellationToken); + + provider.GetRequiredService().RunInstantJob(); + + var jobFinished = await WaitForJobsOrTimeout(1); + jobFinished.ShouldBeTrue(); + + var jobRegistry = provider.GetRequiredService(); + jobRegistry.FindJobDefinition(typeof(SimpleJob)).ShouldBeNull(); + } + [Fact] public async Task ExecuteAnInstantJob() {