Fixed: Remove seeded downloads if they've finished seeding after import

Fixes #3693
This commit is contained in:
Mark McDowall 2020-04-25 11:49:18 -07:00
parent 1498f4e361
commit 9d766cfed5
7 changed files with 68 additions and 24 deletions

View File

@ -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;
}
}
}

View File

@ -6,7 +6,7 @@ using NzbDrone.Core.ThingiProvider;
namespace NzbDrone.Core.Download
{
[DebuggerDisplay("{DownloadClientName}:{Title}")]
[DebuggerDisplay("{DownloadClientInfo?.Name}:{Title}")]
public class DownloadClientItem
{
public DownloadClientItemClientInfo DownloadClientInfo { get; set; }

View File

@ -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;
}
}
}

View File

@ -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<DownloadFailedEvent>,
IHandle<DownloadCompletedEvent>
IHandle<DownloadCompletedEvent>,
IHandle<DownloadCanBeRemovedEvent>
{
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,19 +50,12 @@ 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)
{
var downloadClient = _downloadClientProvider.Get(trackedDownload.DownloadClient);

View File

@ -33,9 +33,18 @@ namespace NzbDrone.Core.Download
_logger = logger;
}
private void RemoveCompletedDownloads(List<TrackedDownload> 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();
@ -47,11 +56,21 @@ namespace NzbDrone.Core.Download
if (trackedDownload.State == TrackedDownloadState.FailedPending)
{
_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)

View File

@ -116,6 +116,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);

View File

@ -32,7 +32,6 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport.Specifications
return Decision.Accept();
}
var test = _historyService.FindByDownloadId(downloadClientItem.DownloadId);
var grabbedEpisodeHistory = _historyService.FindByDownloadId(downloadClientItem.DownloadId)
.OrderByDescending(h => h.Date)
.FirstOrDefault(h => h.EventType == EpisodeHistoryEventType.Grabbed);