From 77eedf448e0618622f86bc00fe8a4c9dc56af476 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sat, 2 Mar 2013 12:14:18 -0800 Subject: [PATCH] fixed jobs running all the time. --- NzbDrone.Api/Episodes/EpisodeResource.cs | 4 +- .../JobTests/JobRepositoryFixture.cs | 79 +++++++++++++------ .../ProviderTests/TvDbProviderTest.cs | 3 + NzbDrone.Core/Jobs/JobRepository.cs | 2 +- NzbDrone.Core/Tv/EpisodeService.cs | 2 + 5 files changed, 63 insertions(+), 27 deletions(-) diff --git a/NzbDrone.Api/Episodes/EpisodeResource.cs b/NzbDrone.Api/Episodes/EpisodeResource.cs index 95bf3b2e7..b1ff70ef7 100644 --- a/NzbDrone.Api/Episodes/EpisodeResource.cs +++ b/NzbDrone.Api/Episodes/EpisodeResource.cs @@ -1,8 +1,6 @@ using System; -using System.Collections.Generic; using System.Linq; -using System.Text; -using NzbDrone.Core.Tv; +using NzbDrone.Core.MediaFiles; namespace NzbDrone.Api.Episodes { diff --git a/NzbDrone.Core.Test/JobTests/JobRepositoryFixture.cs b/NzbDrone.Core.Test/JobTests/JobRepositoryFixture.cs index 06d2abfc8..7d0685c66 100644 --- a/NzbDrone.Core.Test/JobTests/JobRepositoryFixture.cs +++ b/NzbDrone.Core.Test/JobTests/JobRepositoryFixture.cs @@ -41,13 +41,13 @@ namespace NzbDrone.Core.Test.JobTests Subject.Init(); Storage.All().Should().HaveCount(1); - Storage.All().ToList()[0].Interval.Should().Be((Int32)_fakeJob.DefaultInterval.TotalMinutes); - Storage.All().ToList()[0].Name.Should().Be(_fakeJob.Name); - Storage.All().ToList()[0].TypeName.Should().Be(_fakeJob.GetType().ToString()); - Storage.All().ToList()[0].LastExecution.Should().HaveYear(DateTime.Now.Year); - Storage.All().ToList()[0].LastExecution.Should().HaveMonth(DateTime.Now.Month); - Storage.All().ToList()[0].LastExecution.Should().HaveDay(DateTime.Today.Day); - Storage.All().ToList()[0].Enable.Should().BeTrue(); + StoredModel.Interval.Should().Be((Int32)_fakeJob.DefaultInterval.TotalMinutes); + StoredModel.Name.Should().Be(_fakeJob.Name); + StoredModel.TypeName.Should().Be(_fakeJob.GetType().ToString()); + StoredModel.LastExecution.Should().HaveYear(DateTime.Now.Year); + StoredModel.LastExecution.Should().HaveMonth(DateTime.Now.Month); + StoredModel.LastExecution.Should().HaveDay(DateTime.Today.Day); + StoredModel.Enable.Should().BeTrue(); } [Test] @@ -65,9 +65,8 @@ namespace NzbDrone.Core.Test.JobTests Db.Insert(deletedJob); Subject.Init(); - var registeredJobs = Storage.All(); - registeredJobs.Should().HaveCount(1); - registeredJobs.Should().NotContain(c => c.TypeName == deletedJob.TypeName); + AllStoredModels.Should().HaveCount(1); + AllStoredModels.Should().NotContain(c => c.TypeName == deletedJob.TypeName); } [Test] @@ -88,10 +87,8 @@ namespace NzbDrone.Core.Test.JobTests Subject.Init(); - - var registeredJobs = Storage.All(); - registeredJobs.Should().HaveCount(1); - registeredJobs.Should().NotContain(c => c.TypeName == deletedJob.TypeName); + AllStoredModels.Should().HaveCount(1); + AllStoredModels.Should().NotContain(c => c.TypeName == deletedJob.TypeName); } [Test] @@ -118,15 +115,13 @@ namespace NzbDrone.Core.Test.JobTests Subject.Init(); - var registeredJobs = Storage.All(); - registeredJobs.Should().HaveCount(1); - registeredJobs.First().TypeName.Should().Be(newJob.GetType().FullName); - registeredJobs.First().Name.Should().Be(newJob.Name); - registeredJobs.First().Interval.Should().Be((int)newJob.DefaultInterval.TotalMinutes); - - registeredJobs.First().Enable.Should().Be(true); - registeredJobs.First().Success.Should().Be(oldJob.Success); - registeredJobs.First().LastExecution.Should().Be(oldJob.LastExecution); + AllStoredModels.Should().HaveCount(1); + StoredModel.TypeName.Should().Be(newJob.GetType().FullName); + StoredModel.Name.Should().Be(newJob.Name); + StoredModel.Interval.Should().Be((int)newJob.DefaultInterval.TotalMinutes); + StoredModel.Enable.Should().Be(true); + StoredModel.Success.Should().Be(oldJob.Success); + StoredModel.LastExecution.Should().Be(oldJob.LastExecution); } [Test] @@ -142,6 +137,44 @@ namespace NzbDrone.Core.Test.JobTests Storage.All().First().Enable.Should().BeFalse(); } + + [Test] + public void pending_job_should_get_jobs_that_have_matured() + { + var oldJob = Builder.CreateNew() + .With(c => c.Id = 0) + .With(c => c.Interval = 1) + .With(c => c.Enable = true) + .With(c => c.Success = true) + .With(c => c.LastExecution = DateTime.Now.AddMinutes(-5)) + .Build(); + + + Storage.Insert(oldJob); + + + Subject.GetPendingJobs().Should().HaveCount(1); + } + + + [Test] + public void pending_job_should_not_get_jobs_that_havent_matured() + { + var recent = Builder.CreateNew() + .With(c => c.Id = 0) + .With(c => c.Interval = 60) + .With(c => c.Enable = true) + .With(c => c.Success = true) + .With(c => c.LastExecution = DateTime.Now.AddMinutes(-5)) + .Build(); + + + Storage.Insert(recent); + + + Subject.GetPendingJobs().Should().BeEmpty(); + } + /* [Test] public void disabled_jobs_arent_run_by_scheduler() { diff --git a/NzbDrone.Core.Test/ProviderTests/TvDbProviderTest.cs b/NzbDrone.Core.Test/ProviderTests/TvDbProviderTest.cs index ed95d87ee..fb45964db 100644 --- a/NzbDrone.Core.Test/ProviderTests/TvDbProviderTest.cs +++ b/NzbDrone.Core.Test/ProviderTests/TvDbProviderTest.cs @@ -75,6 +75,9 @@ namespace NzbDrone.Core.Test.ProviderTests result.GroupBy(e => e.SeasonNumber.ToString("000") + e.EpisodeNumber.ToString("000")) .Max(e => e.Count()).Should().Be(1); + + result.Select(c => c.TvDbEpisodeId).Should().OnlyHaveUniqueItems(); + } } } \ No newline at end of file diff --git a/NzbDrone.Core/Jobs/JobRepository.cs b/NzbDrone.Core/Jobs/JobRepository.cs index 4878ef3af..1286454fa 100644 --- a/NzbDrone.Core/Jobs/JobRepository.cs +++ b/NzbDrone.Core/Jobs/JobRepository.cs @@ -33,7 +33,7 @@ namespace NzbDrone.Core.Jobs public IList GetPendingJobs() { - return Queryable.Where(c => c.Enable && c.LastExecution < DateTime.UtcNow.AddMinutes(c.Interval)).ToList(); + return Queryable.Where(c => c.Enable && c.LastExecution < DateTime.Now.AddMinutes(-c.Interval)).ToList(); } public void Init() diff --git a/NzbDrone.Core/Tv/EpisodeService.cs b/NzbDrone.Core/Tv/EpisodeService.cs index dfa6898e0..00ee934e0 100644 --- a/NzbDrone.Core/Tv/EpisodeService.cs +++ b/NzbDrone.Core/Tv/EpisodeService.cs @@ -192,6 +192,8 @@ namespace NzbDrone.Core.Tv logger.Trace("Updating info for [{0}] - S{1:00}E{2:00}", series.Title, episode.SeasonNumber, episode.EpisodeNumber); //first check using tvdbId, this should cover cases when and episode number in a season is changed + + var episodes = seriesEpisodes.Where(e => e.TvDbEpisodeId == episode.TvDbEpisodeId).ToList(); var episodeToUpdate = seriesEpisodes.SingleOrDefault(e => e.TvDbEpisodeId == episode.TvDbEpisodeId); //not found, try using season/episode number