diff --git a/src/NzbDrone.Core.Test/Download/DownloadApprovedReportsTests/DownloadApprovedFixture.cs b/src/NzbDrone.Core.Test/Download/DownloadApprovedReportsTests/DownloadApprovedFixture.cs index 32035897a..391d50410 100644 --- a/src/NzbDrone.Core.Test/Download/DownloadApprovedReportsTests/DownloadApprovedFixture.cs +++ b/src/NzbDrone.Core.Test/Download/DownloadApprovedReportsTests/DownloadApprovedFixture.cs @@ -212,7 +212,7 @@ namespace NzbDrone.Core.Test.Download.DownloadApprovedReportsTests decisions.Add(new DownloadDecision(remoteEpisode, new Rejection("Failure!", RejectionType.Temporary))); Subject.ProcessDecisions(decisions); - Mocker.GetMock().Verify(v => v.Add(It.IsAny(), It.IsAny()), Times.Never()); + Mocker.GetMock().Verify(v => v.AddMany(It.IsAny>>()), Times.Never()); } [Test] @@ -226,7 +226,7 @@ namespace NzbDrone.Core.Test.Download.DownloadApprovedReportsTests decisions.Add(new DownloadDecision(remoteEpisode, new Rejection("Failure!", RejectionType.Temporary))); Subject.ProcessDecisions(decisions); - Mocker.GetMock().Verify(v => v.Add(It.IsAny(), It.IsAny()), Times.Exactly(2)); + Mocker.GetMock().Verify(v => v.AddMany(It.IsAny>>()), Times.Once()); } [Test] diff --git a/src/NzbDrone.Core/Download/ProcessDownloadDecisions.cs b/src/NzbDrone.Core/Download/ProcessDownloadDecisions.cs index 5614121b2..eebb59d2b 100644 --- a/src/NzbDrone.Core/Download/ProcessDownloadDecisions.cs +++ b/src/NzbDrone.Core/Download/ProcessDownloadDecisions.cs @@ -40,9 +40,10 @@ namespace NzbDrone.Core.Download var prioritizedDecisions = _prioritizeDownloadDecision.PrioritizeDecisions(qualifiedReports); var grabbed = new List(); var pending = new List(); - var failed = new List(); var rejected = decisions.Where(d => d.Rejected).ToList(); + var pendingAddQueue = new List>(); + var usenetFailed = false; var torrentFailed = false; @@ -59,15 +60,14 @@ namespace NzbDrone.Core.Download if (report.TemporarilyRejected) { - _pendingReleaseService.Add(report, PendingReleaseReason.Delay); - pending.Add(report); + PreparePending(pendingAddQueue, grabbed, pending, report, PendingReleaseReason.Delay); continue; } if (downloadProtocol == DownloadProtocol.Usenet && usenetFailed || downloadProtocol == DownloadProtocol.Torrent && torrentFailed) { - failed.Add(report); + PreparePending(pendingAddQueue, grabbed, pending, report, PendingReleaseReason.DownloadClientUnavailable); continue; } @@ -86,7 +86,7 @@ namespace NzbDrone.Core.Download if (ex is DownloadClientUnavailableException || ex is DownloadClientAuthenticationException) { _logger.Debug(ex, "Failed to send release to download client, storing until later. " + remoteEpisode); - failed.Add(report); + PreparePending(pendingAddQueue, grabbed, pending, report, PendingReleaseReason.DownloadClientUnavailable); if (downloadProtocol == DownloadProtocol.Usenet) { @@ -104,7 +104,10 @@ namespace NzbDrone.Core.Download } } - pending.AddRange(ProcessFailedGrabs(grabbed, failed)); + if (pendingAddQueue.Any()) + { + _pendingReleaseService.AddMany(pendingAddQueue); + } return new ProcessedDecisions(grabbed, pending, rejected); } @@ -126,45 +129,22 @@ namespace NzbDrone.Core.Download .Any(); } - private List ProcessFailedGrabs(List grabbed, List failed) + private void PreparePending(List> queue, List grabbed, List pending, DownloadDecision report, PendingReleaseReason reason) { - var pending = new List(); - var stored = new List(); - - var addQueue = new List>(); - - foreach (var report in failed) - { - // If a release was already grabbed with matching episodes we should store it as a fallback - // and filter it out the next time it is processed incase a higher quality release failed to - // add to the download client, but a lower quality release was sent to another client - // If the release wasn't grabbed already, but was already stored, store it as a fallback, - // otherwise store it as DownloadClientUnavailable. - - if (IsEpisodeProcessed(grabbed, report)) - { - addQueue.Add(Tuple.Create(report, PendingReleaseReason.Fallback)); - pending.Add(report); - } - else if (IsEpisodeProcessed(stored, report)) - { - addQueue.Add(Tuple.Create(report, PendingReleaseReason.Fallback)); - pending.Add(report); - } - else - { - addQueue.Add(Tuple.Create(report, PendingReleaseReason.DownloadClientUnavailable)); - pending.Add(report); - stored.Add(report); - } - } - - if (addQueue.Any()) + // If a release was already grabbed with matching episodes we should store it as a fallback + // and filter it out the next time it is processed. + // If a higher quality release failed to add to the download client, but a lower quality release + // was sent to another client we still list it normally so it apparent that it'll grab next time. + // Delayed is treated the same, but only the first is listed the subsequent items as stored as Fallback. + + if (IsEpisodeProcessed(grabbed, report) || + IsEpisodeProcessed(pending, report)) { - _pendingReleaseService.AddMany(addQueue); + reason = PendingReleaseReason.Fallback; } - return pending; + queue.Add(Tuple.Create(report, reason)); + pending.Add(report); } } }