Lock CommandQueueManager.PushMany too

This commit is contained in:
Taloth Saldono 2020-05-03 18:48:20 +02:00
parent 1487f54749
commit 66ee28d0a9
1 changed files with 28 additions and 25 deletions

View File

@ -54,39 +54,42 @@ namespace NzbDrone.Core.Messaging.Commands
{ {
_logger.Trace("Publishing {0} commands", commands.Count); _logger.Trace("Publishing {0} commands", commands.Count);
var commandModels = new List<CommandModel>(); lock (_commandQueue)
var existingCommands = _commandQueue.QueuedOrStarted();
foreach (var command in commands)
{ {
var existing = existingCommands.SingleOrDefault(c => c.Name == command.Name && CommandEqualityComparer.Instance.Equals(c.Body, command)); var commandModels = new List<CommandModel>();
var existingCommands = _commandQueue.QueuedOrStarted();
if (existing != null) foreach (var command in commands)
{ {
continue; var existing = existingCommands.FirstOrDefault(c => c.Name == command.Name && CommandEqualityComparer.Instance.Equals(c.Body, command));
if (existing != null)
{
continue;
}
var commandModel = new CommandModel
{
Name = command.Name,
Body = command,
QueuedAt = DateTime.UtcNow,
Trigger = CommandTrigger.Unspecified,
Priority = CommandPriority.Normal,
Status = CommandStatus.Queued
};
commandModels.Add(commandModel);
} }
var commandModel = new CommandModel _repo.InsertMany(commandModels);
foreach (var commandModel in commandModels)
{ {
Name = command.Name, _commandQueue.Add(commandModel);
Body = command, }
QueuedAt = DateTime.UtcNow,
Trigger = CommandTrigger.Unspecified,
Priority = CommandPriority.Normal,
Status = CommandStatus.Queued
};
commandModels.Add(commandModel); return commandModels;
} }
_repo.InsertMany(commandModels);
foreach (var commandModel in commandModels)
{
_commandQueue.Add(commandModel);
}
return commandModels;
} }
public CommandModel Push<TCommand>(TCommand command, CommandPriority priority = CommandPriority.Normal, CommandTrigger trigger = CommandTrigger.Unspecified) where TCommand : Command public CommandModel Push<TCommand>(TCommand command, CommandPriority priority = CommandPriority.Normal, CommandTrigger trigger = CommandTrigger.Unspecified) where TCommand : Command