diff --git a/src/NzbDrone.Core.Test/Housekeeping/Housekeepers/FixFutureRunScheduledTasksFixture.cs b/src/NzbDrone.Core.Test/Housekeeping/Housekeepers/FixFutureRunScheduledTasksFixture.cs new file mode 100644 index 000000000..886ef8a58 --- /dev/null +++ b/src/NzbDrone.Core.Test/Housekeeping/Housekeepers/FixFutureRunScheduledTasksFixture.cs @@ -0,0 +1,49 @@ +using System; +using System.Linq; +using FizzWare.NBuilder; +using FluentAssertions; +using Microsoft.Practices.ObjectBuilder2; +using NUnit.Framework; +using NzbDrone.Core.Housekeeping.Housekeepers; +using NzbDrone.Core.Jobs; +using NzbDrone.Core.Organizer; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.Housekeeping.Housekeepers +{ + [TestFixture] + public class FixFutureRunScheduledTasksFixture : DbTest + { + [Test] + public void should_set_last_execution_time_to_now_when_its_in_the_future() + { + var tasks = Builder.CreateListOfSize(5) + .All() + .With(t => t.LastExecution = DateTime.UtcNow.AddDays(5)) + .BuildListOfNew(); + + Db.InsertMany(tasks); + + Subject.Clean(); + + AllStoredModels.ForEach(t => t.LastExecution.Should().BeBefore(DateTime.UtcNow)); + } + + [Test] + public void should_not_change_last_execution_time_when_its_in_the_past() + { + var expectedTime = DateTime.UtcNow.AddHours(-1); + + var tasks = Builder.CreateListOfSize(5) + .All() + .With(t => t.LastExecution = expectedTime) + .BuildListOfNew(); + + Db.InsertMany(tasks); + + Subject.Clean(); + + AllStoredModels.ForEach(t => t.LastExecution.Should().Be(expectedTime)); + } + } +} \ No newline at end of file diff --git a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index cab12b710..6f0904eda 100644 --- a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -133,6 +133,7 @@ + diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/FixFutureRunScheduledTasks.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/FixFutureRunScheduledTasks.cs new file mode 100644 index 000000000..a75bebfdf --- /dev/null +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/FixFutureRunScheduledTasks.cs @@ -0,0 +1,37 @@ +using System; +using NLog; +using NzbDrone.Common.EnvironmentInfo; +using NzbDrone.Core.Datastore; +using NzbDrone.Core.Jobs; + +namespace NzbDrone.Core.Housekeeping.Housekeepers +{ + public class FixFutureRunScheduledTasks : IHousekeepingTask + { + private readonly IDatabase _database; + private readonly Logger _logger; + + public FixFutureRunScheduledTasks(IDatabase database, Logger logger) + { + _database = database; + _logger = logger; + } + + public void Clean() + { + if (BuildInfo.IsDebug) + { + _logger.Trace("Not running scheduled task last execution cleanup during debug"); + } + + _logger.Trace("Running scheduled task last execution cleanup"); + + var mapper = _database.GetDataMapper(); + mapper.AddParameter("time", DateTime.UtcNow); + + mapper.ExecuteNonQuery(@"UPDATE ScheduledTasks + SET LastExecution = @time + WHERE LastExecution > @time"); + } + } +} diff --git a/src/NzbDrone.Core/Jobs/TaskManager.cs b/src/NzbDrone.Core/Jobs/TaskManager.cs index ddd32e7ec..eeb5e29fb 100644 --- a/src/NzbDrone.Core/Jobs/TaskManager.cs +++ b/src/NzbDrone.Core/Jobs/TaskManager.cs @@ -62,7 +62,6 @@ public void Handle(ApplicationStartedEvent message) _logger.Debug("Initializing jobs. Available: {0} Existing:{1}", defaultTasks.Count(), currentTasks.Count()); - foreach (var job in currentTasks) { if (!defaultTasks.Any(c => c.TypeName == job.TypeName)) diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index ae95fbd39..93787fd08 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -251,6 +251,7 @@ +