From 0e6238bf6f3fa2cf92392d07a1f6385eaa8a923b Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sat, 13 Jun 2020 13:11:30 -0700 Subject: [PATCH] Fixed: Exception thrown when marking download as complete --- .../Download/CompletedDownloadService.cs | 30 ++++++++++++++----- .../History/DownloadHistoryService.cs | 12 ++++---- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/NzbDrone.Core/Download/CompletedDownloadService.cs b/src/NzbDrone.Core/Download/CompletedDownloadService.cs index cc417ad3d..aa9d67278 100644 --- a/src/NzbDrone.Core/Download/CompletedDownloadService.cs +++ b/src/NzbDrone.Core/Download/CompletedDownloadService.cs @@ -3,8 +3,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; @@ -159,21 +161,33 @@ namespace NzbDrone.Core.Download // Since imports should be relatively fast and these types of data changes are infrequent this should be quite // safe, but commenting for future benefit. - if (importResults.Any(c => c.Result == ImportResultType.Imported)) + var atLeastOneEpisodeImported = importResults.Any(c => c.Result == ImportResultType.Imported); + + var historyItems = _historyService.FindByDownloadId(trackedDownload.DownloadItem.DownloadId) + .OrderByDescending(h => h.Date) + .ToList(); + + var allEpisodesImportedInHistory = _trackedDownloadAlreadyImported.IsImported(trackedDownload, historyItems); + + if (allEpisodesImportedInHistory) { - var historyItems = _historyService.FindByDownloadId(trackedDownload.DownloadItem.DownloadId) - .OrderByDescending(h => h.Date) - .ToList(); - - var allEpisodesImportedInHistory = _trackedDownloadAlreadyImported.IsImported(trackedDownload, historyItems); - - if (allEpisodesImportedInHistory) + if (atLeastOneEpisodeImported) { _logger.Debug("All episodes were imported in history for {0}", trackedDownload.DownloadItem.Title); trackedDownload.State = TrackedDownloadState.Imported; _eventAggregator.PublishEvent(new DownloadCompletedEvent(trackedDownload, trackedDownload.RemoteEpisode.Series.Id)); + return true; } + + _logger.Debug() + .Message("No Episodes were just imported, but all episodes were previously imported, possible issue with download history.") + .Property("SeriesId", trackedDownload.RemoteEpisode.Series.Id) + .Property("DownloadId", trackedDownload.DownloadItem.DownloadId) + .Property("Title", trackedDownload.DownloadItem.Title) + .Property("Path", trackedDownload.DownloadItem.OutputPath.ToString()) + .WriteSentryWarn("DownloadHistoryIncomplete") + .Write(); } _logger.Debug("Not all episodes 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 174cebb96..bf1b5d406 100644 --- a/src/NzbDrone.Core/Download/History/DownloadHistoryService.cs +++ b/src/NzbDrone.Core/Download/History/DownloadHistoryService.cs @@ -148,7 +148,7 @@ namespace NzbDrone.Core.Download.History var history = new DownloadHistory { EventType = DownloadHistoryEventType.FileImported, - SeriesId = message.EpisodeInfo.Series.Id, + SeriesId = message.ImportedEpisode.SeriesId, DownloadId = downloadId, SourceTitle = message.EpisodeInfo.Path, Date = DateTime.UtcNow, @@ -166,19 +166,21 @@ namespace NzbDrone.Core.Download.History public void Handle(DownloadCompletedEvent message) { + var downloadItem = message.TrackedDownload.DownloadItem; + var history = new DownloadHistory { EventType = DownloadHistoryEventType.DownloadImported, SeriesId = message.SeriesId, - DownloadId = message.TrackedDownload.DownloadItem.DownloadId, - SourceTitle = message.TrackedDownload.DownloadItem.OutputPath.ToString(), + DownloadId = downloadItem.DownloadId, + SourceTitle = downloadItem.Title, 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); }