From 880170637e79e63d9eea90a547cd74f3c7c6bd6e Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Sun, 3 May 2020 16:33:52 +0200 Subject: [PATCH] Fixed timing issue allowing multiple instances of the same command to be queued --- .../Messaging/Commands/CommandQueueManager.cs | 43 ++++++++++--------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/NzbDrone.Core/Messaging/Commands/CommandQueueManager.cs b/src/NzbDrone.Core/Messaging/Commands/CommandQueueManager.cs index 9a65e5abf..92a10c940 100644 --- a/src/NzbDrone.Core/Messaging/Commands/CommandQueueManager.cs +++ b/src/NzbDrone.Core/Messaging/Commands/CommandQueueManager.cs @@ -100,32 +100,35 @@ 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 = QueuedOrStarted(command.Name); - var existing = existingCommands.SingleOrDefault(c => CommandEqualityComparer.Instance.Equals(c.Body, command)); - - if (existing != null) + lock (_commandQueue) { - _logger.Trace("Command is already in progress: {0}", command.Name); + var existingCommands = QueuedOrStarted(command.Name); + var existing = existingCommands.FirstOrDefault(c => CommandEqualityComparer.Instance.Equals(c.Body, command)); - return existing; - } + if (existing != null) + { + _logger.Trace("Command is already in progress: {0}", command.Name); - var commandModel = new CommandModel - { - Name = command.Name, - Body = command, - QueuedAt = DateTime.UtcNow, - Trigger = trigger, - Priority = priority, - Status = CommandStatus.Queued - }; + return existing; + } + + var commandModel = new CommandModel + { + Name = command.Name, + Body = command, + QueuedAt = DateTime.UtcNow, + Trigger = trigger, + Priority = priority, + Status = CommandStatus.Queued + }; - _logger.Trace("Inserting new command: {0}", commandModel.Name); + _logger.Trace("Inserting new command: {0}", commandModel.Name); - _repo.Insert(commandModel); - _commandQueue.Add(commandModel); + _repo.Insert(commandModel); + _commandQueue.Add(commandModel); - return commandModel; + return commandModel; + } } public CommandModel Push(string commandName, DateTime? lastExecutionTime, DateTime? lastStartTime, CommandPriority priority = CommandPriority.Normal, CommandTrigger trigger = CommandTrigger.Unspecified)