Fixed: Exception thrown when marking download as complete

Co-Authored-By: Mark McDowall <markus101@users.noreply.github.com>
pull/2/head
Qstick 4 years ago
parent 566fa8b132
commit 2328b384e2

@ -56,6 +56,10 @@ namespace NzbDrone.Core.Test.Download
Mocker.GetMock<IParsingService>()
.Setup(s => s.GetMovie("Drone.1998"))
.Returns(remoteMovie.Movie);
Mocker.GetMock<IHistoryService>()
.Setup(s => s.FindByDownloadId(It.IsAny<string>()))
.Returns(new List<MovieHistory>());
}
private RemoteMovie BuildRemoteMovie()

@ -2,8 +2,10 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using NLog;
using NLog.Fluent;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Instrumentation.Extensions;
using NzbDrone.Core.Download.TrackedDownloads;
using NzbDrone.Core.History;
using NzbDrone.Core.MediaFiles;
@ -150,21 +152,33 @@ namespace NzbDrone.Core.Download
// Double check if all movies were imported by checking the history if at least one
// file was imported. This will allow the decision engine to reject already imported
// episode files and still mark the download complete when all files are imported.
if (importResults.Any(c => c.Result == ImportResultType.Imported))
{
var historyItems = _historyService.FindByDownloadId(trackedDownload.DownloadItem.DownloadId)
var atLeastOneMovieImported = importResults.Any(c => c.Result == ImportResultType.Imported);
var historyItems = _historyService.FindByDownloadId(trackedDownload.DownloadItem.DownloadId)
.OrderByDescending(h => h.Date)
.ToList();
var allMoviesImportedInHistory = _trackedDownloadAlreadyImported.IsImported(trackedDownload, historyItems);
var allMoviesImportedInHistory = _trackedDownloadAlreadyImported.IsImported(trackedDownload, historyItems);
if (allMoviesImportedInHistory)
if (allMoviesImportedInHistory)
{
if (atLeastOneMovieImported)
{
_logger.Debug("All movies were imported in history for {0}", trackedDownload.DownloadItem.Title);
trackedDownload.State = TrackedDownloadState.Imported;
_eventAggregator.PublishEvent(new DownloadCompletedEvent(trackedDownload, trackedDownload.RemoteMovie.Movie.Id));
return true;
}
_logger.Debug()
.Message("No Movies were just imported, but all movies were previously imported, possible issue with download history.")
.Property("MovieId", trackedDownload.RemoteMovie.Movie.Id)
.Property("DownloadId", trackedDownload.DownloadItem.DownloadId)
.Property("Title", trackedDownload.DownloadItem.Title)
.Property("Path", trackedDownload.DownloadItem.OutputPath.ToString())
.WriteSentryWarn("DownloadHistoryIncomplete")
.Write();
}
_logger.Debug("Not all movies have been imported for {0}", trackedDownload.DownloadItem.Title);

@ -137,7 +137,7 @@ namespace NzbDrone.Core.Download.History
var history = new DownloadHistory
{
EventType = DownloadHistoryEventType.FileImported,
MovieId = message.MovieInfo.Movie.Id,
MovieId = message.ImportedMovie.MovieId,
DownloadId = downloadId,
SourceTitle = message.MovieInfo.Path,
Date = DateTime.UtcNow,
@ -153,19 +153,21 @@ namespace NzbDrone.Core.Download.History
public void Handle(DownloadCompletedEvent message)
{
var downloadItem = message.TrackedDownload.DownloadItem;
var history = new DownloadHistory
{
EventType = DownloadHistoryEventType.DownloadImported,
MovieId = message.MovieId,
DownloadId = message.TrackedDownload.DownloadItem.DownloadId,
SourceTitle = message.TrackedDownload.DownloadItem.OutputPath.ToString(),
DownloadId = downloadItem.DownloadId,
SourceTitle = downloadItem.OutputPath.ToString(),
Date = DateTime.UtcNow,
Protocol = message.TrackedDownload.Protocol,
DownloadClientId = message.TrackedDownload.DownloadClient
};
history.Data.Add("DownloadClient", message.TrackedDownload.DownloadItem.DownloadClientInfo.Type);
history.Data.Add("DownloadClientName", message.TrackedDownload.DownloadItem.DownloadClientInfo.Name);
history.Data.Add("DownloadClient", downloadItem.DownloadClientInfo.Type);
history.Data.Add("DownloadClientName", downloadItem.DownloadClientInfo.Name);
_repository.Insert(history);
}

Loading…
Cancel
Save