From 4b186e894e4e229a435c077e00c65b67ca178333 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sun, 11 Aug 2024 08:48:22 -0700 Subject: [PATCH] Fixed: Marking queued item as failed not blocking the correct Torrent Info Hash --- .../Blocklisting/BlocklistService.cs | 6 ++- .../Download/FailedDownloadService.cs | 49 ++++++++++--------- src/Sonarr.Api.V3/Queue/QueueController.cs | 2 +- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/NzbDrone.Core/Blocklisting/BlocklistService.cs b/src/NzbDrone.Core/Blocklisting/BlocklistService.cs index 0ec53522c..f6aa6ceef 100644 --- a/src/NzbDrone.Core/Blocklisting/BlocklistService.cs +++ b/src/NzbDrone.Core/Blocklisting/BlocklistService.cs @@ -185,8 +185,10 @@ namespace NzbDrone.Core.Blocklisting Indexer = message.Data.GetValueOrDefault("indexer"), Protocol = (DownloadProtocol)Convert.ToInt32(message.Data.GetValueOrDefault("protocol")), Message = message.Message, - TorrentInfoHash = message.Data.GetValueOrDefault("torrentInfoHash"), - Languages = message.Languages + Languages = message.Languages, + TorrentInfoHash = message.TrackedDownload?.Protocol == DownloadProtocol.Torrent + ? message.TrackedDownload.DownloadItem.DownloadId + : message.Data.GetValueOrDefault("torrentInfoHash", null) }; if (Enum.TryParse(message.Data.GetValueOrDefault("indexerFlags"), true, out IndexerFlags flags)) diff --git a/src/NzbDrone.Core/Download/FailedDownloadService.cs b/src/NzbDrone.Core/Download/FailedDownloadService.cs index d392f0ea4..163035b6a 100644 --- a/src/NzbDrone.Core/Download/FailedDownloadService.cs +++ b/src/NzbDrone.Core/Download/FailedDownloadService.cs @@ -12,7 +12,7 @@ namespace NzbDrone.Core.Download public interface IFailedDownloadService { void MarkAsFailed(int historyId, bool skipRedownload = false); - void MarkAsFailed(string downloadId, bool skipRedownload = false); + void MarkAsFailed(TrackedDownload trackedDownload, bool skipRedownload = false); void Check(TrackedDownload trackedDownload); void ProcessFailed(TrackedDownload trackedDownload); } @@ -20,7 +20,6 @@ namespace NzbDrone.Core.Download public class FailedDownloadService : IFailedDownloadService { private readonly IHistoryService _historyService; - private readonly ITrackedDownloadService _trackedDownloadService; private readonly IEventAggregator _eventAggregator; public FailedDownloadService(IHistoryService historyService, @@ -28,7 +27,6 @@ namespace NzbDrone.Core.Download IEventAggregator eventAggregator) { _historyService = historyService; - _trackedDownloadService = trackedDownloadService; _eventAggregator = eventAggregator; } @@ -37,9 +35,10 @@ namespace NzbDrone.Core.Download var history = _historyService.Get(historyId); var downloadId = history.DownloadId; + if (downloadId.IsNullOrWhiteSpace()) { - PublishDownloadFailedEvent(new List { history }, "Manually marked as failed", skipRedownload: skipRedownload); + PublishDownloadFailedEvent(history, new List { history.EpisodeId }, "Manually marked as failed", skipRedownload: skipRedownload); return; } @@ -53,21 +52,19 @@ namespace NzbDrone.Core.Download } // Add any other history items for the download ID then filter out any duplicate history items. - grabbedHistory.AddRange(_historyService.Find(downloadId, EpisodeHistoryEventType.Grabbed)); + grabbedHistory.AddRange(GetGrabbedHistory(downloadId)); grabbedHistory = grabbedHistory.DistinctBy(h => h.Id).ToList(); - PublishDownloadFailedEvent(grabbedHistory, "Manually marked as failed"); + PublishDownloadFailedEvent(history, GetEpisodeIds(grabbedHistory), "Manually marked as failed"); } - public void MarkAsFailed(string downloadId, bool skipRedownload = false) + public void MarkAsFailed(TrackedDownload trackedDownload, bool skipRedownload = false) { - var history = _historyService.Find(downloadId, EpisodeHistoryEventType.Grabbed); + var history = GetGrabbedHistory(trackedDownload.DownloadItem.DownloadId); if (history.Any()) { - var trackedDownload = _trackedDownloadService.Find(downloadId); - - PublishDownloadFailedEvent(history, "Manually marked as failed", trackedDownload, skipRedownload: skipRedownload); + PublishDownloadFailedEvent(history.First(), GetEpisodeIds(history), "Manually marked as failed", trackedDownload, skipRedownload: skipRedownload); } } @@ -82,9 +79,7 @@ namespace NzbDrone.Core.Download if (trackedDownload.DownloadItem.IsEncrypted || trackedDownload.DownloadItem.Status == DownloadItemStatus.Failed) { - var grabbedItems = _historyService - .Find(trackedDownload.DownloadItem.DownloadId, EpisodeHistoryEventType.Grabbed) - .ToList(); + var grabbedItems = GetGrabbedHistory(trackedDownload.DownloadItem.DownloadId); if (grabbedItems.Empty()) { @@ -103,9 +98,7 @@ namespace NzbDrone.Core.Download return; } - var grabbedItems = _historyService - .Find(trackedDownload.DownloadItem.DownloadId, EpisodeHistoryEventType.Grabbed) - .ToList(); + var grabbedItems = GetGrabbedHistory(trackedDownload.DownloadItem.DownloadId); if (grabbedItems.Empty()) { @@ -124,18 +117,17 @@ namespace NzbDrone.Core.Download } trackedDownload.State = TrackedDownloadState.Failed; - PublishDownloadFailedEvent(grabbedItems, failure, trackedDownload); + PublishDownloadFailedEvent(grabbedItems.First(), GetEpisodeIds(grabbedItems), failure, trackedDownload); } - private void PublishDownloadFailedEvent(List historyItems, string message, TrackedDownload trackedDownload = null, bool skipRedownload = false) + private void PublishDownloadFailedEvent(EpisodeHistory historyItem, List episodeIds, string message, TrackedDownload trackedDownload = null, bool skipRedownload = false) { - var historyItem = historyItems.Last(); Enum.TryParse(historyItem.Data.GetValueOrDefault(EpisodeHistory.RELEASE_SOURCE, ReleaseSourceType.Unknown.ToString()), out ReleaseSourceType releaseSource); var downloadFailedEvent = new DownloadFailedEvent { SeriesId = historyItem.SeriesId, - EpisodeIds = historyItems.Select(h => h.EpisodeId).Distinct().ToList(), + EpisodeIds = episodeIds, Quality = historyItem.Quality, SourceTitle = historyItem.SourceTitle, DownloadClient = historyItem.Data.GetValueOrDefault(EpisodeHistory.DOWNLOAD_CLIENT), @@ -145,10 +137,23 @@ namespace NzbDrone.Core.Download TrackedDownload = trackedDownload, Languages = historyItem.Languages, SkipRedownload = skipRedownload, - ReleaseSource = releaseSource + ReleaseSource = releaseSource, }; _eventAggregator.PublishEvent(downloadFailedEvent); } + + private List GetEpisodeIds(List historyItems) + { + return historyItems.Select(h => h.EpisodeId).Distinct().ToList(); + } + + private List GetGrabbedHistory(string downloadId) + { + // Sort by date so items are always in the same order + return _historyService.Find(downloadId, EpisodeHistoryEventType.Grabbed) + .OrderByDescending(h => h.Date) + .ToList(); + } } } diff --git a/src/Sonarr.Api.V3/Queue/QueueController.cs b/src/Sonarr.Api.V3/Queue/QueueController.cs index 34622ad18..2e74dec94 100644 --- a/src/Sonarr.Api.V3/Queue/QueueController.cs +++ b/src/Sonarr.Api.V3/Queue/QueueController.cs @@ -323,7 +323,7 @@ namespace Sonarr.Api.V3.Queue if (blocklist) { - _failedDownloadService.MarkAsFailed(trackedDownload.DownloadItem.DownloadId, skipRedownload); + _failedDownloadService.MarkAsFailed(trackedDownload, skipRedownload); } if (!removeFromClient && !blocklist && !changeCategory)