From 2491da067815e129df3a3a79c0cc7221a9d87094 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Tue, 27 Dec 2022 01:07:15 -0800 Subject: [PATCH] Fixed: Don't try to remove the same item from queue multiple times --- src/Sonarr.Api.V3/Queue/QueueController.cs | 66 ++++++++++++++-------- 1 file changed, 44 insertions(+), 22 deletions(-) diff --git a/src/Sonarr.Api.V3/Queue/QueueController.cs b/src/Sonarr.Api.V3/Queue/QueueController.cs index 4925b9618..ed3a2c2ba 100644 --- a/src/Sonarr.Api.V3/Queue/QueueController.cs +++ b/src/Sonarr.Api.V3/Queue/QueueController.cs @@ -66,29 +66,62 @@ namespace Sonarr.Api.V3.Queue [RestDeleteById] public void RemoveAction(int id, bool removeFromClient = true, bool blocklist = false) { - var trackedDownload = Remove(id, removeFromClient, blocklist); + var pendingRelease = _pendingReleaseService.FindPendingQueueItem(id); - if (trackedDownload != null) + if (pendingRelease != null) { - _trackedDownloadService.StopTracking(trackedDownload.DownloadItem.DownloadId); + Remove(pendingRelease); + + return; } + + var trackedDownload = GetTrackedDownload(id); + + if (trackedDownload == null) + { + throw new NotFoundException(); + } + + Remove(trackedDownload, removeFromClient, blocklist); + _trackedDownloadService.StopTracking(trackedDownload.DownloadItem.DownloadId); } [HttpDelete("bulk")] public object RemoveMany([FromBody] QueueBulkResource resource, [FromQuery] bool removeFromClient = true, [FromQuery] bool blocklist = false) { var trackedDownloadIds = new List(); + var pendingToRemove = new List(); + var trackedToRemove = new List(); foreach (var id in resource.Ids) { - var trackedDownload = Remove(id, removeFromClient, blocklist); + var pendingRelease = _pendingReleaseService.FindPendingQueueItem(id); + + if (pendingRelease != null) + { + pendingToRemove.Add(pendingRelease); + continue; + } + + var trackedDownload = GetTrackedDownload(id); if (trackedDownload != null) { - trackedDownloadIds.Add(trackedDownload.DownloadItem.DownloadId); + trackedToRemove.Add(trackedDownload); } } + foreach (var pendingRelease in pendingToRemove.DistinctBy(p => p.Id)) + { + Remove(pendingRelease); + } + + foreach (var trackedDownload in trackedToRemove.DistinctBy(t => t.DownloadItem.DownloadId)) + { + Remove(trackedDownload, removeFromClient, blocklist); + trackedDownloadIds.Add(trackedDownload.DownloadItem.DownloadId); + } + _trackedDownloadService.StopTracking(trackedDownloadIds); return new { }; @@ -208,25 +241,14 @@ namespace Sonarr.Api.V3.Queue } } - private TrackedDownload Remove(int id, bool removeFromClient, bool blocklist) + private void Remove(NzbDrone.Core.Queue.Queue pendingRelease) { - var pendingRelease = _pendingReleaseService.FindPendingQueueItem(id); - - if (pendingRelease != null) - { - _blocklistService.Block(pendingRelease.RemoteEpisode, "Pending release manually blocklisted"); - _pendingReleaseService.RemovePendingQueueItems(pendingRelease.Id); - - return null; - } - - var trackedDownload = GetTrackedDownload(id); - - if (trackedDownload == null) - { - throw new NotFoundException(); - } + _blocklistService.Block(pendingRelease.RemoteEpisode, "Pending release manually blocklisted"); + _pendingReleaseService.RemovePendingQueueItems(pendingRelease.Id); + } + private TrackedDownload Remove(TrackedDownload trackedDownload, bool removeFromClient, bool blocklist) + { if (removeFromClient) { var downloadClient = _downloadClientProvider.Get(trackedDownload.DownloadClient);