Skip unknown/removed commands still queued in the database

This commit is contained in:
Taloth Saldono 2020-05-03 16:59:37 +02:00
parent 013c46d266
commit 1487f54749
5 changed files with 59 additions and 4 deletions

View File

@ -6,6 +6,7 @@ using Moq;
using NUnit.Framework;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Datastore.Converters;
using NzbDrone.Core.Messaging.Commands;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv.Commands;
@ -50,6 +51,22 @@ namespace NzbDrone.Core.Test.Datastore.Converters
Subject.FromDB(context).Should().BeOfType<RefreshSeriesCommand>();
}
[Test]
public void should_return_unknown_command_when_getting_json_from_db()
{
var dataRecordMock = new Mock<IDataRecord>();
dataRecordMock.Setup(s => s.GetOrdinal("Name")).Returns(0);
dataRecordMock.Setup(s => s.GetString(0)).Returns("MockRemovedCommand");
var context = new ConverterContext
{
DataRecord = dataRecordMock.Object,
DbValue = new RefreshSeriesCommand(2).ToJson()
};
Subject.FromDB(context).Should().BeOfType<UnknownCommand>();
}
[Test]
public void should_return_null_for_null_value_when_getting_from_db()
{

View File

@ -25,11 +25,15 @@ namespace NzbDrone.Core.Datastore.Converters
var ordinal = context.DataRecord.GetOrdinal("Name");
var contract = context.DataRecord.GetString(ordinal);
var impType = typeof (Command).Assembly.FindTypeByName(contract + "Command");
var impType = typeof(Command).Assembly.FindTypeByName(contract + "Command");
if (impType == null)
{
throw new CommandNotFoundException(contract);
var result = Json.Deserialize<UnknownCommand>(stringValue);
result.ContractName = contract;
return result;
}
return Json.Deserialize(stringValue, impType);

View File

@ -67,8 +67,7 @@ namespace NzbDrone.Core.Messaging.Commands
try
{
var handlerContract = typeof(IExecute<>).MakeGenericType(command.GetType());
handler = (IExecute<TCommand>)_serviceFactory.Build(handlerContract);
handler = (IExecute<TCommand>)_serviceFactory.Build(typeof(IExecute<TCommand>));
_logger.Trace("{0} -> {1}", command.GetType().Name, handler.GetType().Name);

View File

@ -0,0 +1,11 @@
namespace NzbDrone.Core.Messaging.Commands
{
public class UnknownCommand : Command
{
public override bool SendUpdatesToClient => false;
public override string CompletionMessage => "Skipped";
public string ContractName { get; set; }
}
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NLog;
namespace NzbDrone.Core.Messaging.Commands
{
public class UnknownCommandExecutor : IExecute<UnknownCommand>
{
private readonly Logger _logger;
public UnknownCommandExecutor(Logger logger)
{
_logger = logger;
}
public void Execute(UnknownCommand message)
{
_logger.Debug("Ignoring unknown command {0}", message.ContractName);
}
}
}