From 4fe1d7e6f799c86dff846a41fb38d4b1cdc83132 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sun, 26 Feb 2012 14:35:45 -0800 Subject: [PATCH] Jobs added to queue have higher priority than scheduler jobs. --- .../JobProviderTests/JobProviderFixture.cs | 16 ++++++++++++++++ NzbDrone.Core/Jobs/JobProvider.cs | 9 +++++---- NzbDrone.Core/Model/JobQueueItem.cs | 8 ++++++++ NzbDrone.Core/Model/Sabnzbd/SabCategoryModel.cs | 4 +--- NzbDrone.Web/Controllers/UpdateController.cs | 1 - 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/NzbDrone.Core.Test/ProviderTests/JobProviderTests/JobProviderFixture.cs b/NzbDrone.Core.Test/ProviderTests/JobProviderTests/JobProviderFixture.cs index 8738c952c..d3cd84548 100644 --- a/NzbDrone.Core.Test/ProviderTests/JobProviderTests/JobProviderFixture.cs +++ b/NzbDrone.Core.Test/ProviderTests/JobProviderTests/JobProviderFixture.cs @@ -490,6 +490,22 @@ namespace NzbDrone.Core.Test.ProviderTests.JobProviderTests ExceptionVerification.ExpectedErrors(1); } + [Test] + public void scheduled_job_should_have_scheduler_as_source() + { + IList BaseFakeJobs = new List { slowJob, fakeJob}; + Mocker.SetConstant(BaseFakeJobs); + + var jobProvider = Mocker.Resolve(); + jobProvider.Initialize(); + ResetLastExecution(); + jobProvider.QueueScheduled(); + + jobProvider.Queue.Should().OnlyContain(c => c.Source == JobQueueItem.JobSourceType.Scheduler); + + WaitForQueue(); + } + } diff --git a/NzbDrone.Core/Jobs/JobProvider.cs b/NzbDrone.Core/Jobs/JobProvider.cs index 21923db28..22712356a 100644 --- a/NzbDrone.Core/Jobs/JobProvider.cs +++ b/NzbDrone.Core/Jobs/JobProvider.cs @@ -139,17 +139,18 @@ namespace NzbDrone.Core.Jobs ).Select(c => _jobs.Where(t => t.GetType().ToString() == c.TypeName).Single().GetType()).ToList(); - pendingJobTypes.ForEach(jobType => QueueJob(jobType)); + pendingJobTypes.ForEach(jobType => QueueJob(jobType, source: JobQueueItem.JobSourceType.Scheduler)); logger.Trace("{0} Scheduled tasks have been added to the queue", pendingJobTypes.Count); } - public virtual void QueueJob(Type jobType, int targetId = 0, int secondaryTargetId = 0) + public virtual void QueueJob(Type jobType, int targetId = 0, int secondaryTargetId = 0, JobQueueItem.JobSourceType source = JobQueueItem.JobSourceType.User) { var queueItem = new JobQueueItem { JobType = jobType, TargetId = targetId, - SecondaryTargetId = secondaryTargetId + SecondaryTargetId = secondaryTargetId, + Source = source }; logger.Debug("Attempting to queue {0}", queueItem); @@ -211,7 +212,7 @@ namespace NzbDrone.Core.Jobs { if (Queue.Count != 0) { - job = Queue.First(); + job = Queue.OrderByDescending(c=>c.Source).First(); logger.Trace("Popping {0} from the queue.", job); Queue.Remove(job); } diff --git a/NzbDrone.Core/Model/JobQueueItem.cs b/NzbDrone.Core/Model/JobQueueItem.cs index c2b983a07..f8332d801 100644 --- a/NzbDrone.Core/Model/JobQueueItem.cs +++ b/NzbDrone.Core/Model/JobQueueItem.cs @@ -8,6 +8,8 @@ namespace NzbDrone.Core.Model public int TargetId { get; set; } public int SecondaryTargetId { get; set; } + public JobSourceType Source { get; set; } + public bool Equals(JobQueueItem other) { return (JobType == other.JobType && TargetId == other.TargetId @@ -18,5 +20,11 @@ namespace NzbDrone.Core.Model { return string.Format("[{0}({1}, {2})]", JobType.Name, TargetId, SecondaryTargetId); } + + public enum JobSourceType + { + User, + Scheduler + } } } diff --git a/NzbDrone.Core/Model/Sabnzbd/SabCategoryModel.cs b/NzbDrone.Core/Model/Sabnzbd/SabCategoryModel.cs index 9ddf34ca2..0b8bd1cbc 100644 --- a/NzbDrone.Core/Model/Sabnzbd/SabCategoryModel.cs +++ b/NzbDrone.Core/Model/Sabnzbd/SabCategoryModel.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Collections.Generic; namespace NzbDrone.Core.Model.Sabnzbd { diff --git a/NzbDrone.Web/Controllers/UpdateController.cs b/NzbDrone.Web/Controllers/UpdateController.cs index e4bbc4d47..8a2982bd3 100644 --- a/NzbDrone.Web/Controllers/UpdateController.cs +++ b/NzbDrone.Web/Controllers/UpdateController.cs @@ -1,5 +1,4 @@ using System; -using System.IO; using System.Linq; using System.Web.Mvc; using NzbDrone.Common;