From 8946b401cfc863945b3c1a52274b90c50c631d57 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Mon, 21 Feb 2022 10:45:58 -0800 Subject: [PATCH] New: Schedule refresh and process monitored download tasks at high priority Closes #4907 --- src/NzbDrone.Core/Datastore/TableMapping.cs | 3 +- .../DownloadMonitoringService.cs | 4 +- src/NzbDrone.Core/Jobs/ScheduledTask.cs | 7 ++ src/NzbDrone.Core/Jobs/Scheduler.cs | 2 +- src/NzbDrone.Core/Jobs/TaskManager.cs | 88 +++++++++++++++---- 5 files changed, 82 insertions(+), 22 deletions(-) diff --git a/src/NzbDrone.Core/Datastore/TableMapping.cs b/src/NzbDrone.Core/Datastore/TableMapping.cs index 4825a5760..3fb430fcf 100644 --- a/src/NzbDrone.Core/Datastore/TableMapping.cs +++ b/src/NzbDrone.Core/Datastore/TableMapping.cs @@ -60,7 +60,8 @@ namespace NzbDrone.Core.Datastore .Ignore(r => r.FreeSpace) .Ignore(r => r.TotalSpace); - Mapper.Entity().RegisterModel("ScheduledTasks"); + Mapper.Entity().RegisterModel("ScheduledTasks") + .Ignore(i => i.Priority); Mapper.Entity().RegisterDefinition("Indexers") .Ignore(i => i.Enable) diff --git a/src/NzbDrone.Core/Download/TrackedDownloads/DownloadMonitoringService.cs b/src/NzbDrone.Core/Download/TrackedDownloads/DownloadMonitoringService.cs index da6e74b4b..c61cecebb 100644 --- a/src/NzbDrone.Core/Download/TrackedDownloads/DownloadMonitoringService.cs +++ b/src/NzbDrone.Core/Download/TrackedDownloads/DownloadMonitoringService.cs @@ -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 { diff --git a/src/NzbDrone.Core/Jobs/ScheduledTask.cs b/src/NzbDrone.Core/Jobs/ScheduledTask.cs index 5d842696d..3547dcf62 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 { @@ -8,5 +9,11 @@ namespace NzbDrone.Core.Jobs public string TypeName { get; set; } public int Interval { get; set; } public DateTime LastExecution { get; set; } + public CommandPriority Priority { get; set; } + + public ScheduledTask() + { + Priority = CommandPriority.Low; + } } } \ No newline at end of file diff --git a/src/NzbDrone.Core/Jobs/Scheduler.cs b/src/NzbDrone.Core/Jobs/Scheduler.cs index a18aac556..c55035597 100644 --- a/src/NzbDrone.Core/Jobs/Scheduler.cs +++ b/src/NzbDrone.Core/Jobs/Scheduler.cs @@ -39,7 +39,7 @@ namespace NzbDrone.Core.Jobs foreach (var task in tasks) { - _commandQueueManager.Push(task.TypeName, task.LastExecution, CommandPriority.Low, CommandTrigger.Scheduled); + _commandQueueManager.Push(task.TypeName, task.LastExecution, task.Priority, CommandTrigger.Scheduled); } } diff --git a/src/NzbDrone.Core/Jobs/TaskManager.cs b/src/NzbDrone.Core/Jobs/TaskManager.cs index 7b862bf4a..c74c58beb 100644 --- a/src/NzbDrone.Core/Jobs/TaskManager.cs +++ b/src/NzbDrone.Core/Jobs/TaskManager.cs @@ -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; @@ -32,49 +33,86 @@ namespace NzbDrone.Core.Jobs 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 = 1, TypeName = typeof(RefreshMonitoredDownloadsCommand).FullName}, - new ScheduledTask{ Interval = 5, TypeName = typeof(MessagingCleanupCommand).FullName}, - new ScheduledTask{ Interval = 6*60, TypeName = typeof(ApplicationUpdateCheckCommand).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}, - new ScheduledTask{ Interval = 24*60, TypeName = typeof(HousekeepingCommand).FullName}, - new ScheduledTask{ Interval = 24*60, TypeName = typeof(CleanUpRecycleBinCommand).FullName}, + new ScheduledTask + { + Interval = 1, + TypeName = typeof(RefreshMonitoredDownloadsCommand).FullName, + Priority = CommandPriority.High + }, new ScheduledTask { - Interval = GetBackupInterval(), - TypeName = typeof(BackupCommand).FullName + Interval = 5, + TypeName = typeof(MessagingCleanupCommand).FullName + }, + + new ScheduledTask + { + Interval = 6 * 60, + TypeName = typeof(ApplicationUpdateCheckCommand).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 + }, + + new ScheduledTask + { + Interval = 24 * 60, + TypeName = typeof(HousekeepingCommand).FullName + }, + + new ScheduledTask + { + Interval = 24 * 60, + TypeName = typeof(CleanUpRecycleBinCommand).FullName }, new ScheduledTask @@ -83,6 +121,12 @@ namespace NzbDrone.Core.Jobs TypeName = typeof(ImportListSyncCommand).FullName }, + new ScheduledTask + { + Interval = GetBackupInterval(), + TypeName = typeof(BackupCommand).FullName + }, + new ScheduledTask { Interval = GetRssSyncInterval(), @@ -114,6 +158,7 @@ namespace NzbDrone.Core.Jobs currentDefinition.LastExecution = DateTime.UtcNow; } + _cache.Set(currentDefinition.TypeName, currentDefinition); _scheduledTaskRepository.Upsert(currentDefinition); } } @@ -159,7 +204,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); + + var lastExecution = DateTime.UtcNow; + + _scheduledTaskRepository.SetLastExecutionTime(scheduledTask.Id, lastExecution); + _cache.Find(scheduledTask.TypeName).LastExecution = lastExecution; } } @@ -172,6 +221,9 @@ namespace NzbDrone.Core.Jobs backup.Interval = GetBackupInterval(); _scheduledTaskRepository.UpdateMany(new List{ rss, backup }); + + _cache.Find(rss.TypeName).Interval = rss.Interval; + _cache.Find(backup.TypeName).Interval = backup.Interval; } } }