1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-11-24 08:42:19 +02:00

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

Closes #4907
This commit is contained in:
Mark McDowall 2022-02-21 10:45:58 -08:00
parent 87f03e1f38
commit 8946b401cf
5 changed files with 82 additions and 22 deletions

View File

@ -60,7 +60,8 @@ public static void Map()
.Ignore(r => r.FreeSpace) .Ignore(r => r.FreeSpace)
.Ignore(r => r.TotalSpace); .Ignore(r => r.TotalSpace);
Mapper.Entity<ScheduledTask>().RegisterModel("ScheduledTasks"); Mapper.Entity<ScheduledTask>().RegisterModel("ScheduledTasks")
.Ignore(i => i.Priority);
Mapper.Entity<IndexerDefinition>().RegisterDefinition("Indexers") Mapper.Entity<IndexerDefinition>().RegisterDefinition("Indexers")
.Ignore(i => i.Enable) .Ignore(i => i.Enable)

View File

@ -54,7 +54,7 @@ public DownloadMonitoringService(IDownloadClientStatusService downloadClientStat
private void QueueRefresh() private void QueueRefresh()
{ {
_manageCommandQueue.Push(new RefreshMonitoredDownloadsCommand()); _manageCommandQueue.Push(new RefreshMonitoredDownloadsCommand(), CommandPriority.High);
} }
private void Refresh() private void Refresh()
@ -75,7 +75,7 @@ private void Refresh()
_trackedDownloadService.UpdateTrackable(trackedDownloads); _trackedDownloadService.UpdateTrackable(trackedDownloads);
_eventAggregator.PublishEvent(new TrackedDownloadRefreshedEvent(trackedDownloads)); _eventAggregator.PublishEvent(new TrackedDownloadRefreshedEvent(trackedDownloads));
_manageCommandQueue.Push(new ProcessMonitoredDownloadsCommand()); _manageCommandQueue.Push(new ProcessMonitoredDownloadsCommand(), CommandPriority.High);
} }
finally finally
{ {

View File

@ -1,5 +1,6 @@
using System; using System;
using NzbDrone.Core.Datastore; using NzbDrone.Core.Datastore;
using NzbDrone.Core.Messaging.Commands;
namespace NzbDrone.Core.Jobs namespace NzbDrone.Core.Jobs
{ {
@ -8,5 +9,11 @@ public class ScheduledTask : ModelBase
public string TypeName { get; set; } public string TypeName { get; set; }
public int Interval { get; set; } public int Interval { get; set; }
public DateTime LastExecution { get; set; } public DateTime LastExecution { get; set; }
public CommandPriority Priority { get; set; }
public ScheduledTask()
{
Priority = CommandPriority.Low;
}
} }
} }

View File

@ -39,7 +39,7 @@ private void ExecuteCommands()
foreach (var task in tasks) 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);
} }
} }

View File

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using NLog; using NLog;
using NzbDrone.Common.Cache;
using NzbDrone.Core.Backup; using NzbDrone.Core.Backup;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
using NzbDrone.Core.Configuration.Events; using NzbDrone.Core.Configuration.Events;
@ -32,49 +33,86 @@ public class TaskManager : ITaskManager, IHandle<ApplicationStartedEvent>, IHand
private readonly IScheduledTaskRepository _scheduledTaskRepository; private readonly IScheduledTaskRepository _scheduledTaskRepository;
private readonly IConfigService _configService; private readonly IConfigService _configService;
private readonly Logger _logger; 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; _scheduledTaskRepository = scheduledTaskRepository;
_configService = configService; _configService = configService;
_cache = cacheManager.GetCache<ScheduledTask>(GetType());
_logger = logger; _logger = logger;
} }
public IList<ScheduledTask> GetPending() public IList<ScheduledTask> GetPending()
{ {
return _scheduledTaskRepository.All() return _cache.Values
.Where(c => c.Interval > 0 && c.LastExecution.AddMinutes(c.Interval) < DateTime.UtcNow) .Where(c => c.Interval > 0 && c.LastExecution.AddMinutes(c.Interval) < DateTime.UtcNow)
.ToList(); .ToList();
} }
public List<ScheduledTask> GetAll() public List<ScheduledTask> GetAll()
{ {
return _scheduledTaskRepository.All().ToList(); return _cache.Values.ToList();
} }
public DateTime GetNextExecution(Type type) 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); return scheduledTask.LastExecution.AddMinutes(scheduledTask.Interval);
} }
public void Handle(ApplicationStartedEvent message) public void Handle(ApplicationStartedEvent message)
{ {
var defaultTasks = new[] var defaultTasks = new List<ScheduledTask>
{ {
new ScheduledTask{ Interval = 1, TypeName = typeof(RefreshMonitoredDownloadsCommand).FullName}, new ScheduledTask
new ScheduledTask{ Interval = 5, TypeName = typeof(MessagingCleanupCommand).FullName}, {
new ScheduledTask{ Interval = 6*60, TypeName = typeof(ApplicationUpdateCheckCommand).FullName}, Interval = 1,
new ScheduledTask{ Interval = 3*60, TypeName = typeof(UpdateSceneMappingCommand).FullName}, TypeName = typeof(RefreshMonitoredDownloadsCommand).FullName,
new ScheduledTask{ Interval = 6*60, TypeName = typeof(CheckHealthCommand).FullName}, Priority = CommandPriority.High
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 new ScheduledTask
{ {
Interval = GetBackupInterval(), Interval = 5,
TypeName = typeof(BackupCommand).FullName 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 new ScheduledTask
@ -83,6 +121,12 @@ public void Handle(ApplicationStartedEvent message)
TypeName = typeof(ImportListSyncCommand).FullName TypeName = typeof(ImportListSyncCommand).FullName
}, },
new ScheduledTask
{
Interval = GetBackupInterval(),
TypeName = typeof(BackupCommand).FullName
},
new ScheduledTask new ScheduledTask
{ {
Interval = GetRssSyncInterval(), Interval = GetRssSyncInterval(),
@ -114,6 +158,7 @@ public void Handle(ApplicationStartedEvent message)
currentDefinition.LastExecution = DateTime.UtcNow; currentDefinition.LastExecution = DateTime.UtcNow;
} }
_cache.Set(currentDefinition.TypeName, currentDefinition);
_scheduledTaskRepository.Upsert(currentDefinition); _scheduledTaskRepository.Upsert(currentDefinition);
} }
} }
@ -159,7 +204,11 @@ public void Handle(CommandExecutedEvent message)
if (scheduledTask != null && message.Command.Body.UpdateScheduledTask) if (scheduledTask != null && message.Command.Body.UpdateScheduledTask)
{ {
_logger.Trace("Updating last run time for: {0}", scheduledTask.TypeName); _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 @@ public void HandleAsync(ConfigSavedEvent message)
backup.Interval = GetBackupInterval(); backup.Interval = GetBackupInterval();
_scheduledTaskRepository.UpdateMany(new List<ScheduledTask>{ rss, backup }); _scheduledTaskRepository.UpdateMany(new List<ScheduledTask>{ rss, backup });
_cache.Find(rss.TypeName).Interval = rss.Interval;
_cache.Find(backup.TypeName).Interval = backup.Interval;
} }
} }
} }