phpExecutableFinder = $phpExecutableFinder; $this->consumerConfig = $consumerConfig; $this->deploymentConfig = $deploymentConfig; $this->shellBackground = $shellBackground; $this->lockManager = $lockManager; $this->mqConnectionTypeResolver = $mqConnectionTypeResolver ?: ObjectManager::getInstance()->get(ConnectionTypeResolver::class); $this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class); $this->checkIsAvailableMessages = $checkIsAvailableMessages ?: ObjectManager::getInstance()->get(CheckIsAvailableMessagesInQueue::class); } /** * Runs consumers processes * * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function run(): void { $runByCron = $this->deploymentConfig->get('cron_consumers_runner/cron_run', true); $multipleProcesses = $this->deploymentConfig->get('cron_consumers_runner/multiple_processes', []); if (!$runByCron) { return; } $maxMessages = (int) $this->deploymentConfig->get('cron_consumers_runner/max_messages', 10000); $allowedConsumers = $this->deploymentConfig->get('cron_consumers_runner/consumers', []); $php = $this->phpExecutableFinder->find() ?: 'php'; foreach ($this->consumerConfig->getConsumers() as $consumer) { if (!$this->canBeRun($consumer, $allowedConsumers)) { continue; } if (array_key_exists($consumer->getName(), $multipleProcesses)) { $numberOfProcesses = $multipleProcesses[$consumer->getName()]; for ($i = 1; $i <= $numberOfProcesses; $i++) { if ($this->lockManager->isLocked(md5($consumer->getName() . '-' . $i))) { //phpcs:ignore continue; } $arguments = [ $consumer->getName(), '--multi-process=' . $i ]; if ($maxMessages) { $arguments = $this->addMaxMessagesArgument($arguments, $consumer, $maxMessages); } $command = $php . ' ' . BP . '/bin/magento queue:consumers:start %s %s' . ($maxMessages ? ' %s' : ''); $this->shellBackground->execute($command, $arguments); } } else if (!$this->lockManager->isLocked(md5($consumer->getName()))) { //phpcs:ignore $arguments = [ $consumer->getName(), '--single-thread' ]; if ($maxMessages) { $arguments = $this->addMaxMessagesArgument($arguments, $consumer, $maxMessages); } $command = $php . ' ' . BP . '/bin/magento queue:consumers:start %s %s' . ($maxMessages ? ' %s' : ''); $this->shellBackground->execute($command, $arguments); } } } /** * Add max-messages argument and log warning if exceeds default * * @param array $arguments Arguments array to append to * @param ConsumerConfigItemInterface $consumer * @param int $defaultMaxMessages * @return array */ private function addMaxMessagesArgument( array $arguments, ConsumerConfigItemInterface $consumer, int $defaultMaxMessages ): array { $consumerMaxMessages =$consumer->getMaxMessages() ?? $defaultMaxMessages; if ($consumerMaxMessages > $defaultMaxMessages) { $this->logger->warning( __( 'Consumer "%1" has max-messages=%2 which exceeds the configured default (%3). ' . 'This may probably cause high memory usage or long processing times.', $consumer->getName(), $consumerMaxMessages, $defaultMaxMessages ) ); } $arguments[] = '--max-messages=' . $consumerMaxMessages; return $arguments; } /** * Checks that the consumer can be run * * @param ConsumerConfigItemInterface $consumerConfig The consumer config * @param array $allowedConsumers The list of allowed consumers * If $allowedConsumers is empty it means that all consumers are allowed * @return bool Returns true if the consumer can be run * @throws \Magento\Framework\Exception\FileSystemException */ private function canBeRun(ConsumerConfigItemInterface $consumerConfig, array $allowedConsumers = []): bool { $consumerName = $consumerConfig->getName(); if (!empty($allowedConsumers) && !in_array($consumerName, $allowedConsumers)) { return false; } $connectionName = $consumerConfig->getConnection(); try { $this->mqConnectionTypeResolver->getConnectionType($connectionName); } catch (\LogicException $e) { $this->logger->info( sprintf( 'Consumer "%s" skipped as required connection "%s" is not configured. %s', $consumerName, $connectionName, $e->getMessage() ) ); return false; } $globalOnlySpawnWhenMessageAvailable = (bool)$this->deploymentConfig->get( 'queue/only_spawn_when_message_available', true ); if ($consumerConfig->getOnlySpawnWhenMessageAvailable() === true || ($consumerConfig->getOnlySpawnWhenMessageAvailable() === null && $globalOnlySpawnWhenMessageAvailable)) { try { return $this->checkIsAvailableMessages->execute( $connectionName, $consumerConfig->getQueue() ); } catch (\LogicException $e) { $this->logger->info( sprintf( 'Consumer "%s" skipped as its related queue "%s" is not available. %s', $consumerName, $consumerConfig->getQueue(), $e->getMessage() ) ); return false; } } return true; } }