New: Schedule refresh and process monitored download tasks at high priority

Co-Authored-By: Mark McDowall <markus101@users.noreply.github.com>
pull/7176/head
Qstick 2 years ago
parent 1ad2dc54b3
commit a45b91abe8

@ -66,7 +66,8 @@ namespace NzbDrone.Core.Datastore
.Ignore(r => r.FreeSpace)
.Ignore(r => r.TotalSpace);
Mapper.Entity<ScheduledTask>("ScheduledTasks").RegisterModel();
Mapper.Entity<ScheduledTask>("ScheduledTasks").RegisterModel()
.Ignore(i => i.Priority);
Mapper.Entity<IndexerDefinition>("Indexers").RegisterModel()
.Ignore(x => x.ImplementationName)

@ -54,7 +54,7 @@ namespace NzbDrone.Core.Download.TrackedDownloads
private void QueueRefresh()
{
_manageCommandQueue.Push(new RefreshMonitoredDownloadsCommand());
_manageCommandQueue.Push(new RefreshMonitoredDownloadsCommand(), CommandPriority.High);
}
private void Refresh()
@ -75,7 +75,7 @@ namespace NzbDrone.Core.Download.TrackedDownloads
_trackedDownloadService.UpdateTrackable(trackedDownloads);
_eventAggregator.PublishEvent(new TrackedDownloadRefreshedEvent(trackedDownloads));
_manageCommandQueue.Push(new ProcessMonitoredDownloadsCommand());
_manageCommandQueue.Push(new ProcessMonitoredDownloadsCommand(), CommandPriority.High);
}
finally
{

@ -1,5 +1,6 @@
using System;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Messaging.Commands;
namespace NzbDrone.Core.Jobs
{
@ -9,5 +10,11 @@ namespace NzbDrone.Core.Jobs
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;
}
}
}

@ -1,4 +1,4 @@
using System.Linq;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NLog;
@ -39,7 +39,7 @@ namespace NzbDrone.Core.Jobs
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

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Common.Cache;
using NzbDrone.Core.Backup;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Configuration.Events;
@ -31,42 +32,74 @@ namespace NzbDrone.Core.Jobs
private readonly IScheduledTaskRepository _scheduledTaskRepository;
private readonly IConfigService _configService;
private readonly Logger _logger;
private readonly ICached<ScheduledTask> _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<ScheduledTask>(GetType());
_logger = logger;
}
public IList<ScheduledTask> 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<ScheduledTask> 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<ScheduledTask>
{
new ScheduledTask { Interval = 5, TypeName = typeof(MessagingCleanupCommand).FullName },
new ScheduledTask { Interval = 6 * 60, TypeName = typeof(ApplicationCheckUpdateCommand).FullName },
new ScheduledTask { Interval = 6 * 60, TypeName = typeof(CheckHealthCommand).FullName },
new ScheduledTask { Interval = 24 * 60, TypeName = typeof(RefreshMovieCommand).FullName },
new ScheduledTask { Interval = 24 * 60, TypeName = typeof(HousekeepingCommand).FullName },
new ScheduledTask { Interval = 24 * 60, TypeName = typeof(CleanUpRecycleBinCommand).FullName },
new ScheduledTask
{
Interval = 5,
TypeName = typeof(MessagingCleanupCommand).FullName
},
new ScheduledTask
{
Interval = 6 * 60,
TypeName = typeof(ApplicationCheckUpdateCommand).FullName
},
new ScheduledTask
{
Interval = 6 * 60,
TypeName = typeof(CheckHealthCommand).FullName
},
new ScheduledTask
{
Interval = 24 * 60,
TypeName = typeof(RefreshMovieCommand).FullName
},
new ScheduledTask
{
Interval = 24 * 60,
TypeName = typeof(HousekeepingCommand).FullName
},
new ScheduledTask
{
Interval = 24 * 60,
TypeName = typeof(CleanUpRecycleBinCommand).FullName
},
new ScheduledTask
{
@ -89,13 +122,14 @@ namespace NzbDrone.Core.Jobs
new ScheduledTask
{
Interval = Math.Max(_configService.CheckForFinishedDownloadInterval, 1),
TypeName = typeof(RefreshMonitoredDownloadsCommand).FullName
TypeName = typeof(RefreshMonitoredDownloadsCommand).FullName,
Priority = CommandPriority.High
}
};
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)
{
@ -117,6 +151,9 @@ namespace NzbDrone.Core.Jobs
currentDefinition.LastExecution = DateTime.UtcNow;
}
currentDefinition.Priority = defaultTask.Priority;
_cache.Set(currentDefinition.TypeName, currentDefinition);
_scheduledTaskRepository.Upsert(currentDefinition);
}
}
@ -170,7 +207,11 @@ namespace NzbDrone.Core.Jobs
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;
}
}
@ -189,6 +230,10 @@ namespace NzbDrone.Core.Jobs
refreshMonitoredDownloads.Interval = _configService.CheckForFinishedDownloadInterval;
_scheduledTaskRepository.UpdateMany(new List<ScheduledTask> { rss, importList, refreshMonitoredDownloads, backup });
_cache.Find(rss.TypeName).Interval = rss.Interval;
_cache.Find(importList.TypeName).Interval = importList.Interval;
_cache.Find(backup.TypeName).Interval = backup.Interval;
}
}
}

@ -0,0 +1,32 @@
using System.Collections.Generic;
namespace NzbDrone.Core.Messaging.Commands
{
public class CommandPriorityComparer : IComparer<CommandStatus>
{
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;
}
}
}

@ -27,6 +27,8 @@ namespace Radarr.Api.V3.Commands
private readonly Debouncer _debouncer;
private readonly Dictionary<int, CommandResource> _pendingUpdates;
private readonly CommandPriorityComparer _commandPriorityComparer = new CommandPriorityComparer();
public CommandController(IManageCommandQueue commandQueueManager,
IBroadcastSignalRMessage signalRBroadcaster,
KnownTypes knownTypes)
@ -74,7 +76,10 @@ namespace Radarr.Api.V3.Commands
[HttpGet]
public List<CommandResource> GetStartedCommands()
{
return _commandQueueManager.All().ToResource();
return _commandQueueManager.All()
.OrderBy(c => c.Status, _commandPriorityComparer)
.ThenByDescending(c => c.Priority)
.ToResource();
}
[RestDeleteById]

Loading…
Cancel
Save