mirror of
https://github.com/Sonarr/Sonarr.git
synced 2024-12-16 11:37:58 +02:00
Fixed: Scheduled Tasks that last ran in the future will be re-run after application start up
This commit is contained in:
parent
e6a4008fad
commit
133ee1a0b3
@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -133,6 +133,7 @@
|
|||||||
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedHistoryItemsFixture.cs" />
|
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedHistoryItemsFixture.cs" />
|
||||||
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedEpisodeFilesFixture.cs" />
|
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedEpisodeFilesFixture.cs" />
|
||||||
<Compile Include="Housekeeping\Housekeepers\CleanupAdditionalNamingSpecsFixture.cs" />
|
<Compile Include="Housekeeping\Housekeepers\CleanupAdditionalNamingSpecsFixture.cs" />
|
||||||
|
<Compile Include="Housekeeping\Housekeepers\FixFutureRunScheduledTasksFixture.cs" />
|
||||||
<Compile Include="IndexerSearchTests\SearchDefinitionFixture.cs" />
|
<Compile Include="IndexerSearchTests\SearchDefinitionFixture.cs" />
|
||||||
<Compile Include="IndexerTests\BasicRssParserFixture.cs" />
|
<Compile Include="IndexerTests\BasicRssParserFixture.cs" />
|
||||||
<Compile Include="IndexerTests\IndexerServiceFixture.cs" />
|
<Compile Include="IndexerTests\IndexerServiceFixture.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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -62,7 +62,6 @@ public void Handle(ApplicationStartedEvent message)
|
|||||||
|
|
||||||
_logger.Debug("Initializing jobs. Available: {0} Existing:{1}", defaultTasks.Count(), currentTasks.Count());
|
_logger.Debug("Initializing jobs. Available: {0} Existing:{1}", defaultTasks.Count(), currentTasks.Count());
|
||||||
|
|
||||||
|
|
||||||
foreach (var job in currentTasks)
|
foreach (var job in currentTasks)
|
||||||
{
|
{
|
||||||
if (!defaultTasks.Any(c => c.TypeName == job.TypeName))
|
if (!defaultTasks.Any(c => c.TypeName == job.TypeName))
|
||||||
|
@ -251,6 +251,7 @@
|
|||||||
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedHistoryItems.cs" />
|
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedHistoryItems.cs" />
|
||||||
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedEpisodeFiles.cs" />
|
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedEpisodeFiles.cs" />
|
||||||
<Compile Include="Housekeeping\Housekeepers\CleanupAdditionalNamingSpecs.cs" />
|
<Compile Include="Housekeeping\Housekeepers\CleanupAdditionalNamingSpecs.cs" />
|
||||||
|
<Compile Include="Housekeeping\Housekeepers\FixFutureRunScheduledTasks.cs" />
|
||||||
<Compile Include="Housekeeping\HousekeepingCommand.cs" />
|
<Compile Include="Housekeeping\HousekeepingCommand.cs" />
|
||||||
<Compile Include="Housekeeping\HousekeepingService.cs" />
|
<Compile Include="Housekeeping\HousekeepingService.cs" />
|
||||||
<Compile Include="Housekeeping\IHousekeepingTask.cs" />
|
<Compile Include="Housekeeping\IHousekeepingTask.cs" />
|
||||||
|
Loading…
Reference in New Issue
Block a user