Fixed: Exception thrown when marking download as complete

This commit is contained in:
Mark McDowall 2020-06-13 13:11:30 -07:00
parent 11cdf13148
commit 0e6238bf6f
2 changed files with 29 additions and 13 deletions

View File

@ -3,8 +3,10 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using NLog; using NLog;
using NLog.Fluent;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Common.Instrumentation.Extensions;
using NzbDrone.Core.Download.TrackedDownloads; using NzbDrone.Core.Download.TrackedDownloads;
using NzbDrone.Core.History; using NzbDrone.Core.History;
using NzbDrone.Core.MediaFiles; 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 // Since imports should be relatively fast and these types of data changes are infrequent this should be quite
// safe, but commenting for future benefit. // 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) if (atLeastOneEpisodeImported)
.OrderByDescending(h => h.Date)
.ToList();
var allEpisodesImportedInHistory = _trackedDownloadAlreadyImported.IsImported(trackedDownload, historyItems);
if (allEpisodesImportedInHistory)
{ {
_logger.Debug("All episodes were imported in history for {0}", trackedDownload.DownloadItem.Title); _logger.Debug("All episodes were imported in history for {0}", trackedDownload.DownloadItem.Title);
trackedDownload.State = TrackedDownloadState.Imported; trackedDownload.State = TrackedDownloadState.Imported;
_eventAggregator.PublishEvent(new DownloadCompletedEvent(trackedDownload, trackedDownload.RemoteEpisode.Series.Id)); _eventAggregator.PublishEvent(new DownloadCompletedEvent(trackedDownload, trackedDownload.RemoteEpisode.Series.Id));
return true; 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); _logger.Debug("Not all episodes have been imported for {0}", trackedDownload.DownloadItem.Title);

View File

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