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

Fixes #2661
Fixes #2666
Fixes #2674
This commit is contained in:
Qstick 2023-01-14 17:34:26 -06:00
parent cb5e249448
commit 5a8082de2d
7 changed files with 164 additions and 74 deletions

View file

@ -27,6 +27,8 @@ namespace Lidarr.Api.V1.Commands
private readonly Debouncer _debouncer; private readonly Debouncer _debouncer;
private readonly Dictionary<int, CommandResource> _pendingUpdates; private readonly Dictionary<int, CommandResource> _pendingUpdates;
private readonly CommandPriorityComparer _commandPriorityComparer = new CommandPriorityComparer();
public CommandController(IManageCommandQueue commandQueueManager, public CommandController(IManageCommandQueue commandQueueManager,
IBroadcastSignalRMessage signalRBroadcaster, IBroadcastSignalRMessage signalRBroadcaster,
KnownTypes knownTypes) KnownTypes knownTypes)
@ -35,10 +37,10 @@ namespace Lidarr.Api.V1.Commands
_commandQueueManager = commandQueueManager; _commandQueueManager = commandQueueManager;
_knownTypes = knownTypes; _knownTypes = knownTypes;
PostValidator.RuleFor(c => c.Name).NotBlank();
_debouncer = new Debouncer(SendUpdates, TimeSpan.FromSeconds(0.1)); _debouncer = new Debouncer(SendUpdates, TimeSpan.FromSeconds(0.1));
_pendingUpdates = new Dictionary<int, CommandResource>(); _pendingUpdates = new Dictionary<int, CommandResource>();
PostValidator.RuleFor(c => c.Name).NotBlank();
} }
public override CommandResource GetResourceById(int id) public override CommandResource GetResourceById(int id)
@ -74,7 +76,10 @@ namespace Lidarr.Api.V1.Commands
[HttpGet] [HttpGet]
public List<CommandResource> GetStartedCommands() public List<CommandResource> GetStartedCommands()
{ {
return _commandQueueManager.All().ToResource(); return _commandQueueManager.All()
.OrderBy(c => c.Status, _commandPriorityComparer)
.ThenByDescending(c => c.Priority)
.ToResource();
} }
[RestDeleteById] [RestDeleteById]

View file

@ -61,7 +61,8 @@ namespace NzbDrone.Core.Datastore
.Ignore(r => r.FreeSpace) .Ignore(r => r.FreeSpace)
.Ignore(r => r.TotalSpace); .Ignore(r => r.TotalSpace);
Mapper.Entity<ScheduledTask>("ScheduledTasks").RegisterModel(); Mapper.Entity<ScheduledTask>("ScheduledTasks").RegisterModel()
.Ignore(i => i.Priority);
Mapper.Entity<IndexerDefinition>("Indexers").RegisterModel() Mapper.Entity<IndexerDefinition>("Indexers").RegisterModel()
.Ignore(x => x.ImplementationName) .Ignore(x => x.ImplementationName)
@ -71,9 +72,9 @@ namespace NzbDrone.Core.Datastore
.Ignore(i => i.SupportsSearch); .Ignore(i => i.SupportsSearch);
Mapper.Entity<ImportListDefinition>("ImportLists").RegisterModel() Mapper.Entity<ImportListDefinition>("ImportLists").RegisterModel()
.Ignore(x => x.ImplementationName) .Ignore(x => x.ImplementationName)
.Ignore(i => i.Enable) .Ignore(i => i.Enable)
.Ignore(i => i.ListType); .Ignore(i => i.ListType);
Mapper.Entity<NotificationDefinition>("Notifications").RegisterModel() Mapper.Entity<NotificationDefinition>("Notifications").RegisterModel()
.Ignore(x => x.ImplementationName) .Ignore(x => x.ImplementationName)
@ -101,64 +102,64 @@ namespace NzbDrone.Core.Datastore
Mapper.Entity<EntityHistory>("History").RegisterModel(); Mapper.Entity<EntityHistory>("History").RegisterModel();
Mapper.Entity<Artist>("Artists") Mapper.Entity<Artist>("Artists")
.Ignore(s => s.RootFolderPath) .Ignore(s => s.RootFolderPath)
.Ignore(s => s.Name) .Ignore(s => s.Name)
.Ignore(s => s.ForeignArtistId) .Ignore(s => s.ForeignArtistId)
.HasOne(a => a.Metadata, a => a.ArtistMetadataId) .HasOne(a => a.Metadata, a => a.ArtistMetadataId)
.HasOne(a => a.QualityProfile, a => a.QualityProfileId) .HasOne(a => a.QualityProfile, a => a.QualityProfileId)
.HasOne(s => s.MetadataProfile, s => s.MetadataProfileId) .HasOne(s => s.MetadataProfile, s => s.MetadataProfileId)
.LazyLoad(a => a.Albums, (db, a) => db.Query<Album>(new SqlBuilder(db.DatabaseType).Where<Album>(rg => rg.ArtistMetadataId == a.Id)).ToList(), a => a.Id > 0); .LazyLoad(a => a.Albums, (db, a) => db.Query<Album>(new SqlBuilder(db.DatabaseType).Where<Album>(rg => rg.ArtistMetadataId == a.Id)).ToList(), a => a.Id > 0);
Mapper.Entity<ArtistMetadata>("ArtistMetadata").RegisterModel(); Mapper.Entity<ArtistMetadata>("ArtistMetadata").RegisterModel();
Mapper.Entity<Album>("Albums").RegisterModel() Mapper.Entity<Album>("Albums").RegisterModel()
.Ignore(x => x.ArtistId) .Ignore(x => x.ArtistId)
.HasOne(r => r.ArtistMetadata, r => r.ArtistMetadataId) .HasOne(r => r.ArtistMetadata, r => r.ArtistMetadataId)
.LazyLoad(a => a.AlbumReleases, (db, album) => db.Query<AlbumRelease>(new SqlBuilder(db.DatabaseType).Where<AlbumRelease>(r => r.AlbumId == album.Id)).ToList(), a => a.Id > 0) .LazyLoad(a => a.AlbumReleases, (db, album) => db.Query<AlbumRelease>(new SqlBuilder(db.DatabaseType).Where<AlbumRelease>(r => r.AlbumId == album.Id)).ToList(), a => a.Id > 0)
.LazyLoad(a => a.Artist, .LazyLoad(a => a.Artist,
(db, album) => ArtistRepository.Query(db, (db, album) => ArtistRepository.Query(db,
new SqlBuilder(db.DatabaseType) new SqlBuilder(db.DatabaseType)
.Join<Artist, ArtistMetadata>((a, m) => a.ArtistMetadataId == m.Id) .Join<Artist, ArtistMetadata>((a, m) => a.ArtistMetadataId == m.Id)
.Where<Artist>(a => a.ArtistMetadataId == album.ArtistMetadataId)).SingleOrDefault(), .Where<Artist>(a => a.ArtistMetadataId == album.ArtistMetadataId)).SingleOrDefault(),
a => a.ArtistMetadataId > 0); a => a.ArtistMetadataId > 0);
Mapper.Entity<AlbumRelease>("AlbumReleases").RegisterModel() Mapper.Entity<AlbumRelease>("AlbumReleases").RegisterModel()
.HasOne(r => r.Album, r => r.AlbumId) .HasOne(r => r.Album, r => r.AlbumId)
.LazyLoad(x => x.Tracks, (db, release) => db.Query<Track>(new SqlBuilder(db.DatabaseType).Where<Track>(t => t.AlbumReleaseId == release.Id)).ToList(), r => r.Id > 0); .LazyLoad(x => x.Tracks, (db, release) => db.Query<Track>(new SqlBuilder(db.DatabaseType).Where<Track>(t => t.AlbumReleaseId == release.Id)).ToList(), r => r.Id > 0);
Mapper.Entity<Track>("Tracks").RegisterModel() Mapper.Entity<Track>("Tracks").RegisterModel()
.Ignore(t => t.HasFile) .Ignore(t => t.HasFile)
.Ignore(t => t.AlbumId) .Ignore(t => t.AlbumId)
.HasOne(track => track.AlbumRelease, track => track.AlbumReleaseId) .HasOne(track => track.AlbumRelease, track => track.AlbumReleaseId)
.HasOne(track => track.ArtistMetadata, track => track.ArtistMetadataId) .HasOne(track => track.ArtistMetadata, track => track.ArtistMetadataId)
.LazyLoad(t => t.TrackFile, .LazyLoad(t => t.TrackFile,
(db, track) => MediaFileRepository.Query(db, (db, track) => MediaFileRepository.Query(db,
new SqlBuilder(db.DatabaseType) new SqlBuilder(db.DatabaseType)
.Join<TrackFile, Track>((l, r) => l.Id == r.TrackFileId) .Join<TrackFile, Track>((l, r) => l.Id == r.TrackFileId)
.Join<TrackFile, Album>((l, r) => l.AlbumId == r.Id) .Join<TrackFile, Album>((l, r) => l.AlbumId == r.Id)
.Join<Album, Artist>((l, r) => l.ArtistMetadataId == r.ArtistMetadataId) .Join<Album, Artist>((l, r) => l.ArtistMetadataId == r.ArtistMetadataId)
.Join<Artist, ArtistMetadata>((l, r) => l.ArtistMetadataId == r.Id) .Join<Artist, ArtistMetadata>((l, r) => l.ArtistMetadataId == r.Id)
.Where<TrackFile>(t => t.Id == track.TrackFileId)).SingleOrDefault(), .Where<TrackFile>(t => t.Id == track.TrackFileId)).SingleOrDefault(),
t => t.TrackFileId > 0) t => t.TrackFileId > 0)
.LazyLoad(x => x.Artist, .LazyLoad(x => x.Artist,
(db, t) => ArtistRepository.Query(db, (db, t) => ArtistRepository.Query(db,
new SqlBuilder(db.DatabaseType) new SqlBuilder(db.DatabaseType)
.Join<Artist, ArtistMetadata>((a, m) => a.ArtistMetadataId == m.Id) .Join<Artist, ArtistMetadata>((a, m) => a.ArtistMetadataId == m.Id)
.Join<Artist, Album>((l, r) => l.ArtistMetadataId == r.ArtistMetadataId) .Join<Artist, Album>((l, r) => l.ArtistMetadataId == r.ArtistMetadataId)
.Join<Album, AlbumRelease>((l, r) => l.Id == r.AlbumId) .Join<Album, AlbumRelease>((l, r) => l.Id == r.AlbumId)
.Where<AlbumRelease>(r => r.Id == t.AlbumReleaseId)).SingleOrDefault(), .Where<AlbumRelease>(r => r.Id == t.AlbumReleaseId)).SingleOrDefault(),
t => t.Id > 0); t => t.Id > 0);
Mapper.Entity<TrackFile>("TrackFiles").RegisterModel() Mapper.Entity<TrackFile>("TrackFiles").RegisterModel()
.HasOne(f => f.Album, f => f.AlbumId) .HasOne(f => f.Album, f => f.AlbumId)
.LazyLoad(x => x.Tracks, (db, file) => db.Query<Track>(new SqlBuilder(db.DatabaseType).Where<Track>(t => t.TrackFileId == file.Id)).ToList(), x => x.Id > 0) .LazyLoad(x => x.Tracks, (db, file) => db.Query<Track>(new SqlBuilder(db.DatabaseType).Where<Track>(t => t.TrackFileId == file.Id)).ToList(), x => x.Id > 0)
.LazyLoad(x => x.Artist, .LazyLoad(x => x.Artist,
(db, f) => ArtistRepository.Query(db, (db, f) => ArtistRepository.Query(db,
new SqlBuilder(db.DatabaseType) new SqlBuilder(db.DatabaseType)
.Join<Artist, ArtistMetadata>((a, m) => a.ArtistMetadataId == m.Id) .Join<Artist, ArtistMetadata>((a, m) => a.ArtistMetadataId == m.Id)
.Join<Artist, Album>((l, r) => l.ArtistMetadataId == r.ArtistMetadataId) .Join<Artist, Album>((l, r) => l.ArtistMetadataId == r.ArtistMetadataId)
.Where<Album>(a => a.Id == f.AlbumId)).SingleOrDefault(), .Where<Album>(a => a.Id == f.AlbumId)).SingleOrDefault(),
t => t.Id > 0); t => t.Id > 0);
Mapper.Entity<QualityDefinition>("QualityDefinitions").RegisterModel() Mapper.Entity<QualityDefinition>("QualityDefinitions").RegisterModel()
.Ignore(d => d.GroupName) .Ignore(d => d.GroupName)

View file

@ -54,7 +54,7 @@ namespace NzbDrone.Core.Download.TrackedDownloads
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 @@ namespace NzbDrone.Core.Download.TrackedDownloads
_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
{ {
@ -9,5 +10,11 @@ namespace NzbDrone.Core.Jobs
public int Interval { get; set; } public int Interval { get; set; }
public DateTime LastExecution { get; set; } public DateTime LastExecution { get; set; }
public DateTime LastStartTime { get; set; } public DateTime LastStartTime { get; set; }
public CommandPriority Priority { get; set; }
public ScheduledTask()
{
Priority = CommandPriority.Low;
}
} }
} }

View file

@ -1,4 +1,4 @@
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using NLog; using NLog;
@ -39,7 +39,7 @@ namespace NzbDrone.Core.Jobs
foreach (var task in tasks) 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 finally

View file

@ -2,6 +2,7 @@ using System;
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;
@ -31,43 +32,81 @@ namespace NzbDrone.Core.Jobs
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 = 6 * 60, TypeName = typeof(CheckHealthCommand).FullName }, TypeName = typeof(RefreshMonitoredDownloadsCommand).FullName,
new ScheduledTask { Interval = 24 * 60, TypeName = typeof(RefreshArtistCommand).FullName }, Priority = CommandPriority.High
new ScheduledTask { Interval = 24 * 60, TypeName = typeof(RescanFoldersCommand).FullName }, },
new ScheduledTask { Interval = 24 * 60, TypeName = typeof(HousekeepingCommand).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(RefreshArtistCommand).FullName
},
new ScheduledTask
{
Interval = 24 * 60,
TypeName = typeof(RescanFoldersCommand).FullName
},
new ScheduledTask
{
Interval = 24 * 60,
TypeName = typeof(HousekeepingCommand).FullName
},
new ScheduledTask new ScheduledTask
{ {
@ -90,7 +129,7 @@ namespace NzbDrone.Core.Jobs
var currentTasks = _scheduledTaskRepository.All().ToList(); 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) foreach (var job in currentTasks)
{ {
@ -112,6 +151,9 @@ namespace NzbDrone.Core.Jobs
currentDefinition.LastExecution = DateTime.UtcNow; currentDefinition.LastExecution = DateTime.UtcNow;
} }
currentDefinition.Priority = defaultTask.Priority;
_cache.Set(currentDefinition.TypeName, currentDefinition);
_scheduledTaskRepository.Upsert(currentDefinition); _scheduledTaskRepository.Upsert(currentDefinition);
} }
} }
@ -170,6 +212,9 @@ namespace NzbDrone.Core.Jobs
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;
} }
} }
} }

View file

@ -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;
}
}
}