From e39fee920999db0a3a188ce0ea61862ff825824d 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 | 9 +++++++ .../Datastore/Converters/CommandConverter.cs | 6 ++++- .../Messaging/Commands/CommandExecutor.cs | 3 +-- .../Messaging/Commands/UnknownCommand.cs | 11 +++++++++ .../Commands/UnknownCommandExecutor.cs | 24 +++++++++++++++++++ 5 files changed, 50 insertions(+), 3 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 633b92ce3..b66cd0896 100644 --- a/src/NzbDrone.Core.Test/Datastore/Converters/CommandConverterFixture.cs +++ b/src/NzbDrone.Core.Test/Datastore/Converters/CommandConverterFixture.cs @@ -3,6 +3,7 @@ using FluentAssertions; using NUnit.Framework; using NzbDrone.Core.Books.Commands; using NzbDrone.Core.Datastore.Converters; +using NzbDrone.Core.Messaging.Commands; using NzbDrone.Core.Test.Framework; namespace NzbDrone.Core.Test.Datastore.Converters @@ -42,6 +43,14 @@ namespace NzbDrone.Core.Test.Datastore.Converters Subject.Parse(data).Should().BeOfType(); } + [Test] + public void should_return_unknown_command_when_getting_json_from_db() + { + var data = "{\"name\": \"EnsureMediaCovers\"}"; + + Subject.Parse(data).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 6754b237c..e2f77c6f0 100644 --- a/src/NzbDrone.Core/Datastore/Converters/CommandConverter.cs +++ b/src/NzbDrone.Core/Datastore/Converters/CommandConverter.cs @@ -27,7 +27,11 @@ namespace NzbDrone.Core.Datastore.Converters if (impType == null) { - throw new CommandNotFoundException(contract); + var result = JsonSerializer.Deserialize(stringValue, SerializerSettings); + + result.ContractName = contract; + + return result; } return (Command)JsonSerializer.Deserialize(stringValue, impType, SerializerSettings); diff --git a/src/NzbDrone.Core/Messaging/Commands/CommandExecutor.cs b/src/NzbDrone.Core/Messaging/Commands/CommandExecutor.cs index 2c9ea763e..18dabae07 100644 --- a/src/NzbDrone.Core/Messaging/Commands/CommandExecutor.cs +++ b/src/NzbDrone.Core/Messaging/Commands/CommandExecutor.cs @@ -69,8 +69,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); + } + } +}