diff --git a/src/NzbDrone.Core.Test/Download/CompletedDownloadServiceTests/ImportFixture.cs b/src/NzbDrone.Core.Test/Download/CompletedDownloadServiceTests/ImportFixture.cs index 9e29b9acb..30b785b2d 100644 --- a/src/NzbDrone.Core.Test/Download/CompletedDownloadServiceTests/ImportFixture.cs +++ b/src/NzbDrone.Core.Test/Download/CompletedDownloadServiceTests/ImportFixture.cs @@ -56,6 +56,10 @@ public void Setup() Mocker.GetMock() .Setup(s => s.GetMovie("Drone.1998")) .Returns(remoteMovie.Movie); + + Mocker.GetMock() + .Setup(s => s.FindByDownloadId(It.IsAny())) + .Returns(new List()); } private RemoteMovie BuildRemoteMovie() diff --git a/src/NzbDrone.Core/Download/CompletedDownloadService.cs b/src/NzbDrone.Core/Download/CompletedDownloadService.cs index 98f9f5ff3..de897a20a 100644 --- a/src/NzbDrone.Core/Download/CompletedDownloadService.cs +++ b/src/NzbDrone.Core/Download/CompletedDownloadService.cs @@ -2,8 +2,10 @@ 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 @@ public bool VerifyImport(TrackedDownload trackedDownload, List imp // 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); diff --git a/src/NzbDrone.Core/Download/History/DownloadHistoryService.cs b/src/NzbDrone.Core/Download/History/DownloadHistoryService.cs index 13763cbff..8ea02d690 100644 --- a/src/NzbDrone.Core/Download/History/DownloadHistoryService.cs +++ b/src/NzbDrone.Core/Download/History/DownloadHistoryService.cs @@ -137,7 +137,7 @@ public void Handle(MovieImportedEvent message) 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 @@ public void Handle(MovieImportedEvent message) 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); }