New: Rebuilt Completed/Failed download handling from scratch

This commit is contained in:
Keivan Beigi
2014-12-18 16:26:42 -08:00
parent 264bb66c16
commit a6d34caf2c
79 changed files with 1221 additions and 2389 deletions
@@ -14,7 +14,5 @@ namespace NzbDrone.Core.MediaFiles.Commands
}
public Boolean SendUpdates { get; set; }
public String Path { get; set; }
public String DownloadClientId { get; set; }
}
}
@@ -4,9 +4,7 @@ using System.IO;
using System.Linq;
using NLog;
using NzbDrone.Common.Disk;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Download;
using NzbDrone.Core.MediaFiles.Commands;
using NzbDrone.Core.MediaFiles.EpisodeImport;
using NzbDrone.Core.Messaging.Commands;
@@ -16,22 +14,16 @@ namespace NzbDrone.Core.MediaFiles
public class DownloadedEpisodesCommandService : IExecute<DownloadedEpisodesScanCommand>
{
private readonly IDownloadedEpisodesImportService _downloadedEpisodesImportService;
private readonly IDownloadTrackingService _downloadTrackingService;
private readonly ICompletedDownloadService _completedDownloadService;
private readonly IDiskProvider _diskProvider;
private readonly IConfigService _configService;
private readonly Logger _logger;
public DownloadedEpisodesCommandService(IDownloadedEpisodesImportService downloadedEpisodesImportService,
IDownloadTrackingService downloadTrackingService,
ICompletedDownloadService completedDownloadService,
IDiskProvider diskProvider,
IConfigService configService,
Logger logger)
{
_downloadedEpisodesImportService = downloadedEpisodesImportService;
_downloadTrackingService = downloadTrackingService;
_completedDownloadService = completedDownloadService;
_diskProvider = diskProvider;
_configService = configService;
_logger = logger;
@@ -56,43 +48,12 @@ namespace NzbDrone.Core.MediaFiles
return _downloadedEpisodesImportService.ProcessRootFolder(new DirectoryInfo(downloadedEpisodesFolder));
}
private List<ImportResult> ProcessFolder(DownloadedEpisodesScanCommand message)
{
if (!_diskProvider.FolderExists(message.Path))
{
_logger.Warn("Folder specified for import scan [{0}] doesn't exist.", message.Path);
return new List<ImportResult>();
}
if (message.DownloadClientId.IsNotNullOrWhiteSpace())
{
var trackedDownload = _downloadTrackingService.GetQueuedDownloads().Where(v => v.DownloadItem.DownloadClientId == message.DownloadClientId).FirstOrDefault();
if (trackedDownload == null)
{
_logger.Warn("External directory scan request for unknown download {0}, attempting normal import. [{1}]", message.DownloadClientId, message.Path);
return _downloadedEpisodesImportService.ProcessFolder(new DirectoryInfo(message.Path));
}
return _completedDownloadService.Import(trackedDownload, message.Path);
}
return _downloadedEpisodesImportService.ProcessFolder(new DirectoryInfo(message.Path));
}
public void Execute(DownloadedEpisodesScanCommand message)
{
List<ImportResult> importResults;
if (message.Path.IsNotNullOrWhiteSpace())
{
importResults = ProcessFolder(message);
}
else
{
importResults = ProcessDroneFactoryFolder();
}
if (importResults == null || !importResults.Any(v => v.Result == ImportResultType.Imported))
var importResults = ProcessDroneFactoryFolder();
if (importResults == null || importResults.All(v => v.Result != ImportResultType.Imported))
{
// Atm we don't report it as a command failure, coz that would cause the download to be failed.
// Changing the message won't do a thing either, coz it will get set to 'Completed' a msec later.
@@ -16,9 +16,7 @@ namespace NzbDrone.Core.MediaFiles
{
List<ImportResult> ProcessRootFolder(DirectoryInfo directoryInfo);
List<ImportResult> ProcessFolder(DirectoryInfo directoryInfo, DownloadClientItem downloadClientItem = null);
List<ImportResult> ProcessFolder(DirectoryInfo directoryInfo, Series series, DownloadClientItem downloadClientItem = null);
List<ImportResult> ProcessFile(FileInfo fileInfo, DownloadClientItem downloadClientItem = null);
List<ImportResult> ProcessFile(FileInfo fileInfo, Series series, DownloadClientItem downloadClientItem = null);
List<ImportResult> ProcessPath(string path, DownloadClientItem downloadClientItem = null);
}
public class DownloadedEpisodesImportService : IDownloadedEpisodesImportService
@@ -88,12 +86,12 @@ namespace NzbDrone.Core.MediaFiles
return ProcessFolder(directoryInfo, series, downloadClientItem);
}
public List<ImportResult> ProcessFolder(DirectoryInfo directoryInfo, Series series,
private List<ImportResult> ProcessFolder(DirectoryInfo directoryInfo, Series series,
DownloadClientItem downloadClientItem = null)
{
if (_seriesService.SeriesPathExists(directoryInfo.FullName))
{
_logger.Warn("Unable to process folder that contains sorted TV Shows");
_logger.Warn("Unable to process folder that is mapped to an existing show");
return new List<ImportResult>();
}
@@ -147,7 +145,7 @@ namespace NzbDrone.Core.MediaFiles
return ProcessFile(fileInfo, series, downloadClientItem);
}
public List<ImportResult> ProcessFile(FileInfo fileInfo, Series series, DownloadClientItem downloadClientItem = null)
private List<ImportResult> ProcessFile(FileInfo fileInfo, Series series, DownloadClientItem downloadClientItem = null)
{
if (downloadClientItem == null)
{
@@ -164,6 +162,16 @@ namespace NzbDrone.Core.MediaFiles
return _importApprovedEpisodes.Import(decisions, true, downloadClientItem);
}
public List<ImportResult> ProcessPath(string path, DownloadClientItem downloadClientItem = null)
{
if (_diskProvider.FolderExists(path))
{
return ProcessFolder(new DirectoryInfo(path), downloadClientItem);
}
return ProcessFile(new FileInfo(path), downloadClientItem);
}
private string GetCleanedUpFolderName(string folder)
{
folder = folder.Replace("_UNPACK_", "")
@@ -99,7 +99,7 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport
if (downloadClientItem != null)
{
_eventAggregator.PublishEvent(new EpisodeImportedEvent(localEpisode, episodeFile, newDownload, downloadClientItem.DownloadClient, downloadClientItem.DownloadClientId));
_eventAggregator.PublishEvent(new EpisodeImportedEvent(localEpisode, episodeFile, newDownload, downloadClientItem.DownloadClient, downloadClientItem.DownloadId));
}
else
{
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Parser.Model;
namespace NzbDrone.Core.MediaFiles.EpisodeImport
@@ -13,7 +14,7 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport
{
get
{
return !Rejections.Any();
return Rejections.Empty();
}
}
@@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using NzbDrone.Common.EnsureThat;
using NzbDrone.Common.Extensions;
namespace NzbDrone.Core.MediaFiles.EpisodeImport
{
@@ -15,14 +14,17 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport
{
get
{
//Approved and imported
if (Errors.Empty()) return ImportResultType.Imported;
if (Errors.Any())
{
if (ImportDecision.Approved)
{
return ImportResultType.Skipped;
}
//Decision was approved, but it was not imported
if (ImportDecision.Approved) return ImportResultType.Skipped;
return ImportResultType.Rejected;
}
//Decision was rejected
return ImportResultType.Rejected;
return ImportResultType.Imported;
}
}
@@ -8,9 +8,9 @@ namespace NzbDrone.Core.MediaFiles.Events
{
public LocalEpisode EpisodeInfo { get; private set; }
public EpisodeFile ImportedEpisode { get; private set; }
public Boolean NewDownload { get; set; }
public String DownloadClient { get; set; }
public String DownloadClientId { get; set; }
public Boolean NewDownload { get; private set; }
public String DownloadClient { get; private set; }
public String DownloadId { get; private set; }
public EpisodeImportedEvent(LocalEpisode episodeInfo, EpisodeFile importedEpisode, bool newDownload)
{
@@ -19,13 +19,13 @@ namespace NzbDrone.Core.MediaFiles.Events
NewDownload = newDownload;
}
public EpisodeImportedEvent(LocalEpisode episodeInfo, EpisodeFile importedEpisode, bool newDownload, string downloadClient, string downloadClientId)
public EpisodeImportedEvent(LocalEpisode episodeInfo, EpisodeFile importedEpisode, bool newDownload, string downloadClient, string downloadId)
{
EpisodeInfo = episodeInfo;
ImportedEpisode = importedEpisode;
NewDownload = newDownload;
DownloadClient = downloadClient;
DownloadClientId = downloadClientId;
DownloadId = downloadId;
}
}
}