From 77c82df482c8023d597f2dff50f952aaf391a917 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sun, 10 Jul 2011 17:03:01 -0700 Subject: [PATCH] Priority/JobProvider tweaks --- NzbDrone.Core.Test/JobProviderTest.cs | 11 +++- NzbDrone.Core/Providers/Jobs/JobProvider.cs | 67 ++++++++++----------- NzbDrone/Program.cs | 28 +++++++-- 3 files changed, 63 insertions(+), 43 deletions(-) diff --git a/NzbDrone.Core.Test/JobProviderTest.cs b/NzbDrone.Core.Test/JobProviderTest.cs index d46dfeb1a..8bf8cf543 100644 --- a/NzbDrone.Core.Test/JobProviderTest.cs +++ b/NzbDrone.Core.Test/JobProviderTest.cs @@ -98,6 +98,7 @@ namespace NzbDrone.Core.Test firstRun.Should().BeTrue(); secondRun.Should().BeTrue(); + JobProvider.Queue.Should().BeEmpty(); } [Test] @@ -113,12 +114,13 @@ namespace NzbDrone.Core.Test var timerProvider = mocker.Resolve(); timerProvider.Initialize(); - var firstRun = timerProvider.QueueJob(typeof(SlowJob), 1); - var secondRun = timerProvider.QueueJob(typeof(SlowJob), 2); - var third = timerProvider.QueueJob(typeof(SlowJob), 3); + timerProvider.QueueJob(typeof(SlowJob), 1); + timerProvider.QueueJob(typeof(SlowJob), 2); + timerProvider.QueueJob(typeof(SlowJob), 3); Thread.Sleep(10000); + JobProvider.Queue.Should().BeEmpty(); //Asserts are done in ExceptionVerification } @@ -143,6 +145,7 @@ namespace NzbDrone.Core.Test firstRun.Should().BeTrue(); secondRun.Should().BeTrue(); Thread.Sleep(2000); + JobProvider.Queue.Should().BeEmpty(); ExceptionVerification.ExcpectedErrors(2); } @@ -206,6 +209,7 @@ namespace NzbDrone.Core.Test Thread.Sleep(5000); Assert.AreEqual(1, slowJob.ExexutionCount); + JobProvider.Queue.Should().BeEmpty(); } @@ -362,6 +366,7 @@ namespace NzbDrone.Core.Test var settings = timerProvider.All(); settings.Should().NotBeEmpty(); settings[0].LastExecution.Should().HaveYear(2000); + JobProvider.Queue.Should().BeEmpty(); } [Test] diff --git a/NzbDrone.Core/Providers/Jobs/JobProvider.cs b/NzbDrone.Core/Providers/Jobs/JobProvider.cs index 7e83f720e..068b21f96 100644 --- a/NzbDrone.Core/Providers/Jobs/JobProvider.cs +++ b/NzbDrone.Core/Providers/Jobs/JobProvider.cs @@ -21,7 +21,9 @@ namespace NzbDrone.Core.Providers.Jobs private static readonly object ExecutionLock = new object(); private Thread _jobThread; private static bool _isRunning; - private static readonly List> Queue = new List>(); + public static readonly List> Queue = new List>(); + + private ProgressNotification _notification; @@ -35,7 +37,6 @@ namespace NzbDrone.Core.Providers.Jobs public JobProvider() { } - /// /// Returns a list of all registered jobs /// @@ -158,7 +159,7 @@ namespace NzbDrone.Core.Providers.Jobs } }; - _jobThread = new Thread(starter) { Name = "JobQueueThread", Priority = ThreadPriority.BelowNormal }; + _jobThread = new Thread(starter) { Name = "JobQueueThread" }; _jobThread.Start(); } @@ -176,45 +177,41 @@ namespace NzbDrone.Core.Providers.Jobs /// private void ProcessQueue() { - Tuple job = null; - - try + do { - lock (Queue) + Tuple job = null; + + try { - if (Queue.Count != 0) + lock (Queue) { - job = Queue[0]; + if (Queue.Count != 0) + { + job = Queue.First(); + } + } + + if (job != null) + { + Execute(job.Item1, job.Item2); + } + + } + catch (Exception e) + { + Logger.FatalException("An error has occurred while processing queued job.", e); + } + finally + { + if (job != null) + { + Queue.Remove(job); } } - if (job != null) - { - Execute(job.Item1, job.Item2); - } + } while (Queue.Count != 0); - } - catch (Exception e) - { - Logger.FatalException("An error has occurred while processing queued job.", e); - } - finally - { - if (job != null) - { - Queue.Remove(job); - } - } - - //Try to find next job is last run found a job. - if (job != null) - { - ProcessQueue(); - } - else - { - Logger.Trace("Finished processing jobs in the queue."); - } + Logger.Trace("Finished processing jobs in the queue."); return; } diff --git a/NzbDrone/Program.cs b/NzbDrone/Program.cs index da4abd270..73c65a6a3 100644 --- a/NzbDrone/Program.cs +++ b/NzbDrone/Program.cs @@ -19,11 +19,8 @@ namespace NzbDrone Thread.CurrentThread.Name = "Host"; Process currentProcess = Process.GetCurrentProcess(); - if (currentProcess.PriorityClass < ProcessPriorityClass.Normal) - { - Logger.Info("Promoting process priority from {0} to {1}", currentProcess.PriorityClass, ProcessPriorityClass.Normal); - currentProcess.PriorityClass = ProcessPriorityClass.Normal; - } + + FixPriorities(); currentProcess.EnableRaisingEvents = true; currentProcess.Exited += ProgramExited; @@ -38,6 +35,8 @@ namespace NzbDrone #if DEBUG Attach(); #endif + FixPriorities(); + if (Environment.UserInteractive) { try @@ -64,6 +63,25 @@ namespace NzbDrone Console.ReadLine(); } + private static void FixPriorities() + { + Process currentProcess = Process.GetCurrentProcess(); + if (currentProcess.PriorityClass < ProcessPriorityClass.Normal) + { + Logger.Info("Promoting process priority from {0} to {1}", currentProcess.PriorityClass, + ProcessPriorityClass.Normal); + currentProcess.PriorityClass = ProcessPriorityClass.Normal; + } + + + if (IISController.IISProcess!=null && IISController.IISProcess.PriorityClass < ProcessPriorityClass.Normal) + { + Logger.Info("Promoting process priority from {0} to {1}", IISController.IISProcess.PriorityClass, + ProcessPriorityClass.Normal); + IISController.IISProcess.PriorityClass = ProcessPriorityClass.Normal; + } + } + #if DEBUG private static void Attach() {