You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
4.2 KiB
103 lines
4.2 KiB
12 years ago
|
using System;
|
||
12 years ago
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using NLog;
|
||
11 years ago
|
using NzbDrone.Core.Configuration;
|
||
|
using NzbDrone.Core.Configuration.Events;
|
||
11 years ago
|
using NzbDrone.Core.DataAugmentation;
|
||
11 years ago
|
using NzbDrone.Core.DataAugmentation.Scene;
|
||
11 years ago
|
using NzbDrone.Core.DataAugmentation.Xem;
|
||
11 years ago
|
using NzbDrone.Core.Housekeeping;
|
||
12 years ago
|
using NzbDrone.Core.Indexers;
|
||
12 years ago
|
using NzbDrone.Core.Instrumentation.Commands;
|
||
12 years ago
|
using NzbDrone.Core.Lifecycle;
|
||
12 years ago
|
using NzbDrone.Core.MediaFiles.Commands;
|
||
11 years ago
|
using NzbDrone.Core.Messaging.Commands.Tracking;
|
||
11 years ago
|
using NzbDrone.Core.Messaging.Events;
|
||
12 years ago
|
using NzbDrone.Core.Tv.Commands;
|
||
12 years ago
|
using NzbDrone.Core.Update.Commands;
|
||
12 years ago
|
|
||
|
namespace NzbDrone.Core.Jobs
|
||
|
{
|
||
|
public interface ITaskManager
|
||
|
{
|
||
|
IList<ScheduledTask> GetPending();
|
||
|
}
|
||
|
|
||
11 years ago
|
public class TaskManager : ITaskManager, IHandle<ApplicationStartedEvent>, IHandle<CommandExecutedEvent>, IHandleAsync<ConfigSavedEvent>
|
||
12 years ago
|
{
|
||
|
private readonly IScheduledTaskRepository _scheduledTaskRepository;
|
||
11 years ago
|
private readonly IConfigService _configService;
|
||
12 years ago
|
private readonly Logger _logger;
|
||
|
|
||
11 years ago
|
public TaskManager(IScheduledTaskRepository scheduledTaskRepository, IConfigService configService, Logger logger)
|
||
12 years ago
|
{
|
||
|
_scheduledTaskRepository = scheduledTaskRepository;
|
||
11 years ago
|
_configService = configService;
|
||
12 years ago
|
_logger = logger;
|
||
|
}
|
||
|
|
||
|
public IList<ScheduledTask> GetPending()
|
||
|
{
|
||
12 years ago
|
return _scheduledTaskRepository.All().Where(c => c.LastExecution.AddMinutes(c.Interval) < DateTime.UtcNow).ToList();
|
||
12 years ago
|
}
|
||
|
|
||
|
public void Handle(ApplicationStartedEvent message)
|
||
|
{
|
||
|
var defaultTasks = new[]
|
||
|
{
|
||
11 years ago
|
new ScheduledTask{ Interval = _configService.RssSyncInterval, TypeName = typeof(RssSyncCommand).FullName},
|
||
11 years ago
|
new ScheduledTask{ Interval = 12*60, TypeName = typeof(UpdateXemMappingsCommand).FullName},
|
||
11 years ago
|
new ScheduledTask{ Interval = 12*60, TypeName = typeof(RefreshSeriesCommand).FullName},
|
||
12 years ago
|
new ScheduledTask{ Interval = 1, TypeName = typeof(DownloadedEpisodesScanCommand).FullName},
|
||
11 years ago
|
new ScheduledTask{ Interval = 60, TypeName = typeof(ApplicationUpdateCommand).FullName},
|
||
11 years ago
|
new ScheduledTask{ Interval = 1*60, TypeName = typeof(TrimLogCommand).FullName},
|
||
11 years ago
|
new ScheduledTask{ Interval = 3*60, TypeName = typeof(UpdateSceneMappingCommand).FullName},
|
||
11 years ago
|
new ScheduledTask{ Interval = 1, TypeName = typeof(TrackedCommandCleanupCommand).FullName},
|
||
|
new ScheduledTask{ Interval = 24*60, TypeName = typeof(HousekeepingCommand).FullName}
|
||
12 years ago
|
};
|
||
|
|
||
|
var currentTasks = _scheduledTaskRepository.All();
|
||
|
|
||
|
_logger.Debug("Initializing jobs. Available: {0} Existing:{1}", defaultTasks.Count(), currentTasks.Count());
|
||
|
|
||
|
|
||
|
foreach (var job in currentTasks)
|
||
|
{
|
||
12 years ago
|
if (!defaultTasks.Any(c => c.TypeName == job.TypeName))
|
||
12 years ago
|
{
|
||
12 years ago
|
_logger.Debug("Removing job from database '{0}'", job.TypeName);
|
||
12 years ago
|
_scheduledTaskRepository.Delete(job.Id);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
foreach (var defaultTask in defaultTasks)
|
||
|
{
|
||
12 years ago
|
var currentDefinition = currentTasks.SingleOrDefault(c => c.TypeName == defaultTask.TypeName) ?? defaultTask;
|
||
12 years ago
|
|
||
12 years ago
|
currentDefinition.Interval = defaultTask.Interval;
|
||
12 years ago
|
|
||
12 years ago
|
_scheduledTaskRepository.Upsert(currentDefinition);
|
||
12 years ago
|
}
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
public void Handle(CommandExecutedEvent message)
|
||
12 years ago
|
{
|
||
11 years ago
|
var scheduledTask = _scheduledTaskRepository.All().SingleOrDefault(c => c.TypeName == message.Command.GetType().FullName);
|
||
12 years ago
|
|
||
|
if (scheduledTask != null)
|
||
|
{
|
||
11 years ago
|
_logger.Trace("Updating last run time for: {0}", scheduledTask.TypeName);
|
||
12 years ago
|
_scheduledTaskRepository.SetLastExecutionTime(scheduledTask.Id, DateTime.UtcNow);
|
||
|
}
|
||
12 years ago
|
}
|
||
11 years ago
|
|
||
|
public void HandleAsync(ConfigSavedEvent message)
|
||
|
{
|
||
11 years ago
|
var rss = _scheduledTaskRepository.GetDefinition(typeof(RssSyncCommand));
|
||
11 years ago
|
rss.Interval = _configService.RssSyncInterval;
|
||
|
_scheduledTaskRepository.Update(rss);
|
||
|
}
|
||
12 years ago
|
}
|
||
11 years ago
|
}
|