2013-05-11 01:53:50 +02:00
|
|
|
using System;
|
2013-05-09 08:38:20 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common.Messaging;
|
|
|
|
using NzbDrone.Core.Indexers;
|
|
|
|
using NzbDrone.Core.Lifecycle;
|
2013-05-13 04:52:55 +02:00
|
|
|
using NzbDrone.Core.MediaFiles.Commands;
|
2013-05-09 08:38:20 +02:00
|
|
|
using NzbDrone.Core.Providers;
|
2013-05-20 02:30:02 +02:00
|
|
|
using NzbDrone.Core.Update.Commands;
|
2013-05-09 08:38:20 +02:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Jobs
|
|
|
|
{
|
|
|
|
public interface ITaskManager
|
|
|
|
{
|
|
|
|
IList<ScheduledTask> GetPending();
|
|
|
|
}
|
|
|
|
|
2013-05-12 01:38:41 +02:00
|
|
|
public class TaskManager : IHandle<ApplicationStartedEvent>, IHandleAsync<CommandExecutedEvent>, ITaskManager
|
2013-05-09 08:38:20 +02:00
|
|
|
{
|
|
|
|
private readonly IScheduledTaskRepository _scheduledTaskRepository;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
public TaskManager(IScheduledTaskRepository scheduledTaskRepository, Logger logger)
|
|
|
|
{
|
|
|
|
_scheduledTaskRepository = scheduledTaskRepository;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IList<ScheduledTask> GetPending()
|
|
|
|
{
|
2013-05-11 07:59:42 +02:00
|
|
|
return _scheduledTaskRepository.All().Where(c => c.LastExecution.AddMinutes(c.Interval) < DateTime.UtcNow).ToList();
|
2013-05-09 08:38:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Handle(ApplicationStartedEvent message)
|
|
|
|
{
|
|
|
|
var defaultTasks = new[]
|
|
|
|
{
|
2013-05-11 01:53:50 +02:00
|
|
|
new ScheduledTask{ Interval = 25, TypeName = typeof(RssSyncCommand).FullName},
|
2013-05-13 04:52:55 +02:00
|
|
|
new ScheduledTask{ Interval = 12*60, TypeName = typeof(UpdateXemMappings).FullName},
|
2013-05-15 04:57:57 +02:00
|
|
|
new ScheduledTask{ Interval = 6*60, TypeName = typeof(DiskScanCommand).FullName},
|
2013-05-20 02:30:02 +02:00
|
|
|
new ScheduledTask{ Interval = 1, TypeName = typeof(DownloadedEpisodesScanCommand).FullName},
|
2013-05-21 04:49:52 +02:00
|
|
|
new ScheduledTask{ Interval = 5, TypeName = typeof(ApplicationUpdateCommand).FullName}
|
2013-05-09 08:38:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
var currentTasks = _scheduledTaskRepository.All();
|
|
|
|
|
|
|
|
_logger.Debug("Initializing jobs. Available: {0} Existing:{1}", defaultTasks.Count(), currentTasks.Count());
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var job in currentTasks)
|
|
|
|
{
|
2013-05-11 01:53:50 +02:00
|
|
|
if (!defaultTasks.Any(c => c.TypeName == job.TypeName))
|
2013-05-09 08:38:20 +02:00
|
|
|
{
|
2013-05-11 01:53:50 +02:00
|
|
|
_logger.Debug("Removing job from database '{0}'", job.TypeName);
|
2013-05-09 08:38:20 +02:00
|
|
|
_scheduledTaskRepository.Delete(job.Id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var defaultTask in defaultTasks)
|
|
|
|
{
|
2013-05-21 06:46:30 +02:00
|
|
|
var currentDefinition = currentTasks.SingleOrDefault(c => c.TypeName == defaultTask.TypeName) ?? defaultTask;
|
2013-05-09 08:38:20 +02:00
|
|
|
|
2013-05-21 06:46:30 +02:00
|
|
|
currentDefinition.Interval = defaultTask.Interval;
|
2013-05-09 08:38:20 +02:00
|
|
|
|
2013-05-21 06:46:30 +02:00
|
|
|
_scheduledTaskRepository.Upsert(currentDefinition);
|
2013-05-09 08:38:20 +02:00
|
|
|
}
|
|
|
|
}
|
2013-05-12 01:38:41 +02:00
|
|
|
|
|
|
|
public void HandleAsync(CommandExecutedEvent message)
|
|
|
|
{
|
2013-05-13 02:36:23 +02:00
|
|
|
var scheduledTask = _scheduledTaskRepository.All().SingleOrDefault(c => c.TypeName == message.Command.GetType().FullName);
|
|
|
|
|
|
|
|
if (scheduledTask != null)
|
|
|
|
{
|
|
|
|
_scheduledTaskRepository.SetLastExecutionTime(scheduledTask.Id, DateTime.UtcNow);
|
|
|
|
}
|
2013-05-12 01:38:41 +02:00
|
|
|
}
|
2013-05-09 08:38:20 +02:00
|
|
|
}
|
|
|
|
}
|