From fa1b287d589b09f3aea8915e9dbd57ebeba6dd32 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Thu, 21 Apr 2011 22:46:47 -0700 Subject: [PATCH] JobProvider now stores last execution and success JobProvider is now wired working Job view ui is updated --- NzbDrone.Core.Test/JobProviderTest.cs | 32 +++++++++- NzbDrone.Core/CentralDispatch.cs | 5 +- NzbDrone.Core/Providers/Jobs/JobProvider.cs | 14 +++-- NzbDrone.Core/WebTimer.cs | 9 ++- NzbDrone.Web/Controllers/HistoryController.cs | 1 + .../{TimersController.cs => JobController.cs} | 4 +- NzbDrone.Web/NzbDrone.Web.csproj | 4 +- NzbDrone.Web/Views/Job/index.cshtml | 9 +++ NzbDrone.Web/Views/Timers/index.cshtml | 59 ------------------- 9 files changed, 65 insertions(+), 72 deletions(-) rename NzbDrone.Web/Controllers/{TimersController.cs => JobController.cs} (79%) create mode 100644 NzbDrone.Web/Views/Job/index.cshtml delete mode 100644 NzbDrone.Web/Views/Timers/index.cshtml diff --git a/NzbDrone.Core.Test/JobProviderTest.cs b/NzbDrone.Core.Test/JobProviderTest.cs index f1da4e25e..f24719093 100644 --- a/NzbDrone.Core.Test/JobProviderTest.cs +++ b/NzbDrone.Core.Test/JobProviderTest.cs @@ -13,7 +13,7 @@ namespace NzbDrone.Core.Test public class JobProviderTest { [Test] - public void Run_Jobs() + public void Run_Jobs_Updates_Last_Execution() { IEnumerable fakeTimers = new List { new FakeJob() }; @@ -22,10 +22,38 @@ namespace NzbDrone.Core.Test mocker.SetConstant(MockLib.GetEmptyRepository()); mocker.SetConstant(fakeTimers); + //Act var timerProvider = mocker.Resolve(); timerProvider.Initialize(); timerProvider.RunScheduled(); + //Assert + var settings = timerProvider.All(); + Assert.IsNotEmpty(settings); + Assert.AreNotEqual(DateTime.MinValue, settings[0].LastExecution); + Assert.IsTrue(settings[0].Success); + } + + [Test] + public void Run_Jobs_Updates_Last_Execution_Mark_as_unsuccesful() + { + + IEnumerable fakeTimers = new List { new BrokenJob() }; + var mocker = new AutoMoqer(); + + mocker.SetConstant(MockLib.GetEmptyRepository()); + mocker.SetConstant(fakeTimers); + + //Act + var timerProvider = mocker.Resolve(); + timerProvider.Initialize(); + timerProvider.RunScheduled(); + + //Assert + var settings = timerProvider.All(); + Assert.IsNotEmpty(settings); + Assert.AreNotEqual(DateTime.MinValue, settings[0].LastExecution); + Assert.IsFalse(settings[0].Success); } @@ -222,7 +250,7 @@ namespace NzbDrone.Core.Test public void Start(ProgressNotification notification, int targetId) { - throw new NotImplementedException(); + } } diff --git a/NzbDrone.Core/CentralDispatch.cs b/NzbDrone.Core/CentralDispatch.cs index e227a57b9..cacd861ae 100644 --- a/NzbDrone.Core/CentralDispatch.cs +++ b/NzbDrone.Core/CentralDispatch.cs @@ -98,6 +98,7 @@ namespace NzbDrone.Core _kernel.Bind().ToSelf().InSingletonScope(); _kernel.Bind().ToSelf().InSingletonScope(); _kernel.Bind().ToSelf().InSingletonScope(); + _kernel.Bind().ToSelf().InSingletonScope(); _kernel.Bind().ToMethod( c => new SimpleRepository(dbProvider, SimpleRepositoryOptions.RunMigrations)).InSingletonScope(); @@ -130,9 +131,9 @@ namespace NzbDrone.Core _kernel.Bind().To().InTransientScope(); _kernel.Bind().To().InTransientScope(); _kernel.Bind().To().InTransientScope(); + _kernel.Get().Initialize(); - - new WebTimer().StartTimer(1); + _kernel.Get().StartTimer(30); } diff --git a/NzbDrone.Core/Providers/Jobs/JobProvider.cs b/NzbDrone.Core/Providers/Jobs/JobProvider.cs index facba16d2..f2ed6949a 100644 --- a/NzbDrone.Core/Providers/Jobs/JobProvider.cs +++ b/NzbDrone.Core/Providers/Jobs/JobProvider.cs @@ -87,8 +87,6 @@ namespace NzbDrone.Core.Providers.Jobs foreach (var pendingTimer in pendingJobs) { - Logger.Info("Attempting to start job [{0}]. Last executing {1}", pendingTimer.Name, - pendingTimer.LastExecution); var timerClass = _jobs.Where(t => t.GetType().ToString() == pendingTimer.TypeName).FirstOrDefault(); Execute(timerClass.GetType(), 0); } @@ -162,12 +160,16 @@ namespace NzbDrone.Core.Providers.Jobs var timerClass = _jobs.Where(t => t.GetType() == jobType).FirstOrDefault(); if (timerClass == null) { - Logger.Error("Unable to locate implantation for [{0}]. Make sure its properly registered.", jobType.ToString()); + Logger.Error("Unable to locate implantation for '{0}'. Make sure its properly registered.", jobType.ToString()); return; } + var settings = All().Where(j => j.TypeName == jobType.ToString()).FirstOrDefault(); + try { + Logger.Info("Starting job '{0}'. Last execution {1}", settings.Name, settings.LastExecution); + settings.LastExecution = DateTime.Now; var sw = Stopwatch.StartNew(); using (_notification = new ProgressNotification(timerClass.Name)) { @@ -175,13 +177,17 @@ namespace NzbDrone.Core.Providers.Jobs timerClass.Start(_notification, targetId); _notification.Status = ProgressNotificationStatus.Completed; } + settings.Success = true; sw.Stop(); - Logger.Info("timer [{0}] finished executing successfully. Duration {1}", timerClass.Name, sw.Elapsed.ToString()); + Logger.Info("Job '{0}' successfully completed in {1} seconds", timerClass.Name, sw.Elapsed.Minutes, sw.Elapsed.Seconds); } catch (Exception e) { + settings.Success = false; Logger.ErrorException("An error has occurred while executing timer job " + timerClass.Name, e); } + + SaveSettings(settings); } /// diff --git a/NzbDrone.Core/WebTimer.cs b/NzbDrone.Core/WebTimer.cs index 1c9ab69d4..3058e037d 100644 --- a/NzbDrone.Core/WebTimer.cs +++ b/NzbDrone.Core/WebTimer.cs @@ -5,15 +5,22 @@ using System.Text; using System.Web; using System.Web.Caching; using NLog; +using NzbDrone.Core.Providers.Jobs; namespace NzbDrone.Core { class WebTimer { + private readonly JobProvider _jobProvider; private static CacheItemRemovedCallback _onCacheRemove; private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); + public WebTimer(JobProvider jobProvider) + { + _jobProvider = jobProvider; + } + public void StartTimer(int secondInterval) { _onCacheRemove = new CacheItemRemovedCallback(DoWork); @@ -26,7 +33,7 @@ namespace NzbDrone.Core public void DoWork(string k, object v, CacheItemRemovedReason r) { - Logger.Info("Tick!"); + _jobProvider.RunScheduled(); StartTimer(Convert.ToInt32(v)); } } diff --git a/NzbDrone.Web/Controllers/HistoryController.cs b/NzbDrone.Web/Controllers/HistoryController.cs index 02977f0bf..7af54721b 100644 --- a/NzbDrone.Web/Controllers/HistoryController.cs +++ b/NzbDrone.Web/Controllers/HistoryController.cs @@ -40,6 +40,7 @@ namespace NzbDrone.Web.Controllers { //TODO: possible subsonic bug, IQuarible causes some issues so ToList() is called + //https://github.com/subsonic/SubSonic-3.0/issues/263 var history = _historyProvider.AllItems().ToList().Select(h => new HistoryModel { diff --git a/NzbDrone.Web/Controllers/TimersController.cs b/NzbDrone.Web/Controllers/JobController.cs similarity index 79% rename from NzbDrone.Web/Controllers/TimersController.cs rename to NzbDrone.Web/Controllers/JobController.cs index 20e9a3fd1..3f14d0803 100644 --- a/NzbDrone.Web/Controllers/TimersController.cs +++ b/NzbDrone.Web/Controllers/JobController.cs @@ -7,11 +7,11 @@ using NzbDrone.Core.Providers.Jobs; namespace NzbDrone.Web.Controllers { - public class TimersController : Controller + public class JobController : Controller { private readonly JobProvider _jobProvider; - public TimersController(JobProvider jobProvider) + public JobController(JobProvider jobProvider) { _jobProvider = jobProvider; } diff --git a/NzbDrone.Web/NzbDrone.Web.csproj b/NzbDrone.Web/NzbDrone.Web.csproj index fa91d0073..03a029a06 100644 --- a/NzbDrone.Web/NzbDrone.Web.csproj +++ b/NzbDrone.Web/NzbDrone.Web.csproj @@ -219,7 +219,7 @@ - + Global.asax @@ -660,7 +660,7 @@ - + diff --git a/NzbDrone.Web/Views/Job/index.cshtml b/NzbDrone.Web/Views/Job/index.cshtml new file mode 100644 index 000000000..ead69dd53 --- /dev/null +++ b/NzbDrone.Web/Views/Job/index.cshtml @@ -0,0 +1,9 @@ +@model IEnumerable +@section TitleContent{ +Jobs +} +@section MainContent{ + @{Html.Telerik().Grid(Model).Name("Grid") + .TableHtmlAttributes(new { @class = "Grid" }) + .Render();} +} diff --git a/NzbDrone.Web/Views/Timers/index.cshtml b/NzbDrone.Web/Views/Timers/index.cshtml deleted file mode 100644 index d8d7ab783..000000000 --- a/NzbDrone.Web/Views/Timers/index.cshtml +++ /dev/null @@ -1,59 +0,0 @@ -@model IEnumerable -@{ - Layout = null; -} - - - - Timers - - - - - - - - - - - - - @foreach (var item in Model) - { - - - - - - - - - } -
- - Enable - - TypeName - - Name - - Interval - - LastExecution - - Success -
- @item.Enable - - @item.TypeName - - @item.Name - - @item.Interval - - @String.Format("{0:g}", item.LastExecution) - - @item.Success -
- -