diff --git a/src/NzbDrone.Core/Datastore/TableMapping.cs b/src/NzbDrone.Core/Datastore/TableMapping.cs index 27c70fb9e..85ab151de 100644 --- a/src/NzbDrone.Core/Datastore/TableMapping.cs +++ b/src/NzbDrone.Core/Datastore/TableMapping.cs @@ -41,7 +41,8 @@ public static void Map() Mapper.Entity("Config").RegisterModel(); - Mapper.Entity("ScheduledTasks").RegisterModel(); + Mapper.Entity("ScheduledTasks").RegisterModel() + .Ignore(i => i.Priority); Mapper.Entity("Indexers").RegisterModel() .Ignore(x => x.ImplementationName) diff --git a/src/NzbDrone.Core/Jobs/ScheduledTask.cs b/src/NzbDrone.Core/Jobs/ScheduledTask.cs index 3cdfa3132..82c948b2f 100644 --- a/src/NzbDrone.Core/Jobs/ScheduledTask.cs +++ b/src/NzbDrone.Core/Jobs/ScheduledTask.cs @@ -1,5 +1,6 @@ using System; using NzbDrone.Core.Datastore; +using NzbDrone.Core.Messaging.Commands; namespace NzbDrone.Core.Jobs { @@ -9,5 +10,11 @@ public class ScheduledTask : ModelBase public int Interval { get; set; } public DateTime LastExecution { get; set; } public DateTime LastStartTime { get; set; } + public CommandPriority Priority { get; set; } + + public ScheduledTask() + { + Priority = CommandPriority.Low; + } } } diff --git a/src/NzbDrone.Core/Jobs/Scheduler.cs b/src/NzbDrone.Core/Jobs/Scheduler.cs index dffe4dba7..54e243467 100644 --- a/src/NzbDrone.Core/Jobs/Scheduler.cs +++ b/src/NzbDrone.Core/Jobs/Scheduler.cs @@ -1,4 +1,4 @@ -using System.Linq; +using System.Linq; using System.Threading; using System.Threading.Tasks; using NLog; @@ -39,7 +39,7 @@ private void ExecuteCommands() foreach (var task in tasks) { - _commandQueueManager.Push(task.TypeName, task.LastExecution, task.LastStartTime, CommandPriority.Low, CommandTrigger.Scheduled); + _commandQueueManager.Push(task.TypeName, task.LastExecution, task.LastStartTime, task.Priority, CommandTrigger.Scheduled); } } finally diff --git a/src/NzbDrone.Core/Jobs/TaskManager.cs b/src/NzbDrone.Core/Jobs/TaskManager.cs index 7882d7fd4..9fc059696 100644 --- a/src/NzbDrone.Core/Jobs/TaskManager.cs +++ b/src/NzbDrone.Core/Jobs/TaskManager.cs @@ -2,9 +2,11 @@ using System.Collections.Generic; using System.Linq; using NLog; +using NzbDrone.Common.Cache; using NzbDrone.Core.Applications; using NzbDrone.Core.Backup; using NzbDrone.Core.Configuration; +using NzbDrone.Core.Configuration.Events; using NzbDrone.Core.HealthCheck; using NzbDrone.Core.History; using NzbDrone.Core.Housekeeping; @@ -28,35 +30,38 @@ public class TaskManager : ITaskManager, IHandle, IHand private readonly IScheduledTaskRepository _scheduledTaskRepository; private readonly IConfigService _configService; private readonly Logger _logger; + private readonly ICached _cache; - public TaskManager(IScheduledTaskRepository scheduledTaskRepository, IConfigService configService, Logger logger) + public TaskManager(IScheduledTaskRepository scheduledTaskRepository, IConfigService configService, ICacheManager cacheManager, Logger logger) { _scheduledTaskRepository = scheduledTaskRepository; _configService = configService; + _cache = cacheManager.GetCache(GetType()); _logger = logger; } public IList GetPending() { - return _scheduledTaskRepository.All() - .Where(c => c.Interval > 0 && c.LastExecution.AddMinutes(c.Interval) < DateTime.UtcNow) - .ToList(); + return _cache.Values + .Where(c => c.Interval > 0 && c.LastExecution.AddMinutes(c.Interval) < DateTime.UtcNow) + .ToList(); } public List GetAll() { - return _scheduledTaskRepository.All().ToList(); + return _cache.Values.ToList(); } public DateTime GetNextExecution(Type type) { - var scheduledTask = _scheduledTaskRepository.All().Single(v => v.TypeName == type.FullName); + var scheduledTask = _cache.Find(type.FullName); + return scheduledTask.LastExecution.AddMinutes(scheduledTask.Interval); } public void Handle(ApplicationStartedEvent message) { - var defaultTasks = new[] + var defaultTasks = new List { new ScheduledTask { Interval = 5, TypeName = typeof(MessagingCleanupCommand).FullName }, new ScheduledTask { Interval = 6 * 60, TypeName = typeof(ApplicationCheckUpdateCommand).FullName }, @@ -75,7 +80,7 @@ public void Handle(ApplicationStartedEvent message) var currentTasks = _scheduledTaskRepository.All().ToList(); - _logger.Trace("Initializing jobs. Available: {0} Existing: {1}", defaultTasks.Length, currentTasks.Count); + _logger.Trace("Initializing jobs. Available: {0} Existing: {1}", defaultTasks.Count, currentTasks.Count); foreach (var job in currentTasks) { @@ -97,6 +102,9 @@ public void Handle(ApplicationStartedEvent message) currentDefinition.LastExecution = DateTime.UtcNow; } + currentDefinition.Priority = defaultTask.Priority; + + _cache.Set(currentDefinition.TypeName, currentDefinition); _scheduledTaskRepository.Upsert(currentDefinition); } } @@ -115,8 +123,23 @@ public void Handle(CommandExecutedEvent message) if (scheduledTask != null && message.Command.Body.UpdateScheduledTask) { _logger.Trace("Updating last run time for: {0}", scheduledTask.TypeName); - _scheduledTaskRepository.SetLastExecutionTime(scheduledTask.Id, DateTime.UtcNow, message.Command.StartedAt.Value); + + var lastExecution = DateTime.UtcNow; + + _scheduledTaskRepository.SetLastExecutionTime(scheduledTask.Id, lastExecution, message.Command.StartedAt.Value); + _cache.Find(scheduledTask.TypeName).LastExecution = lastExecution; + _cache.Find(scheduledTask.TypeName).LastStartTime = message.Command.StartedAt.Value; } } + + public void HandleAsync(ConfigSavedEvent message) + { + var backup = _scheduledTaskRepository.GetDefinition(typeof(BackupCommand)); + backup.Interval = GetBackupInterval(); + + _scheduledTaskRepository.UpdateMany(new List { backup }); + + _cache.Find(backup.TypeName).Interval = backup.Interval; + } } } diff --git a/src/NzbDrone.Core/Messaging/Commands/CommandPriorityComparer.cs b/src/NzbDrone.Core/Messaging/Commands/CommandPriorityComparer.cs new file mode 100644 index 000000000..5d8d5f13e --- /dev/null +++ b/src/NzbDrone.Core/Messaging/Commands/CommandPriorityComparer.cs @@ -0,0 +1,32 @@ +using System.Collections.Generic; + +namespace NzbDrone.Core.Messaging.Commands +{ + public class CommandPriorityComparer : IComparer + { + public int Compare(CommandStatus x, CommandStatus y) + { + if (x == CommandStatus.Started && y != CommandStatus.Started) + { + return -1; + } + + if (x != CommandStatus.Started && y == CommandStatus.Started) + { + return 1; + } + + if (x < y) + { + return -1; + } + + if (x > y) + { + return 1; + } + + return 0; + } + } +} diff --git a/src/Prowlarr.Api.V1/Commands/CommandController.cs b/src/Prowlarr.Api.V1/Commands/CommandController.cs index eb9ee70cf..6859c94cc 100644 --- a/src/Prowlarr.Api.V1/Commands/CommandController.cs +++ b/src/Prowlarr.Api.V1/Commands/CommandController.cs @@ -26,6 +26,8 @@ public class CommandController : RestControllerWithSignalR _pendingUpdates; + private readonly CommandPriorityComparer _commandPriorityComparer = new CommandPriorityComparer(); + public CommandController(IManageCommandQueue commandQueueManager, IBroadcastSignalRMessage signalRBroadcaster, KnownTypes knownTypes) @@ -73,7 +75,10 @@ public ActionResult StartCommand(CommandResource commandResourc [Produces("application/json")] public List GetStartedCommands() { - return _commandQueueManager.All().ToResource(); + return _commandQueueManager.All() + .OrderBy(c => c.Status, _commandPriorityComparer) + .ThenByDescending(c => c.Priority) + .ToResource(); } [RestDeleteById]