From 162cbf01edb03f621bdfd7cc11494ef073727f9c Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sat, 25 Apr 2020 11:49:18 -0700 Subject: [PATCH] Fixed: Remove seeded downloads if they've finished seeding after import Closes #1389 (cherry picked from commit 9d766cfed588ee49525f68815b7e5eb980ddc11f) --- .../Download/DownloadCanBeRemovedEvent.cs | 15 ++++++++ .../Download/DownloadCompletedEvent.cs | 15 ++++++++ .../Download/DownloadEventHub.cs | 38 +++++++++---------- .../Download/DownloadProcessingService.cs | 19 ++++++++++ .../DownloadMonitoringService.cs | 1 + 5 files changed, 67 insertions(+), 21 deletions(-) create mode 100644 src/NzbDrone.Core/Download/DownloadCanBeRemovedEvent.cs create mode 100644 src/NzbDrone.Core/Download/DownloadCompletedEvent.cs diff --git a/src/NzbDrone.Core/Download/DownloadCanBeRemovedEvent.cs b/src/NzbDrone.Core/Download/DownloadCanBeRemovedEvent.cs new file mode 100644 index 000000000..b852ee63d --- /dev/null +++ b/src/NzbDrone.Core/Download/DownloadCanBeRemovedEvent.cs @@ -0,0 +1,15 @@ +using NzbDrone.Common.Messaging; +using NzbDrone.Core.Download.TrackedDownloads; + +namespace NzbDrone.Core.Download +{ + public class DownloadCanBeRemovedEvent : IEvent + { + public TrackedDownload TrackedDownload { get; private set; } + + public DownloadCanBeRemovedEvent(TrackedDownload trackedDownload) + { + TrackedDownload = trackedDownload; + } + } +} diff --git a/src/NzbDrone.Core/Download/DownloadCompletedEvent.cs b/src/NzbDrone.Core/Download/DownloadCompletedEvent.cs new file mode 100644 index 000000000..0957ed860 --- /dev/null +++ b/src/NzbDrone.Core/Download/DownloadCompletedEvent.cs @@ -0,0 +1,15 @@ +using NzbDrone.Common.Messaging; +using NzbDrone.Core.Download.TrackedDownloads; + +namespace NzbDrone.Core.Download +{ + public class DownloadCompletedEvent : IEvent + { + public TrackedDownload TrackedDownload { get; private set; } + + public DownloadCompletedEvent(TrackedDownload trackedDownload) + { + TrackedDownload = trackedDownload; + } + } +} diff --git a/src/NzbDrone.Core/Download/DownloadEventHub.cs b/src/NzbDrone.Core/Download/DownloadEventHub.cs index 2c2ef8181..e75fdbc1f 100644 --- a/src/NzbDrone.Core/Download/DownloadEventHub.cs +++ b/src/NzbDrone.Core/Download/DownloadEventHub.cs @@ -1,24 +1,14 @@ using System; using NLog; -using NzbDrone.Common.Messaging; using NzbDrone.Core.Configuration; using NzbDrone.Core.Download.TrackedDownloads; using NzbDrone.Core.Messaging.Events; namespace NzbDrone.Core.Download { - public class DownloadCompletedEvent : IEvent - { - public TrackedDownload TrackedDownload { get; private set; } - - public DownloadCompletedEvent(TrackedDownload trackedDownload) - { - TrackedDownload = trackedDownload; - } - } - public class DownloadEventHub : IHandle, - IHandle + IHandle, + IHandle { private readonly IConfigService _configService; private readonly IProvideDownloadClient _downloadClientProvider; @@ -33,6 +23,18 @@ namespace NzbDrone.Core.Download _logger = logger; } + public void Handle(DownloadFailedEvent message) + { + var trackedDownload = message.TrackedDownload; + + if (trackedDownload == null || !trackedDownload.DownloadItem.CanBeRemoved || _configService.RemoveFailedDownloads == false) + { + return; + } + + RemoveFromDownloadClient(trackedDownload); + } + public void Handle(DownloadCompletedEvent message) { if (_configService.RemoveCompletedDownloads && @@ -48,16 +50,10 @@ namespace NzbDrone.Core.Download } } - public void Handle(DownloadFailedEvent message) + public void Handle(DownloadCanBeRemovedEvent message) { - var trackedDownload = message.TrackedDownload; - - if (trackedDownload == null || !trackedDownload.DownloadItem.CanBeRemoved || _configService.RemoveFailedDownloads == false) - { - return; - } - - RemoveFromDownloadClient(trackedDownload); + // Already verified that it can be removed, just needs to be removed + RemoveFromDownloadClient(message.TrackedDownload); } private void RemoveFromDownloadClient(TrackedDownload trackedDownload) diff --git a/src/NzbDrone.Core/Download/DownloadProcessingService.cs b/src/NzbDrone.Core/Download/DownloadProcessingService.cs index 535027e52..067c37da4 100644 --- a/src/NzbDrone.Core/Download/DownloadProcessingService.cs +++ b/src/NzbDrone.Core/Download/DownloadProcessingService.cs @@ -31,9 +31,18 @@ namespace NzbDrone.Core.Download _logger = logger; } + private void RemoveCompletedDownloads(List trackedDownloads) + { + foreach (var trackedDownload in trackedDownloads.Where(c => c.DownloadItem.CanBeRemoved && c.State == TrackedDownloadState.Imported)) + { + _eventAggregator.PublishEvent(new DownloadCompletedEvent(trackedDownload)); + } + } + public void Execute(ProcessMonitoredDownloadsCommand message) { var enableCompletedDownloadHandling = _configService.EnableCompletedDownloadHandling; + var removeCompletedDownloads = _configService.RemoveCompletedDownloads; var trackedDownloads = _trackedDownloadService.GetTrackedDownloads() .Where(t => t.IsTrackable) .ToList(); @@ -45,11 +54,21 @@ namespace NzbDrone.Core.Download if (trackedDownload.State == TrackedDownloadState.DownloadFailedPending) { _failedDownloadService.ProcessFailed(trackedDownload); + continue; } if (enableCompletedDownloadHandling && trackedDownload.State == TrackedDownloadState.ImportPending) { _completedDownloadService.Import(trackedDownload); + continue; + } + + if (removeCompletedDownloads && + trackedDownload.DownloadItem.Removed && + trackedDownload.DownloadItem.CanBeRemoved && + trackedDownload.State == TrackedDownloadState.Imported) + { + _eventAggregator.PublishEvent(new DownloadCanBeRemovedEvent(trackedDownload)); } } catch (Exception e) diff --git a/src/NzbDrone.Core/Download/TrackedDownloads/DownloadMonitoringService.cs b/src/NzbDrone.Core/Download/TrackedDownloads/DownloadMonitoringService.cs index 8e7cea905..4641dd2e5 100644 --- a/src/NzbDrone.Core/Download/TrackedDownloads/DownloadMonitoringService.cs +++ b/src/NzbDrone.Core/Download/TrackedDownloads/DownloadMonitoringService.cs @@ -114,6 +114,7 @@ namespace NzbDrone.Core.Download.TrackedDownloads try { var trackedDownload = _trackedDownloadService.TrackDownload((DownloadClientDefinition)downloadClient.Definition, downloadItem); + if (trackedDownload != null && trackedDownload.State == TrackedDownloadState.Downloading) { _failedDownloadService.Check(trackedDownload);