From 1487f54749af061793f1376ba994b395f7c85f65 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Sun, 3 May 2020 16:59:37 +0200 Subject: [PATCH] Skip unknown/removed commands still queued in the database --- .../Converters/CommandConverterFixture.cs | 17 +++++++++++++ .../Datastore/Converters/CommandConverter.cs | 8 +++++-- .../Messaging/Commands/CommandExecutor.cs | 3 +-- .../Messaging/Commands/UnknownCommand.cs | 11 +++++++++ .../Commands/UnknownCommandExecutor.cs | 24 +++++++++++++++++++ 5 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 src/NzbDrone.Core/Messaging/Commands/UnknownCommand.cs create mode 100644 src/NzbDrone.Core/Messaging/Commands/UnknownCommandExecutor.cs diff --git a/src/NzbDrone.Core.Test/Datastore/Converters/CommandConverterFixture.cs b/src/NzbDrone.Core.Test/Datastore/Converters/CommandConverterFixture.cs index ac0a3359e..6331a2f5c 100644 --- a/src/NzbDrone.Core.Test/Datastore/Converters/CommandConverterFixture.cs +++ b/src/NzbDrone.Core.Test/Datastore/Converters/CommandConverterFixture.cs @@ -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(); } + [Test] + public void should_return_unknown_command_when_getting_json_from_db() + { + var dataRecordMock = new Mock(); + 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(); + } + [Test] public void should_return_null_for_null_value_when_getting_from_db() { diff --git a/src/NzbDrone.Core/Datastore/Converters/CommandConverter.cs b/src/NzbDrone.Core/Datastore/Converters/CommandConverter.cs index 0bb97f23a..8d592b775 100644 --- a/src/NzbDrone.Core/Datastore/Converters/CommandConverter.cs +++ b/src/NzbDrone.Core/Datastore/Converters/CommandConverter.cs @@ -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(stringValue); + + result.ContractName = contract; + + return result; } return Json.Deserialize(stringValue, impType); diff --git a/src/NzbDrone.Core/Messaging/Commands/CommandExecutor.cs b/src/NzbDrone.Core/Messaging/Commands/CommandExecutor.cs index 0eb43a857..2089ce0ed 100644 --- a/src/NzbDrone.Core/Messaging/Commands/CommandExecutor.cs +++ b/src/NzbDrone.Core/Messaging/Commands/CommandExecutor.cs @@ -67,8 +67,7 @@ namespace NzbDrone.Core.Messaging.Commands try { - var handlerContract = typeof(IExecute<>).MakeGenericType(command.GetType()); - handler = (IExecute)_serviceFactory.Build(handlerContract); + handler = (IExecute)_serviceFactory.Build(typeof(IExecute)); _logger.Trace("{0} -> {1}", command.GetType().Name, handler.GetType().Name); diff --git a/src/NzbDrone.Core/Messaging/Commands/UnknownCommand.cs b/src/NzbDrone.Core/Messaging/Commands/UnknownCommand.cs new file mode 100644 index 000000000..fa579916e --- /dev/null +++ b/src/NzbDrone.Core/Messaging/Commands/UnknownCommand.cs @@ -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; } + } +} diff --git a/src/NzbDrone.Core/Messaging/Commands/UnknownCommandExecutor.cs b/src/NzbDrone.Core/Messaging/Commands/UnknownCommandExecutor.cs new file mode 100644 index 000000000..08fb2725d --- /dev/null +++ b/src/NzbDrone.Core/Messaging/Commands/UnknownCommandExecutor.cs @@ -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 + { + private readonly Logger _logger; + + public UnknownCommandExecutor(Logger logger) + { + _logger = logger; + } + + public void Execute(UnknownCommand message) + { + _logger.Debug("Ignoring unknown command {0}", message.ContractName); + } + } +}