From 755a42ea45fa05131083f350fc17c99ff41cffdd Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Wed, 25 Mar 2015 16:46:41 -0700 Subject: [PATCH] Use cache to check for running or started commands --- .../Messaging/Commands/CommandExecutor.cs | 6 +++++- .../Messaging/Commands/CommandQueueManager.cs | 13 ++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/NzbDrone.Core/Messaging/Commands/CommandExecutor.cs b/src/NzbDrone.Core/Messaging/Commands/CommandExecutor.cs index 8679cba82..42051f763 100644 --- a/src/NzbDrone.Core/Messaging/Commands/CommandExecutor.cs +++ b/src/NzbDrone.Core/Messaging/Commands/CommandExecutor.cs @@ -38,7 +38,7 @@ namespace NzbDrone.Core.Messaging.Commands { try { - ExecuteCommand((dynamic)command.Body, command); + ExecuteCommand((dynamic) command.Body, command); } catch (Exception ex) { @@ -51,6 +51,10 @@ namespace NzbDrone.Core.Messaging.Commands _logger.ErrorException(ex.Message, ex); Thread.ResetAbort(); } + catch (Exception ex) + { + _logger.Error(ex.Message, ex); + } } private void ExecuteCommand(TCommand command, CommandModel commandModel) where TCommand : Command diff --git a/src/NzbDrone.Core/Messaging/Commands/CommandQueueManager.cs b/src/NzbDrone.Core/Messaging/Commands/CommandQueueManager.cs index e91d383a1..bb91685f6 100644 --- a/src/NzbDrone.Core/Messaging/Commands/CommandQueueManager.cs +++ b/src/NzbDrone.Core/Messaging/Commands/CommandQueueManager.cs @@ -57,7 +57,7 @@ namespace NzbDrone.Core.Messaging.Commands _logger.Trace("Publishing {0}", command.Name); _logger.Trace("Checking if command is queued or started: {0}", command.Name); - var existingCommands = _repo.FindQueuedOrStarted(command.Name); + var existingCommands = QueuedOrStarted(command.Name); var existing = existingCommands.SingleOrDefault(c => CommandEqualityComparer.Instance.Equals(c.Body, command)); if (existing != null) @@ -123,8 +123,8 @@ namespace NzbDrone.Core.Messaging.Commands command.Status = CommandStatus.Started; _logger.Trace("Marking command as started: {0}", command.Name); + _commandCache.Set(command.Id.ToString(), command); _repo.Update(command); - _commandCache.Set(command.Id.ToString(), command); } public void Complete(CommandModel command, string message) @@ -191,8 +191,15 @@ namespace NzbDrone.Core.Messaging.Commands command.Status = status; _logger.Trace("Updating command status"); - _repo.Update(command); _commandCache.Set(command.Id.ToString(), command); + _repo.Update(command); + } + + private List QueuedOrStarted(string name) + { + return _commandCache.Values.Where(q => q.Name == name && + (q.Status == CommandStatus.Queued || + q.Status == CommandStatus.Started)).ToList(); } public void Handle(ApplicationStartedEvent message)