From bb460163c20b87a7c0dcadb64e0e88150924ac40 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Thu, 1 Dec 2011 22:26:06 -0800 Subject: [PATCH] Fixed job provider issue where deleted job classes would break scheduler. --- .../JobProviderTests/JobProviderFixture.cs | 31 +++++++----- NzbDrone.Core/Jobs/JobProvider.cs | 48 ++++++++----------- 2 files changed, 39 insertions(+), 40 deletions(-) diff --git a/NzbDrone.Core.Test/ProviderTests/JobProviderTests/JobProviderFixture.cs b/NzbDrone.Core.Test/ProviderTests/JobProviderTests/JobProviderFixture.cs index 5c55fc2c9..c132925e2 100644 --- a/NzbDrone.Core.Test/ProviderTests/JobProviderTests/JobProviderFixture.cs +++ b/NzbDrone.Core.Test/ProviderTests/JobProviderTests/JobProviderFixture.cs @@ -4,11 +4,13 @@ using System.Linq; using System; using System.Collections.Generic; using System.Threading; +using FizzWare.NBuilder; using FluentAssertions; using NUnit.Framework; using NzbDrone.Core.Jobs; using NzbDrone.Core.Model; using NzbDrone.Core.Providers.Jobs; +using NzbDrone.Core.Repository; using NzbDrone.Core.Test.Framework; using NzbDrone.Test.Common; using NzbDrone.Test.Common.AutoMoq; @@ -284,35 +286,38 @@ namespace NzbDrone.Core.Test.ProviderTests.JobProviderTests } [Test] - public void jobs_with_zero_interval_are_registered_as_disabled() + public void inti_should_removed_jobs_that_no_longer_exist() { - IList fakeJobs = new List { disabledJob }; + IList fakeJobs = new List { fakeJob }; Mocker.SetConstant(fakeJobs); - + + WithRealDb(); + var deletedJob = Builder.CreateNew().Build(); + Db.Insert(deletedJob); var jobProvider = Mocker.Resolve(); + + //Act jobProvider.Initialize(); //Assert - jobProvider.All().Should().HaveCount(1); - jobProvider.All().First().Enable.Should().BeFalse(); + var registeredJobs = Db.Fetch(); + registeredJobs.Should().HaveCount(1); + registeredJobs.Should().NotContain(c => c.Name == deletedJob.Name); + } [Test] - public void Get_Next_Execution_Time() + public void jobs_with_zero_interval_are_registered_as_disabled() { - IList BaseFakeJobs = new List { fakeJob }; - Mocker.SetConstant(BaseFakeJobs); + IList fakeJobs = new List { disabledJob }; + Mocker.SetConstant(fakeJobs); - //Act var jobProvider = Mocker.Resolve(); jobProvider.Initialize(); - jobProvider.QueueScheduled(); - WaitForQueue(); //Assert - var next = jobProvider.NextScheduledRun(typeof(FakeJob)); jobProvider.All().Should().HaveCount(1); - jobProvider.All().First().LastExecution.Should().Be(next.AddMinutes(-fakeJob.DefaultInterval)); + jobProvider.All().First().Enable.Should().BeFalse(); } [Test] diff --git a/NzbDrone.Core/Jobs/JobProvider.cs b/NzbDrone.Core/Jobs/JobProvider.cs index 88cb9d51c..f63fc997c 100644 --- a/NzbDrone.Core/Jobs/JobProvider.cs +++ b/NzbDrone.Core/Jobs/JobProvider.cs @@ -64,40 +64,45 @@ namespace NzbDrone.Core.Jobs } } - /// - /// Returns a list of all registered jobs - /// public virtual List All() { return _database.Fetch().ToList(); } - /// - /// Initializes jobs in the database using the IJob instances that are - /// registered using ninject - /// public virtual void Initialize() { - logger.Debug("Initializing jobs. Count {0}", _jobs.Count()); - var currentTimer = All(); + var currentJobs = All(); + logger.Debug("Initializing jobs. Available: {0} Existing:{1}", _jobs.Count(), currentJobs.Count); + + foreach (var currentJob in currentJobs) + { + if (!_jobs.Any(c => c.Name == currentJob.Name)) + { + logger.Debug("Removing job from database '{0}'", currentJob.Name); + _database.Delete(currentJob); + } + } - foreach (var timer in _jobs) + foreach (var job in _jobs) { - var timerProviderLocal = timer; - if (!currentTimer.Exists(c => c.TypeName == timerProviderLocal.GetType().ToString())) + var jobLocal = job; + if (!currentJobs.Exists(c => c.TypeName == jobLocal.GetType().ToString())) { var settings = new JobDefinition { - Enable = timerProviderLocal.DefaultInterval > 0, - TypeName = timer.GetType().ToString(), - Name = timerProviderLocal.Name, - Interval = timerProviderLocal.DefaultInterval, + Enable = jobLocal.DefaultInterval > 0, + TypeName = job.GetType().ToString(), + Name = jobLocal.Name, + Interval = jobLocal.DefaultInterval, LastExecution = DateTime.Now }; SaveDefinition(settings); } } + + + } /// @@ -141,17 +146,6 @@ namespace NzbDrone.Core.Jobs logger.Trace("{0} Scheduled tasks have been added to the queue", pendingJobTypes.Count); } - /// - /// Gets the next scheduled run time for a specific job - /// (Estimated due to schedule timer) - /// - /// DateTime of next scheduled job execution - public virtual DateTime NextScheduledRun(Type jobType) - { - var job = All().Where(t => t.TypeName == jobType.ToString()).Single(); - return job.LastExecution.AddMinutes(job.Interval); - } - public virtual void QueueJob(Type jobType, int targetId = 0, int secondaryTargetId = 0) { var queueItem = new JobQueueItem