|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gendoria\CommandQueueBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Gendoria\CommandQueueBundle\Worker\WorkerRunnerManager; |
|
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Description of RunWorkerCommand |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tomasz Struczyński <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class RunWorkerCommand extends ContainerAwareCommand |
|
17
|
|
|
{ |
|
18
|
2 |
|
protected function configure() |
|
19
|
|
|
{ |
|
20
|
2 |
|
$this->setName('command-queue:worker') |
|
21
|
2 |
|
->setDescription('Runs a worker process. Specific worker has to be registered by driver or application.') |
|
22
|
2 |
|
->addArgument('name', InputArgument::REQUIRED, 'Worker name.'); |
|
23
|
2 |
|
} |
|
24
|
|
|
|
|
25
|
2 |
|
public function execute(InputInterface $input, OutputInterface $output) |
|
26
|
|
|
{ |
|
27
|
2 |
|
$name = $input->getArgument('name'); |
|
28
|
|
|
/* @var $workerRunnerService WorkerRunnerManager */ |
|
29
|
2 |
|
$workerRunnerService = $this->getContainer()->get('gendoria_command_queue.runner_manager'); |
|
30
|
2 |
|
if (!$workerRunnerService->has($name)) { |
|
31
|
1 |
|
$runners = $workerRunnerService->getRunners(); |
|
32
|
1 |
|
$runnersFormatted = array_map(array($this, 'formatRunnerName'), $runners); |
|
33
|
1 |
|
$output->writeln(sprintf('<error>Worker "%s" not registered.</error>', $name)); |
|
34
|
1 |
|
$output->writeln('Registered workers:'); |
|
35
|
1 |
|
$output->writeln($runnersFormatted); |
|
36
|
1 |
|
return 1; |
|
37
|
|
|
} |
|
38
|
1 |
|
$workerRunnerService->run($name, $output); |
|
39
|
1 |
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
public function formatRunnerName($name) |
|
42
|
|
|
{ |
|
43
|
1 |
|
return sprintf(" * <info>%s</info>", $name); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|