Fixed: Refresh cache for tracked queue on artist/album add or removal

Prevents a NullRef in CompletedDownloadService.VerifyImport when publishing DownloadCompletedEvent for an already cached tracked download
This commit is contained in:
Bogdan 2024-09-18 22:43:12 +03:00
commit 031f32a52c

View file

@ -16,7 +16,7 @@ using NzbDrone.Core.Parser.Model;
namespace NzbDrone.Core.Download.TrackedDownloads namespace NzbDrone.Core.Download.TrackedDownloads
{ {
public interface ITrackedDownloadService : IHandle<AlbumDeletedEvent> public interface ITrackedDownloadService
{ {
TrackedDownload Find(string downloadId); TrackedDownload Find(string downloadId);
void StopTracking(string downloadId); void StopTracking(string downloadId);
@ -26,13 +26,16 @@ namespace NzbDrone.Core.Download.TrackedDownloads
void UpdateTrackable(List<TrackedDownload> trackedDownloads); void UpdateTrackable(List<TrackedDownload> trackedDownloads);
} }
public class TrackedDownloadService : ITrackedDownloadService public class TrackedDownloadService : ITrackedDownloadService,
IHandle<AlbumInfoRefreshedEvent>,
IHandle<AlbumDeletedEvent>,
IHandle<ArtistAddedEvent>,
IHandle<ArtistsDeletedEvent>
{ {
private readonly IParsingService _parsingService; private readonly IParsingService _parsingService;
private readonly IHistoryService _historyService; private readonly IHistoryService _historyService;
private readonly IEventAggregator _eventAggregator; private readonly IEventAggregator _eventAggregator;
private readonly IDownloadHistoryService _downloadHistoryService; private readonly IDownloadHistoryService _downloadHistoryService;
private readonly ITrackedDownloadAlreadyImported _trackedDownloadAlreadyImported;
private readonly ICustomFormatCalculationService _formatCalculator; private readonly ICustomFormatCalculationService _formatCalculator;
private readonly Logger _logger; private readonly Logger _logger;
private readonly ICached<TrackedDownload> _cache; private readonly ICached<TrackedDownload> _cache;
@ -43,7 +46,6 @@ namespace NzbDrone.Core.Download.TrackedDownloads
ICustomFormatCalculationService formatCalculator, ICustomFormatCalculationService formatCalculator,
IEventAggregator eventAggregator, IEventAggregator eventAggregator,
IDownloadHistoryService downloadHistoryService, IDownloadHistoryService downloadHistoryService,
ITrackedDownloadAlreadyImported trackedDownloadAlreadyImported,
Logger logger) Logger logger)
{ {
_parsingService = parsingService; _parsingService = parsingService;
@ -51,7 +53,6 @@ namespace NzbDrone.Core.Download.TrackedDownloads
_cache = cacheManager.GetCache<TrackedDownload>(GetType()); _cache = cacheManager.GetCache<TrackedDownload>(GetType());
_formatCalculator = formatCalculator; _formatCalculator = formatCalculator;
_eventAggregator = eventAggregator; _eventAggregator = eventAggregator;
_trackedDownloadAlreadyImported = trackedDownloadAlreadyImported;
_downloadHistoryService = downloadHistoryService; _downloadHistoryService = downloadHistoryService;
_logger = logger; _logger = logger;
} }
@ -264,18 +265,6 @@ namespace NzbDrone.Core.Download.TrackedDownloads
} }
} }
public void Handle(AlbumDeletedEvent message)
{
var cachedItems = _cache.Values.Where(x => x.RemoteAlbum != null && x.RemoteAlbum.Albums.Any(a => a.Id == message.Album.Id)).ToList();
if (cachedItems.Any())
{
cachedItems.ForEach(UpdateCachedItem);
_eventAggregator.PublishEvent(new TrackedDownloadRefreshedEvent(GetTrackedDownloads()));
}
}
public void Handle(AlbumInfoRefreshedEvent message) public void Handle(AlbumInfoRefreshedEvent message)
{ {
var needsToUpdate = false; var needsToUpdate = false;
@ -301,11 +290,44 @@ namespace NzbDrone.Core.Download.TrackedDownloads
} }
} }
public void Handle(AlbumDeletedEvent message)
{
var cachedItems = _cache.Values
.Where(t =>
t.RemoteAlbum?.Albums != null &&
t.RemoteAlbum.Albums.Any(a => a.Id == message.Album.Id || a.ForeignAlbumId == message.Album.ForeignAlbumId))
.ToList();
if (cachedItems.Any())
{
cachedItems.ForEach(UpdateCachedItem);
_eventAggregator.PublishEvent(new TrackedDownloadRefreshedEvent(GetTrackedDownloads()));
}
}
public void Handle(ArtistAddedEvent message)
{
var cachedItems = _cache.Values
.Where(t =>
t.RemoteAlbum?.Artist == null ||
message.Artist?.ForeignArtistId == t.RemoteAlbum.Artist.ForeignArtistId)
.ToList();
if (cachedItems.Any())
{
cachedItems.ForEach(UpdateCachedItem);
_eventAggregator.PublishEvent(new TrackedDownloadRefreshedEvent(GetTrackedDownloads()));
}
}
public void Handle(ArtistsDeletedEvent message) public void Handle(ArtistsDeletedEvent message)
{ {
var cachedItems = _cache.Values.Where(t => var cachedItems = _cache.Values
.Where(t =>
t.RemoteAlbum?.Artist != null && t.RemoteAlbum?.Artist != null &&
message.Artists.Select(a => a.Id).Contains(t.RemoteAlbum.Artist.Id)) message.Artists.Any(a => a.Id == t.RemoteAlbum.Artist.Id || a.ForeignArtistId == t.RemoteAlbum.Artist.ForeignArtistId))
.ToList(); .ToList();
if (cachedItems.Any()) if (cachedItems.Any())