From e7815010215ba9c36eae9270b653d195fbfbd184 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Wed, 23 Nov 2011 17:09:09 -0800 Subject: [PATCH] Added RecentBacklogSearchJob to fill missing episodes from the last 30 days, runs nightly. --- .../JobTests/RecentBacklogSearchJobTest.cs | 83 +++++++++++++++++++ NzbDrone.Core.Test/NzbDrone.Core.Test.csproj | 1 + NzbDrone.Core/CentralDispatch.cs | 1 + NzbDrone.Core/NzbDrone.Core.csproj | 1 + .../Providers/Jobs/RecentBacklogSearchJob.cs | 51 ++++++++++++ 5 files changed, 137 insertions(+) create mode 100644 NzbDrone.Core.Test/JobTests/RecentBacklogSearchJobTest.cs create mode 100644 NzbDrone.Core/Providers/Jobs/RecentBacklogSearchJob.cs diff --git a/NzbDrone.Core.Test/JobTests/RecentBacklogSearchJobTest.cs b/NzbDrone.Core.Test/JobTests/RecentBacklogSearchJobTest.cs new file mode 100644 index 000000000..3a21b6ff4 --- /dev/null +++ b/NzbDrone.Core.Test/JobTests/RecentBacklogSearchJobTest.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +using FizzWare.NBuilder; +using Moq; +using NUnit.Framework; +using NzbDrone.Core.Model.Notification; +using NzbDrone.Core.Providers; +using NzbDrone.Core.Providers.Jobs; +using NzbDrone.Core.Repository; +using NzbDrone.Core.Test.Framework; +using NzbDrone.Test.Common.AutoMoq; + +namespace NzbDrone.Core.Test.JobTests +{ + [TestFixture] + public class RecentBacklogSearchJobTest : CoreTest + { + [SetUp] + public void Setup() + { + + } + + [Test] + public void no_missing_epsiodes_should_not_trigger_any_search() + { + //Setup + var episodes = new List(); + + Mocker.GetMock() + .Setup(s => s.EpisodesWithoutFiles(true)).Returns(episodes); + + //Act + Mocker.Resolve().Start(MockNotification, 0, 0); + + //Assert + Mocker.GetMock().Verify(c => c.Start(MockNotification, It.IsAny(), 0), + Times.Never()); + } + + [Test] + public void should_only_process_missing_episodes_from_the_last_30_days() + { + //Setup + var episodes = Builder.CreateListOfSize(50) + .TheFirst(5) + .With(e => e.AirDate = DateTime.Today) + .TheNext(5) + .With(e => e.AirDate = DateTime.Today.AddDays(-1)) //Today + .TheNext(5) + .With(e => e.AirDate = DateTime.Today.AddDays(-5)) //Yeserday + .TheNext(5) + .With(e => e.AirDate = DateTime.Today.AddDays(-10)) + .TheNext(5) + .With(e => e.AirDate = DateTime.Today.AddDays(-15)) + .TheNext(5) + .With(e => e.AirDate = DateTime.Today.AddDays(-20)) + .TheNext(5) + .With(e => e.AirDate = DateTime.Today.AddDays(-25)) + .TheNext(5) + .With(e => e.AirDate = DateTime.Today.AddDays(-30)) + .TheNext(5) + .With(e => e.AirDate = DateTime.Today.AddDays(-31)) //31 Days + .TheNext(5) + .With(e => e.AirDate = DateTime.Today.AddDays(-35)) + .Build(); + + Mocker.GetMock() + .Setup(s => s.EpisodesWithoutFiles(true)).Returns(episodes); + + Mocker.GetMock().Setup(c => c.Start(It.IsAny(), It.IsAny(), 0)); + + //Act + Mocker.Resolve().Start(MockNotification, 0, 0); + + //Assert + Mocker.GetMock().Verify(c => c.Start(It.IsAny(), It.IsAny(), 0), + Times.Exactly(40)); + } + } +} diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 1645b0f7d..ad6b33b72 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -96,6 +96,7 @@ + diff --git a/NzbDrone.Core/CentralDispatch.cs b/NzbDrone.Core/CentralDispatch.cs index 30e4686cc..8830485e0 100644 --- a/NzbDrone.Core/CentralDispatch.cs +++ b/NzbDrone.Core/CentralDispatch.cs @@ -95,6 +95,7 @@ namespace NzbDrone.Core Kernel.Bind().To().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); + Kernel.Bind().To().InSingletonScope(); Kernel.Get().Initialize(); Kernel.Get().StartTimer(30); diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 794da4ac6..f4f155d70 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -235,6 +235,7 @@ + diff --git a/NzbDrone.Core/Providers/Jobs/RecentBacklogSearchJob.cs b/NzbDrone.Core/Providers/Jobs/RecentBacklogSearchJob.cs new file mode 100644 index 000000000..ae0e13746 --- /dev/null +++ b/NzbDrone.Core/Providers/Jobs/RecentBacklogSearchJob.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using NLog; +using NzbDrone.Core.Model; +using NzbDrone.Core.Model.Notification; +using NzbDrone.Core.Model.Search; +using NzbDrone.Core.Repository; + +namespace NzbDrone.Core.Providers.Jobs +{ + public class RecentBacklogSearchJob : IJob + { + private readonly EpisodeProvider _episodeProvider; + private readonly EpisodeSearchJob _episodeSearchJob; + private readonly SeasonSearchJob _seasonSearchJob; + + private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); + + public RecentBacklogSearchJob(EpisodeProvider episodeProvider, EpisodeSearchJob episodeSearchJob, + SeasonSearchJob seasonSearchJob) + { + _episodeProvider = episodeProvider; + _episodeSearchJob = episodeSearchJob; + _seasonSearchJob = seasonSearchJob; + } + + public string Name + { + get { return "Recent Backlog Search"; } + } + + public int DefaultInterval + { + get { return 1440; } + } + + public void Start(ProgressNotification notification, int targetId, int secondaryTargetId) + { + //Get episodes that are considered missing and aired in the last 30 days + var missingEpisodes = _episodeProvider.EpisodesWithoutFiles(true).Where(e => e.AirDate >= DateTime.Today.AddDays(-30)); + + Logger.Debug("Processing missing episodes from the last 30 days"); + //Process the list of remaining episodes, 1 by 1 + foreach (var episode in missingEpisodes) + { + _episodeSearchJob.Start(notification, episode.EpisodeId, 0); + } + } + } +} \ No newline at end of file