*/ class RunWorker implements Action, ServiceLocatorAwareInterface { use ServiceLocatorAwareTrait; public function __invoke($params) { $queue = null; $limit = 0; /** @var QueueDispatcherInterface $queueService */ $queueService = $this->getServiceLocator()->get(QueueDispatcherInterface::SERVICE_ID); if ($queueService->isSync()) { return \common_report_Report::createInfo('No worker is needed because all registered queue is a Sync Queue.'); } foreach ($params as $param) { list($option, $value) = explode('=', $param); switch ($option) { case '--queue': $queue = $queueService->getQueue($value); break; case '--limit': $limit = (int) $value; break; } } /** @var TaskLogInterface $taskLog */ $taskLog = $this->getServiceLocator()->get(TaskLogInterface::SERVICE_ID); // if it is install on windows do not use the signals pcntl (specific for ubuntu). $handleSignals = stripos(PHP_OS, 'win') === 0 ? false : true; try { $worker = new LongRunningWorker($queue ?: $queueService, $taskLog, $handleSignals); $worker->setServiceLocator($this->getServiceLocator()); if ($limit) { $worker->setMaxIterations($limit); } $worker->run(); } catch (\Exception $e) { return \common_report_Report::createFailure($e->getMessage()); } return \common_report_Report::createSuccess('Worker finished at ' . (new DateTime('now'))->format(DateTime::ATOM)); } }