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

(cherry picked from commit 103ce3def4636ef891e72bd687ef8f46b5125233)
pull/3600/head
Mark McDowall 1 year ago committed by Bogdan
parent 8f3aba7b79
commit 45329f29bd

@ -17,6 +17,7 @@ namespace Lidarr.Api.V1.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 Lidarr.Api.V1.Commands
Body = model.Body,
Priority = model.Priority,
Status = model.Status,
Result = model.Result,
Queued = model.QueuedAt,
Started = model.StartedAt,
Ended = model.EndedAt,

@ -0,0 +1,14 @@
using FluentMigrator;
using NzbDrone.Core.Datastore.Migration.Framework;
namespace NzbDrone.Core.Datastore.Migration
{
[Migration(064)]
public class add_result_to_commands : NzbDroneMigrationBase
{
protected override void MainDbUpgrade()
{
Alter.Table("Commands").AddColumn("Result").AsInt32().WithDefaultValue(1);
}
}
}

@ -4,6 +4,7 @@ using System.Linq;
using NLog;
using NzbDrone.Common.Disk;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Instrumentation.Extensions;
using NzbDrone.Core.Download;
using NzbDrone.Core.Download.TrackedDownloads;
using NzbDrone.Core.MediaFiles.Commands;
@ -18,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 DownloadedAlbumsCommandService(IDownloadedTracksImportService downloadedTracksImportService,
ITrackedDownloadService trackedDownloadService,
IDiskProvider diskProvider,
ICompletedDownloadService completedDownloadService,
ICommandResultReporter commandResultReporter,
Logger logger)
{
_downloadedTracksImportService = downloadedTracksImportService;
_trackedDownloadService = trackedDownloadService;
_diskProvider = diskProvider;
_completedDownloadService = completedDownloadService;
_commandResultReporter = commandResultReporter;
_logger = logger;
}
@ -77,9 +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.
// Changing the message won't do a thing either, coz it will get set to 'Completed' a msec later.
// message.SetMessage("Failed to import");
// Allow the command to complete successfully, but report as unsuccessful
_logger.ProgressDebug("Failed to import");
_commandResultReporter.Report(CommandResult.Unsuccessful);
}
}
}

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

@ -26,6 +26,7 @@ namespace NzbDrone.Core.Messaging.Commands
CommandModel Get(int id);
List<CommandModel> 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();

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

@ -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();
}
}
}
}
Loading…
Cancel
Save