From ba9536b4533731116976fbe299f40a8743cd38cf Mon Sep 17 00:00:00 2001 From: nulltoken Date: Mon, 4 Nov 2024 17:09:57 +0100 Subject: [PATCH 1/3] Drop unused method --- src/NCronJob/Registry/JobRegistry.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/NCronJob/Registry/JobRegistry.cs b/src/NCronJob/Registry/JobRegistry.cs index 73f4fc97..38ff163a 100644 --- a/src/NCronJob/Registry/JobRegistry.cs +++ b/src/NCronJob/Registry/JobRegistry.cs @@ -18,8 +18,6 @@ private readonly Dictionary> depe 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); From faea98c1eefb853f8c4da7132d3c9f504deb3283 Mon Sep 17 00:00:00 2001 From: nulltoken Date: Mon, 4 Nov 2024 17:09:17 +0100 Subject: [PATCH 2/3] Prevent InstantJobRegistry from altering the content of JobRegistry --- CHANGELOG.md | 4 ++++ src/NCronJob/Registry/IInstantJobRegistry.cs | 11 ++++------- src/NCronJob/Registry/JobRegistry.cs | 2 -- .../NCronJob.Tests/NCronJobIntegrationTests.cs | 17 +++++++++++++++++ 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3846159c..c97c430b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ 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 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 38ff163a..dc1fdc6e 100644 --- a/src/NCronJob/Registry/JobRegistry.cs +++ b/src/NCronJob/Registry/JobRegistry.cs @@ -16,8 +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? 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() { From 261de0f5a826258c5c72471e692ca835147294dc Mon Sep 17 00:00:00 2001 From: nulltoken Date: Mon, 4 Nov 2024 18:41:46 +0100 Subject: [PATCH 3/3] Fix wrong link in CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c97c430b..1011a3b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ All notable changes to **NCronJob** will be documented in this file. The project ### 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).