From 6199cb299905f61fbe115204526c065e3a0598f3 Mon Sep 17 00:00:00 2001 From: Qstick Date: Sat, 14 Jan 2023 17:34:26 -0600 Subject: [PATCH] Fixed: Schedule refresh and process monitored download tasks at high priority Fixes #1542 Fixes #1553 Fixes #1537 --- src/NzbDrone.Core/Datastore/TableMapping.cs | 27 +++--- .../DownloadMonitoringService.cs | 4 +- src/NzbDrone.Core/Jobs/ScheduledTask.cs | 7 ++ src/NzbDrone.Core/Jobs/Scheduler.cs | 4 +- src/NzbDrone.Core/Jobs/TaskManager.cs | 86 +++++++++++++++---- .../Commands/CommandPriorityComparer.cs | 32 +++++++ .../Commands/CommandController.cs | 11 ++- 7 files changed, 135 insertions(+), 36 deletions(-) create mode 100644 src/NzbDrone.Core/Messaging/Commands/CommandPriorityComparer.cs diff --git a/src/NzbDrone.Core/Datastore/TableMapping.cs b/src/NzbDrone.Core/Datastore/TableMapping.cs index c42ef592c..555d4fab1 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("ScheduledTasks").RegisterModel(); + Mapper.Entity("ScheduledTasks").RegisterModel() + .Ignore(i => i.Priority); Mapper.Entity("Indexers").RegisterModel() .Ignore(x => x.ImplementationName) @@ -71,9 +72,9 @@ namespace NzbDrone.Core.Datastore .Ignore(d => d.Tags); Mapper.Entity("ImportLists").RegisterModel() - .Ignore(x => x.ImplementationName) - .Ignore(i => i.Enable) - .Ignore(i => i.ListType); + .Ignore(x => x.ImplementationName) + .Ignore(i => i.Enable) + .Ignore(i => i.ListType); Mapper.Entity("Notifications").RegisterModel() .Ignore(x => x.ImplementationName) @@ -102,13 +103,13 @@ namespace NzbDrone.Core.Datastore Mapper.Entity("History").RegisterModel(); Mapper.Entity("Authors") - .Ignore(s => s.RootFolderPath) - .Ignore(s => s.Name) - .Ignore(s => s.ForeignAuthorId) - .HasOne(a => a.Metadata, a => a.AuthorMetadataId) - .HasOne(a => a.QualityProfile, a => a.QualityProfileId) - .HasOne(s => s.MetadataProfile, s => s.MetadataProfileId) - .LazyLoad(a => a.Books, (db, a) => db.Query(new SqlBuilder(db.DatabaseType).Where(b => b.AuthorMetadataId == a.AuthorMetadataId)).ToList(), a => a.AuthorMetadataId > 0); + .Ignore(s => s.RootFolderPath) + .Ignore(s => s.Name) + .Ignore(s => s.ForeignAuthorId) + .HasOne(a => a.Metadata, a => a.AuthorMetadataId) + .HasOne(a => a.QualityProfile, a => a.QualityProfileId) + .HasOne(s => s.MetadataProfile, s => s.MetadataProfileId) + .LazyLoad(a => a.Books, (db, a) => db.Query(new SqlBuilder(db.DatabaseType).Where(b => b.AuthorMetadataId == a.AuthorMetadataId)).ToList(), a => a.AuthorMetadataId > 0); Mapper.Entity("Series").RegisterModel() .Ignore(s => s.ForeignAuthorId) @@ -123,8 +124,8 @@ namespace NzbDrone.Core.Datastore s => s.Id > 0); Mapper.Entity("SeriesBookLink").RegisterModel() - .HasOne(l => l.Book, l => l.BookId) - .HasOne(l => l.Series, l => l.SeriesId); + .HasOne(l => l.Book, l => l.BookId) + .HasOne(l => l.Series, l => l.SeriesId); Mapper.Entity("AuthorMetadata").RegisterModel(); diff --git a/src/NzbDrone.Core/Download/TrackedDownloads/DownloadMonitoringService.cs b/src/NzbDrone.Core/Download/TrackedDownloads/DownloadMonitoringService.cs index 4641dd2e5..1fec7177d 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 3cdfa3132..dd2fe0ce0 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,6 +9,12 @@ 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 DateTime LastStartTime { 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 @@ 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 diff --git a/src/NzbDrone.Core/Jobs/TaskManager.cs b/src/NzbDrone.Core/Jobs/TaskManager.cs index 0b55160af..09177a88b 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.Books.Commands; using NzbDrone.Core.Configuration; @@ -31,43 +32,81 @@ 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 = 6 * 60, TypeName = typeof(CheckHealthCommand).FullName }, - new ScheduledTask { Interval = 24 * 60, TypeName = typeof(RefreshAuthorCommand).FullName }, - new ScheduledTask { Interval = 24 * 60, TypeName = typeof(RescanFoldersCommand).FullName }, - new ScheduledTask { Interval = 24 * 60, TypeName = typeof(HousekeepingCommand).FullName }, + new ScheduledTask + { + Interval = 1, + TypeName = typeof(RefreshMonitoredDownloadsCommand).FullName, + Priority = CommandPriority.High + }, + + new ScheduledTask + { + Interval = 5, + TypeName = typeof(MessagingCleanupCommand).FullName + }, + + new ScheduledTask + { + Interval = 6 * 60, + TypeName = typeof(ApplicationUpdateCheckCommand).FullName + }, + + new ScheduledTask + { + Interval = 6 * 60, + TypeName = typeof(CheckHealthCommand).FullName + }, + + new ScheduledTask + { + Interval = 24 * 60, + TypeName = typeof(RefreshAuthorCommand).FullName + }, + + new ScheduledTask + { + Interval = 24 * 60, + TypeName = typeof(RescanFoldersCommand).FullName + }, + + new ScheduledTask + { + Interval = 24 * 60, + TypeName = typeof(HousekeepingCommand).FullName + }, new ScheduledTask { @@ -90,7 +129,7 @@ namespace NzbDrone.Core.Jobs var currentTasks = _scheduledTaskRepository.All().ToList(); - _logger.Trace("Initializing jobs. Available: {0} Existing: {1}", defaultTasks.Count(), currentTasks.Count()); + _logger.Trace("Initializing jobs. Available: {0} Existing: {1}", defaultTasks.Count, currentTasks.Count); foreach (var job in currentTasks) { @@ -112,6 +151,9 @@ namespace NzbDrone.Core.Jobs currentDefinition.LastExecution = DateTime.UtcNow; } + currentDefinition.Priority = defaultTask.Priority; + + _cache.Set(currentDefinition.TypeName, currentDefinition); _scheduledTaskRepository.Upsert(currentDefinition); } } @@ -152,7 +194,16 @@ 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; + var startTime = message.Command.StartedAt.Value; + + _scheduledTaskRepository.SetLastExecutionTime(scheduledTask.Id, lastExecution, startTime); + + var cached = _cache.Find(scheduledTask.TypeName); + + cached.LastExecution = lastExecution; + cached.LastStartTime = startTime; } } @@ -165,6 +216,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; } } } 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/Readarr.Api.V1/Commands/CommandController.cs b/src/Readarr.Api.V1/Commands/CommandController.cs index 54244b7d0..406456c45 100644 --- a/src/Readarr.Api.V1/Commands/CommandController.cs +++ b/src/Readarr.Api.V1/Commands/CommandController.cs @@ -27,6 +27,8 @@ namespace Readarr.Api.V1.Commands private readonly Debouncer _debouncer; private readonly Dictionary _pendingUpdates; + private readonly CommandPriorityComparer _commandPriorityComparer = new CommandPriorityComparer(); + public CommandController(IManageCommandQueue commandQueueManager, IBroadcastSignalRMessage signalRBroadcaster, KnownTypes knownTypes) @@ -35,10 +37,10 @@ namespace Readarr.Api.V1.Commands _commandQueueManager = commandQueueManager; _knownTypes = knownTypes; - PostValidator.RuleFor(c => c.Name).NotBlank(); - _debouncer = new Debouncer(SendUpdates, TimeSpan.FromSeconds(0.1)); _pendingUpdates = new Dictionary(); + + PostValidator.RuleFor(c => c.Name).NotBlank(); } protected override CommandResource GetResourceById(int id) @@ -72,7 +74,10 @@ namespace Readarr.Api.V1.Commands [HttpGet] public List GetStartedCommands() { - return _commandQueueManager.All().ToResource(); + return _commandQueueManager.All() + .OrderBy(c => c.Status, _commandPriorityComparer) + .ThenByDescending(c => c.Priority) + .ToResource(); } [RestDeleteById]