checkParams($params); $report = Report::createInfo('Running command...'); /** @var QueueDispatcherInterface|ConfigurableService $queueService */ $queueService = $this->getServiceLocator()->get(QueueDispatcherInterface::SERVICE_ID); $registerBroker = $this->registerBroker($params, $queueService); // Create queues if (!$queueService->isSync()) { $queueService->initialize(); $report->add(Report::createSuccess('Queue(s) initialized.')); } if ($registerBroker) { $this->registerService(QueueDispatcherInterface::SERVICE_ID, $queueService); $report->add(Report::createSuccess('Queue service re-registered.')); } // Create task log container /** @var TaskLogInterface $taskLog */ $taskLog = $this->getServiceLocator()->get(TaskLogInterface::SERVICE_ID); $taskLog->createContainer(); $report->add(Report::createSuccess('Task Log container created.')); return $report; } catch (\Exception $e) { return Report::createFailure($e->getMessage()); } } /** * @param array $params */ private function checkParams(array $params) { foreach ($params as $param) { list($option, $value) = explode('=', $param); switch ($option) { case '--broker': if (!in_array($value, self::AVAILABLE_BROKERS)) { throw new InvalidArgumentException( sprintf('Broker "%s" is not a valid broker option. Valid options: %s', $value, implode(', ', self::AVAILABLE_BROKERS) ) ); } $this->wantedBroker = $value; break; case '--persistence': $this->persistenceId = $value; break; case '--receive': $this->receive = abs((int) $value); break; case '--queue': $this->queue = (string) $value; break; case '--strategy': if (!class_exists($value)) { throw new InvalidArgumentException('Strategy "' . $value . '" does not exist.'); } $this->strategy = new $value(); break; } } } private function registerBroker(array $params, QueueDispatcherInterface $queueService): bool { $reRegister = false; // if any new change is wanted on queues if (count($params) > 0) { // BROKER settings if ($this->wantedBroker) { $brokerFactory = $this->getBrokerFactory(); $broker = $brokerFactory->create($this->wantedBroker, $this->persistenceId, $this->receive ?: 1); if (!is_null($this->queue)) { $queue = $queueService->getQueue($this->queue); $queue->setBroker(clone $broker); } else { foreach ($queueService->getQueues() as $queue) { $queue->setBroker(clone $broker); } } } // STRATEGY settings if ($this->strategy) { $queueService->setTaskSelector($this->strategy); } $reRegister = true; } return $reRegister; } private function getBrokerFactory(): BrokerFactory { return $this->getServiceLocator()->get(BrokerFactory::class); } }