Fixed: Marking queued item as failed not blocking the correct Torrent Info Hash

(cherry picked from commit 4b186e894e4e229a435c077e00c65b67ca178333)

Fixes #4977
Closes #4988
This commit is contained in:
Mark McDowall 2024-08-11 08:48:22 -07:00 committed by Bogdan
commit 6e57c14e57
3 changed files with 31 additions and 25 deletions

View file

@ -302,7 +302,7 @@ namespace Lidarr.Api.V1.Queue
if (blocklist) if (blocklist)
{ {
_failedDownloadService.MarkAsFailed(trackedDownload.DownloadItem.DownloadId, skipRedownload); _failedDownloadService.MarkAsFailed(trackedDownload, skipRedownload);
} }
if (!removeFromClient && !blocklist && !changeCategory) if (!removeFromClient && !blocklist && !changeCategory)

View file

@ -185,7 +185,9 @@ namespace NzbDrone.Core.Blocklisting
Indexer = message.Data.GetValueOrDefault("indexer"), Indexer = message.Data.GetValueOrDefault("indexer"),
Protocol = (DownloadProtocol)Convert.ToInt32(message.Data.GetValueOrDefault("protocol")), Protocol = (DownloadProtocol)Convert.ToInt32(message.Data.GetValueOrDefault("protocol")),
Message = message.Message, Message = message.Message,
TorrentInfoHash = message.Data.GetValueOrDefault("torrentInfoHash") 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)) if (Enum.TryParse(message.Data.GetValueOrDefault("indexerFlags"), true, out IndexerFlags flags))

View file

@ -12,7 +12,7 @@ namespace NzbDrone.Core.Download
public interface IFailedDownloadService public interface IFailedDownloadService
{ {
void MarkAsFailed(int historyId, bool skipRedownload = false); 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 Check(TrackedDownload trackedDownload);
void ProcessFailed(TrackedDownload trackedDownload); void ProcessFailed(TrackedDownload trackedDownload);
} }
@ -20,15 +20,12 @@ namespace NzbDrone.Core.Download
public class FailedDownloadService : IFailedDownloadService public class FailedDownloadService : IFailedDownloadService
{ {
private readonly IHistoryService _historyService; private readonly IHistoryService _historyService;
private readonly ITrackedDownloadService _trackedDownloadService;
private readonly IEventAggregator _eventAggregator; private readonly IEventAggregator _eventAggregator;
public FailedDownloadService(IHistoryService historyService, public FailedDownloadService(IHistoryService historyService,
ITrackedDownloadService trackedDownloadService,
IEventAggregator eventAggregator) IEventAggregator eventAggregator)
{ {
_historyService = historyService; _historyService = historyService;
_trackedDownloadService = trackedDownloadService;
_eventAggregator = eventAggregator; _eventAggregator = eventAggregator;
} }
@ -37,9 +34,10 @@ namespace NzbDrone.Core.Download
var history = _historyService.Get(historyId); var history = _historyService.Get(historyId);
var downloadId = history.DownloadId; var downloadId = history.DownloadId;
if (downloadId.IsNullOrWhiteSpace()) if (downloadId.IsNullOrWhiteSpace())
{ {
PublishDownloadFailedEvent(new List<EntityHistory> { history }, "Manually marked as failed", skipRedownload: skipRedownload); PublishDownloadFailedEvent(history, new List<int> { history.AlbumId }, "Manually marked as failed", skipRedownload: skipRedownload);
return; return;
} }
@ -53,21 +51,19 @@ namespace NzbDrone.Core.Download
} }
// Add any other history items for the download ID then filter out any duplicate history items. // Add any other history items for the download ID then filter out any duplicate history items.
grabbedHistory.AddRange(_historyService.Find(downloadId, EntityHistoryEventType.Grabbed)); grabbedHistory.AddRange(GetGrabbedHistory(downloadId));
grabbedHistory = grabbedHistory.DistinctBy(h => h.Id).ToList(); grabbedHistory = grabbedHistory.DistinctBy(h => h.Id).ToList();
PublishDownloadFailedEvent(grabbedHistory, "Manually marked as failed"); PublishDownloadFailedEvent(history, GetAlbumIds(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, EntityHistoryEventType.Grabbed); var history = GetGrabbedHistory(trackedDownload.DownloadItem.DownloadId);
if (history.Any()) if (history.Any())
{ {
var trackedDownload = _trackedDownloadService.Find(downloadId); PublishDownloadFailedEvent(history.First(), GetAlbumIds(history), "Manually marked as failed", trackedDownload, skipRedownload: skipRedownload);
PublishDownloadFailedEvent(history, "Manually marked as failed", trackedDownload, skipRedownload: skipRedownload);
} }
} }
@ -82,9 +78,7 @@ namespace NzbDrone.Core.Download
if (trackedDownload.DownloadItem.IsEncrypted || if (trackedDownload.DownloadItem.IsEncrypted ||
trackedDownload.DownloadItem.Status == DownloadItemStatus.Failed) trackedDownload.DownloadItem.Status == DownloadItemStatus.Failed)
{ {
var grabbedItems = _historyService var grabbedItems = GetGrabbedHistory(trackedDownload.DownloadItem.DownloadId);
.Find(trackedDownload.DownloadItem.DownloadId, EntityHistoryEventType.Grabbed)
.ToList();
if (grabbedItems.Empty()) if (grabbedItems.Empty())
{ {
@ -103,9 +97,7 @@ namespace NzbDrone.Core.Download
return; return;
} }
var grabbedItems = _historyService var grabbedItems = GetGrabbedHistory(trackedDownload.DownloadItem.DownloadId);
.Find(trackedDownload.DownloadItem.DownloadId, EntityHistoryEventType.Grabbed)
.ToList();
if (grabbedItems.Empty()) if (grabbedItems.Empty())
{ {
@ -124,18 +116,17 @@ namespace NzbDrone.Core.Download
} }
trackedDownload.State = TrackedDownloadState.DownloadFailed; trackedDownload.State = TrackedDownloadState.DownloadFailed;
PublishDownloadFailedEvent(grabbedItems, failure, trackedDownload); PublishDownloadFailedEvent(grabbedItems.First(), GetAlbumIds(grabbedItems), failure, trackedDownload);
} }
private void PublishDownloadFailedEvent(List<EntityHistory> historyItems, string message, TrackedDownload trackedDownload = null, bool skipRedownload = false) private void PublishDownloadFailedEvent(EntityHistory historyItem, List<int> albumIds, string message, TrackedDownload trackedDownload = null, bool skipRedownload = false)
{ {
var historyItem = historyItems.Last();
Enum.TryParse(historyItem.Data.GetValueOrDefault(EntityHistory.RELEASE_SOURCE, ReleaseSourceType.Unknown.ToString()), out ReleaseSourceType releaseSource); Enum.TryParse(historyItem.Data.GetValueOrDefault(EntityHistory.RELEASE_SOURCE, ReleaseSourceType.Unknown.ToString()), out ReleaseSourceType releaseSource);
var downloadFailedEvent = new DownloadFailedEvent var downloadFailedEvent = new DownloadFailedEvent
{ {
ArtistId = historyItem.ArtistId, ArtistId = historyItem.ArtistId,
AlbumIds = historyItems.Select(h => h.AlbumId).Distinct().ToList(), AlbumIds = albumIds,
Quality = historyItem.Quality, Quality = historyItem.Quality,
SourceTitle = historyItem.SourceTitle, SourceTitle = historyItem.SourceTitle,
DownloadClient = historyItem.Data.GetValueOrDefault(EntityHistory.DOWNLOAD_CLIENT), DownloadClient = historyItem.Data.GetValueOrDefault(EntityHistory.DOWNLOAD_CLIENT),
@ -144,10 +135,23 @@ namespace NzbDrone.Core.Download
Data = historyItem.Data, Data = historyItem.Data,
TrackedDownload = trackedDownload, TrackedDownload = trackedDownload,
SkipRedownload = skipRedownload, SkipRedownload = skipRedownload,
ReleaseSource = releaseSource ReleaseSource = releaseSource,
}; };
_eventAggregator.PublishEvent(downloadFailedEvent); _eventAggregator.PublishEvent(downloadFailedEvent);
} }
private List<int> GetAlbumIds(List<EntityHistory> historyItems)
{
return historyItems.Select(h => h.AlbumId).Distinct().ToList();
}
private List<EntityHistory> GetGrabbedHistory(string downloadId)
{
// Sort by date so items are always in the same order
return _historyService.Find(downloadId, EntityHistoryEventType.Grabbed)
.OrderByDescending(h => h.Date)
.ToList();
}
} }
} }