From 1fc49f2aa1b0a41894e3c6e981fa49cd26191746 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 | 11 +++++++++ .../Datastore/Converters/CommandConverter.cs | 6 ++++- .../Messaging/Commands/CommandExecutor.cs | 3 +-- .../Messaging/Commands/UnknownCommand.cs | 11 +++++++++ .../Commands/UnknownCommandExecutor.cs | 24 +++++++++++++++++++ 5 files changed, 52 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 ef5b5dc1a..db8077c5c 100644 --- a/src/NzbDrone.Core.Test/Datastore/Converters/CommandConverterFixture.cs +++ b/src/NzbDrone.Core.Test/Datastore/Converters/CommandConverterFixture.cs @@ -1,7 +1,10 @@ +using System.Data; using System.Data.SQLite; using FluentAssertions; +using Moq; using NUnit.Framework; using NzbDrone.Core.Datastore.Converters; +using NzbDrone.Core.Messaging.Commands; using NzbDrone.Core.Movies.Commands; using NzbDrone.Core.Test.Framework; @@ -42,6 +45,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 ad6e53545..37271bdab 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 ee1946b1e..fdd7e18af 100644 --- a/src/NzbDrone.Core/Messaging/Commands/CommandExecutor.cs +++ b/src/NzbDrone.Core/Messaging/Commands/CommandExecutor.cs @@ -71,8 +71,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); + } + } +}