New: Add result to commands to report commands that did not complete successfully

Closes #4759
This commit is contained in:
Mark McDowall 2023-04-03 22:11:43 -07:00
parent 07f0fbf9a5
commit 103ce3def4
7 changed files with 89 additions and 3 deletions

View File

@ -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);
}
}
}

View File

@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using NLog; using NLog;
@ -19,18 +19,21 @@ namespace NzbDrone.Core.MediaFiles
private readonly ITrackedDownloadService _trackedDownloadService; private readonly ITrackedDownloadService _trackedDownloadService;
private readonly IDiskProvider _diskProvider; private readonly IDiskProvider _diskProvider;
private readonly ICompletedDownloadService _completedDownloadService; private readonly ICompletedDownloadService _completedDownloadService;
private readonly ICommandResultReporter _commandResultReporter;
private readonly Logger _logger; private readonly Logger _logger;
public DownloadedEpisodesCommandService(IDownloadedEpisodesImportService downloadedEpisodesImportService, public DownloadedEpisodesCommandService(IDownloadedEpisodesImportService downloadedEpisodesImportService,
ITrackedDownloadService trackedDownloadService, ITrackedDownloadService trackedDownloadService,
IDiskProvider diskProvider, IDiskProvider diskProvider,
ICompletedDownloadService completedDownloadService, ICompletedDownloadService completedDownloadService,
ICommandResultReporter commandResultReporter,
Logger logger) Logger logger)
{ {
_downloadedEpisodesImportService = downloadedEpisodesImportService; _downloadedEpisodesImportService = downloadedEpisodesImportService;
_trackedDownloadService = trackedDownloadService; _trackedDownloadService = trackedDownloadService;
_diskProvider = diskProvider; _diskProvider = diskProvider;
_completedDownloadService = completedDownloadService; _completedDownloadService = completedDownloadService;
_commandResultReporter = commandResultReporter;
_logger = logger; _logger = logger;
} }
@ -78,8 +81,10 @@ namespace NzbDrone.Core.MediaFiles
if (importResults == null || importResults.All(v => v.Result != ImportResultType.Imported)) 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"); _logger.ProgressDebug("Failed to import");
_commandResultReporter.Report(CommandResult.Unsuccessful);
} }
} }
} }

View File

@ -1,4 +1,4 @@
using System; using System;
using NzbDrone.Common.Messaging; using NzbDrone.Common.Messaging;
using NzbDrone.Core.Datastore; using NzbDrone.Core.Datastore;
@ -10,6 +10,7 @@ namespace NzbDrone.Core.Messaging.Commands
public Command Body { get; set; } public Command Body { get; set; }
public CommandPriority Priority { get; set; } public CommandPriority Priority { get; set; }
public CommandStatus Status { get; set; } public CommandStatus Status { get; set; }
public CommandResult Result { get; set; }
public DateTime QueuedAt { get; set; } public DateTime QueuedAt { get; set; }
public DateTime? StartedAt { get; set; } public DateTime? StartedAt { get; set; }
public DateTime? EndedAt { get; set; } public DateTime? EndedAt { get; set; }

View File

@ -26,6 +26,7 @@ namespace NzbDrone.Core.Messaging.Commands
CommandModel Get(int id); CommandModel Get(int id);
List<CommandModel> GetStarted(); List<CommandModel> GetStarted();
void SetMessage(CommandModel command, string message); void SetMessage(CommandModel command, string message);
void SetResult(CommandModel command, CommandResult result);
void Start(CommandModel command); void Start(CommandModel command);
void Complete(CommandModel command, string message); void Complete(CommandModel command, string message);
void Fail(CommandModel command, string message, Exception e); void Fail(CommandModel command, string message, Exception e);
@ -180,6 +181,11 @@ namespace NzbDrone.Core.Messaging.Commands
command.Message = message; command.Message = message;
} }
public void SetResult(CommandModel command, CommandResult result)
{
command.Result = result;
}
public void Start(CommandModel command) 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 // 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) 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); Update(command, CommandStatus.Completed, message);
_commandQueue.PulseAllConsumers(); _commandQueue.PulseAllConsumers();

View File

@ -0,0 +1,9 @@
namespace NzbDrone.Core.Messaging.Commands
{
public enum CommandResult
{
Unknown = 0,
Successful = 1,
Unsuccessful = 2
}
}

View File

@ -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();
}
}
}
}

View File

@ -17,6 +17,7 @@ namespace Sonarr.Api.V3.Commands
public Command Body { get; set; } public Command Body { get; set; }
public CommandPriority Priority { get; set; } public CommandPriority Priority { get; set; }
public CommandStatus Status { get; set; } public CommandStatus Status { get; set; }
public CommandResult Result { get; set; }
public DateTime Queued { get; set; } public DateTime Queued { get; set; }
public DateTime? Started { get; set; } public DateTime? Started { get; set; }
public DateTime? Ended { get; set; } public DateTime? Ended { get; set; }
@ -102,6 +103,7 @@ namespace Sonarr.Api.V3.Commands
Body = model.Body, Body = model.Body,
Priority = model.Priority, Priority = model.Priority,
Status = model.Status, Status = model.Status,
Result = model.Result,
Queued = model.QueuedAt, Queued = model.QueuedAt,
Started = model.StartedAt, Started = model.StartedAt,
Ended = model.EndedAt, Ended = model.EndedAt,