Radarr/src/NzbDrone.Core/Jobs/TaskManager.cs

132 lines
5.3 KiB
C#
Raw Normal View History

2013-05-10 23:53:50 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Core.Backup;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Configuration.Events;
using NzbDrone.Core.DataAugmentation.Scene;
2013-10-22 07:31:36 +00:00
using NzbDrone.Core.Download;
2014-02-26 05:40:47 +00:00
using NzbDrone.Core.HealthCheck;
using NzbDrone.Core.Housekeeping;
using NzbDrone.Core.Indexers;
2013-06-19 01:01:08 +00:00
using NzbDrone.Core.Instrumentation.Commands;
using NzbDrone.Core.Lifecycle;
2013-05-13 02:52:55 +00:00
using NzbDrone.Core.MediaFiles.Commands;
2013-09-14 06:42:09 +00:00
using NzbDrone.Core.Messaging.Commands.Tracking;
2013-09-11 06:33:47 +00:00
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Tv.Commands;
2013-05-20 00:30:02 +00:00
using NzbDrone.Core.Update.Commands;
namespace NzbDrone.Core.Jobs
{
public interface ITaskManager
{
IList<ScheduledTask> GetPending();
List<ScheduledTask> GetAll();
}
public class TaskManager : ITaskManager, IHandle<ApplicationStartedEvent>, IHandle<CommandExecutedEvent>, IHandleAsync<ConfigSavedEvent>
{
private readonly IScheduledTaskRepository _scheduledTaskRepository;
private readonly IConfigService _configService;
private readonly Logger _logger;
public TaskManager(IScheduledTaskRepository scheduledTaskRepository, IConfigService configService, Logger logger)
{
_scheduledTaskRepository = scheduledTaskRepository;
_configService = configService;
_logger = logger;
}
public IList<ScheduledTask> GetPending()
{
return _scheduledTaskRepository.All()
.Where(c => c.Interval > 0 && c.LastExecution.AddMinutes(c.Interval) < DateTime.UtcNow)
.ToList();
}
public List<ScheduledTask> GetAll()
{
return _scheduledTaskRepository.All().ToList();
}
public void Handle(ApplicationStartedEvent message)
{
var defaultTasks = new[]
{
new ScheduledTask{ Interval = 1, TypeName = typeof(TrackedCommandCleanupCommand).FullName},
new ScheduledTask{ Interval = 1, TypeName = typeof(CheckForFinishedDownloadCommand).FullName},
new ScheduledTask{ Interval = 6*60, TypeName = typeof(ApplicationUpdateCommand).FullName},
new ScheduledTask{ Interval = 1*60, TypeName = typeof(TrimLogCommand).FullName},
new ScheduledTask{ Interval = 3*60, TypeName = typeof(UpdateSceneMappingCommand).FullName},
new ScheduledTask{ Interval = 6*60, TypeName = typeof(CheckHealthCommand).FullName},
new ScheduledTask{ Interval = 12*60, TypeName = typeof(RefreshSeriesCommand).FullName},
2013-10-22 07:31:36 +00:00
new ScheduledTask{ Interval = 24*60, TypeName = typeof(HousekeepingCommand).FullName},
new ScheduledTask{ Interval = 7*24*60, TypeName = typeof(BackupCommand).FullName},
new ScheduledTask
{
2015-02-06 07:32:28 +00:00
Interval = new [] { _configService.RssSyncInterval, 10 }.Max(),
TypeName = typeof(RssSyncCommand).FullName
},
new ScheduledTask
{
Interval = _configService.DownloadedEpisodesScanInterval,
TypeName = typeof(DownloadedEpisodesScanCommand).FullName
},
};
var currentTasks = _scheduledTaskRepository.All().ToList();
2014-08-02 01:08:02 +00:00
_logger.Trace("Initializing jobs. Available: {0} Existing: {1}", defaultTasks.Count(), currentTasks.Count());
foreach (var job in currentTasks)
{
2013-05-10 23:53:50 +00:00
if (!defaultTasks.Any(c => c.TypeName == job.TypeName))
{
2014-08-02 01:08:02 +00:00
_logger.Trace("Removing job from database '{0}'", job.TypeName);
_scheduledTaskRepository.Delete(job.Id);
}
}
foreach (var defaultTask in defaultTasks)
{
2013-05-21 04:46:30 +00:00
var currentDefinition = currentTasks.SingleOrDefault(c => c.TypeName == defaultTask.TypeName) ?? defaultTask;
2013-05-21 04:46:30 +00:00
currentDefinition.Interval = defaultTask.Interval;
if (currentDefinition.Id == 0)
{
currentDefinition.LastExecution = DateTime.UtcNow;
}
2013-05-21 04:46:30 +00:00
_scheduledTaskRepository.Upsert(currentDefinition);
}
}
public void Handle(CommandExecutedEvent message)
{
2013-09-11 06:33:47 +00:00
var scheduledTask = _scheduledTaskRepository.All().SingleOrDefault(c => c.TypeName == message.Command.GetType().FullName);
if (scheduledTask != null)
{
2014-08-02 01:08:02 +00:00
_logger.Trace("Updating last run time for: {0}", scheduledTask.TypeName);
_scheduledTaskRepository.SetLastExecutionTime(scheduledTask.Id, DateTime.UtcNow);
}
}
public void HandleAsync(ConfigSavedEvent message)
{
var rss = _scheduledTaskRepository.GetDefinition(typeof(RssSyncCommand));
rss.Interval = _configService.RssSyncInterval;
var downloadedEpisodes = _scheduledTaskRepository.GetDefinition(typeof(DownloadedEpisodesScanCommand));
downloadedEpisodes.Interval = _configService.DownloadedEpisodesScanInterval;
_scheduledTaskRepository.UpdateMany(new List<ScheduledTask> { rss, downloadedEpisodes });
}
}
}