1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-14 11:23:42 +02:00

Fixed: Scheduled Tasks that last ran in the future will be re-run after application start up

This commit is contained in:
Mark McDowall 2013-11-28 18:07:11 -08:00
parent e6a4008fad
commit 133ee1a0b3
5 changed files with 88 additions and 1 deletions

View File

@ -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<FixFutureRunScheduledTasks, ScheduledTask>
{
[Test]
public void should_set_last_execution_time_to_now_when_its_in_the_future()
{
var tasks = Builder<ScheduledTask>.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<ScheduledTask>.CreateListOfSize(5)
.All()
.With(t => t.LastExecution = expectedTime)
.BuildListOfNew();
Db.InsertMany(tasks);
Subject.Clean();
AllStoredModels.ForEach(t => t.LastExecution.Should().Be(expectedTime));
}
}
}

View File

@ -133,6 +133,7 @@
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedHistoryItemsFixture.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedEpisodeFilesFixture.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupAdditionalNamingSpecsFixture.cs" />
<Compile Include="Housekeeping\Housekeepers\FixFutureRunScheduledTasksFixture.cs" />
<Compile Include="IndexerSearchTests\SearchDefinitionFixture.cs" />
<Compile Include="IndexerTests\BasicRssParserFixture.cs" />
<Compile Include="IndexerTests\IndexerServiceFixture.cs" />

View File

@ -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");
}
}
}

View File

@ -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))

View File

@ -251,6 +251,7 @@
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedHistoryItems.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedEpisodeFiles.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupAdditionalNamingSpecs.cs" />
<Compile Include="Housekeeping\Housekeepers\FixFutureRunScheduledTasks.cs" />
<Compile Include="Housekeeping\HousekeepingCommand.cs" />
<Compile Include="Housekeeping\HousekeepingService.cs" />
<Compile Include="Housekeeping\IHousekeepingTask.cs" />