diff --git a/src/NzbDrone.Core/Datastore/Migration/186_add_result_to_commands.cs b/src/NzbDrone.Core/Datastore/Migration/186_add_result_to_commands.cs new file mode 100644 index 000000000..cafdaacf2 --- /dev/null +++ b/src/NzbDrone.Core/Datastore/Migration/186_add_result_to_commands.cs @@ -0,0 +1,14 @@ +using FluentMigrator; +using NzbDrone.Core.Datastore.Migration.Framework; + +namespace NzbDrone.Core.Datastore.Migration +{ + [Migration(186)] + public class add_result_to_commands : NzbDroneMigrationBase + { + protected override void MainDbUpgrade() + { + Alter.Table("Commands").AddColumn("Result").AsInt32().WithDefaultValue(1); + } + } +} diff --git a/src/NzbDrone.Core/MediaFiles/DownloadedEpisodesCommandService.cs b/src/NzbDrone.Core/MediaFiles/DownloadedEpisodesCommandService.cs index 8c75b196e..ee7ac8110 100644 --- a/src/NzbDrone.Core/MediaFiles/DownloadedEpisodesCommandService.cs +++ b/src/NzbDrone.Core/MediaFiles/DownloadedEpisodesCommandService.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using NLog; @@ -19,18 +19,21 @@ namespace NzbDrone.Core.MediaFiles private readonly ITrackedDownloadService _trackedDownloadService; private readonly IDiskProvider _diskProvider; private readonly ICompletedDownloadService _completedDownloadService; + private readonly ICommandResultReporter _commandResultReporter; private readonly Logger _logger; public DownloadedEpisodesCommandService(IDownloadedEpisodesImportService downloadedEpisodesImportService, ITrackedDownloadService trackedDownloadService, IDiskProvider diskProvider, ICompletedDownloadService completedDownloadService, + ICommandResultReporter commandResultReporter, Logger logger) { _downloadedEpisodesImportService = downloadedEpisodesImportService; _trackedDownloadService = trackedDownloadService; _diskProvider = diskProvider; _completedDownloadService = completedDownloadService; + _commandResultReporter = commandResultReporter; _logger = logger; } @@ -78,8 +81,10 @@ namespace NzbDrone.Core.MediaFiles 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. + // Allow the command to complete successfully, but report as unsuccessful + _logger.ProgressDebug("Failed to import"); + _commandResultReporter.Report(CommandResult.Unsuccessful); } } } diff --git a/src/NzbDrone.Core/Messaging/Commands/CommandModel.cs b/src/NzbDrone.Core/Messaging/Commands/CommandModel.cs index 276a9b3ca..d7ec0dc37 100644 --- a/src/NzbDrone.Core/Messaging/Commands/CommandModel.cs +++ b/src/NzbDrone.Core/Messaging/Commands/CommandModel.cs @@ -1,4 +1,4 @@ -using System; +using System; using NzbDrone.Common.Messaging; using NzbDrone.Core.Datastore; @@ -10,6 +10,7 @@ namespace NzbDrone.Core.Messaging.Commands public Command Body { get; set; } public CommandPriority Priority { get; set; } public CommandStatus Status { get; set; } + public CommandResult Result { get; set; } public DateTime QueuedAt { get; set; } public DateTime? StartedAt { get; set; } public DateTime? EndedAt { get; set; } diff --git a/src/NzbDrone.Core/Messaging/Commands/CommandQueueManager.cs b/src/NzbDrone.Core/Messaging/Commands/CommandQueueManager.cs index 44edc9009..c2accb8c4 100644 --- a/src/NzbDrone.Core/Messaging/Commands/CommandQueueManager.cs +++ b/src/NzbDrone.Core/Messaging/Commands/CommandQueueManager.cs @@ -26,6 +26,7 @@ namespace NzbDrone.Core.Messaging.Commands CommandModel Get(int id); List GetStarted(); void SetMessage(CommandModel command, string message); + void SetResult(CommandModel command, CommandResult result); void Start(CommandModel command); void Complete(CommandModel command, string message); void Fail(CommandModel command, string message, Exception e); @@ -180,6 +181,11 @@ namespace NzbDrone.Core.Messaging.Commands command.Message = message; } + public void SetResult(CommandModel command, CommandResult result) + { + command.Result = result; + } + public void Start(CommandModel command) { // Marks the command as started in the DB, the queue takes care of marking it as started on it's own @@ -189,6 +195,12 @@ namespace NzbDrone.Core.Messaging.Commands public void Complete(CommandModel command, string message) { + // If the result hasn't been set yet then set it to successful + if (command.Result == CommandResult.Unknown) + { + command.Result = CommandResult.Successful; + } + Update(command, CommandStatus.Completed, message); _commandQueue.PulseAllConsumers(); diff --git a/src/NzbDrone.Core/Messaging/Commands/CommandResult.cs b/src/NzbDrone.Core/Messaging/Commands/CommandResult.cs new file mode 100644 index 000000000..9e865df92 --- /dev/null +++ b/src/NzbDrone.Core/Messaging/Commands/CommandResult.cs @@ -0,0 +1,9 @@ +namespace NzbDrone.Core.Messaging.Commands +{ + public enum CommandResult + { + Unknown = 0, + Successful = 1, + Unsuccessful = 2 + } +} diff --git a/src/NzbDrone.Core/Messaging/Commands/CommandResultReporter.cs b/src/NzbDrone.Core/Messaging/Commands/CommandResultReporter.cs new file mode 100644 index 000000000..c74d2c06b --- /dev/null +++ b/src/NzbDrone.Core/Messaging/Commands/CommandResultReporter.cs @@ -0,0 +1,43 @@ +using NzbDrone.Core.ProgressMessaging; + +namespace NzbDrone.Core.Messaging.Commands +{ + public interface ICommandResultReporter + { + void Report(CommandResult result); + } + + public class CommandResultReporter : ICommandResultReporter + { + private readonly IManageCommandQueue _commandQueueManager; + + public CommandResultReporter(IManageCommandQueue commandQueueManager) + { + _commandQueueManager = commandQueueManager; + } + + public void Report(CommandResult result) + { + var command = ProgressMessageContext.CommandModel; + + if (command == null) + { + return; + } + + if (!ProgressMessageContext.LockReentrancy()) + { + return; + } + + try + { + _commandQueueManager.SetResult(command, result); + } + finally + { + ProgressMessageContext.UnlockReentrancy(); + } + } + } +} diff --git a/src/Sonarr.Api.V3/Commands/CommandResource.cs b/src/Sonarr.Api.V3/Commands/CommandResource.cs index 7b2461ebf..c801968e7 100644 --- a/src/Sonarr.Api.V3/Commands/CommandResource.cs +++ b/src/Sonarr.Api.V3/Commands/CommandResource.cs @@ -17,6 +17,7 @@ namespace Sonarr.Api.V3.Commands public Command Body { get; set; } public CommandPriority Priority { get; set; } public CommandStatus Status { get; set; } + public CommandResult Result { get; set; } public DateTime Queued { get; set; } public DateTime? Started { get; set; } public DateTime? Ended { get; set; } @@ -102,6 +103,7 @@ namespace Sonarr.Api.V3.Commands Body = model.Body, Priority = model.Priority, Status = model.Status, + Result = model.Result, Queued = model.QueuedAt, Started = model.StartedAt, Ended = model.EndedAt,