1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Aist Queue (http://mateuszsitek.com/projects/queue) |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (c) 2017 DIGITAL WOLVES LTD (http://digitalwolves.ltd) All rights reserved. |
7
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Aist\Queue\Console\Command; |
11
|
|
|
|
12
|
|
|
use SlmQueue\Controller\Exception\WorkerProcessException; |
13
|
|
|
use SlmQueue\Exception\ExceptionInterface; |
14
|
|
|
use SlmQueue\Queue\QueuePluginManager; |
15
|
|
|
use Symfony\Component\Console\Command\Command; |
16
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
18
|
|
|
use Symfony\Component\Console\Input\InputOption; |
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
20
|
|
|
|
21
|
|
|
class StartQueueWorkerCommand extends Command |
22
|
|
|
{ |
23
|
|
|
const NAME = 'queue:worker:start'; |
24
|
|
|
|
25
|
|
|
const DESCRIPTION = 'Process queue worker.'; |
26
|
|
|
|
27
|
|
|
const HELP = <<< 'EOT' |
28
|
|
|
Process queue worker. |
29
|
|
|
EOT; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
protected function configure() |
35
|
|
|
{ |
36
|
|
|
$this |
37
|
|
|
->setName(self::NAME) |
38
|
|
|
->setDescription(self::DESCRIPTION) |
39
|
|
|
->setHelp(self::HELP) |
40
|
|
|
->setDefinition([ |
41
|
|
|
new InputArgument( |
42
|
|
|
'name', |
43
|
|
|
InputArgument::REQUIRED, |
44
|
|
|
'The queue name.' |
45
|
|
|
), |
46
|
|
|
]) |
47
|
|
|
; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
54
|
|
|
{ |
55
|
|
|
// $queueHelper = $this->getHelper('qm'); |
|
|
|
|
56
|
|
|
$queuePluginManager = $this->getApplication()->getHelperSet()->get('qm'); |
57
|
|
|
|
58
|
|
|
$worker = $this->getApplication()->getHelperSet()->get('worker'); |
59
|
|
|
|
60
|
|
|
$output->writeln( |
61
|
|
|
'Processing queue <info>' . $input->getArgument('name') . '</info> PID <info>' . getmypid() . '</info>' |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
return $this->executeWorkerCommand($input, $output, $queuePluginManager, $worker); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Execute worker |
69
|
|
|
*/ |
70
|
|
|
protected function executeWorkerCommand( |
71
|
|
|
InputInterface $input, |
72
|
|
|
OutputInterface $output, |
73
|
|
|
QueuePluginManager $queuePluginManager, |
74
|
|
|
$worker |
75
|
|
|
) { |
76
|
|
|
$options = $input->getOptions(); |
77
|
|
|
$name = $input->getArgument('name'); |
78
|
|
|
$queue = $queuePluginManager->get($name); |
79
|
|
|
|
80
|
|
|
try { |
81
|
|
|
$messages = $worker->processQueue($queue, $options); |
82
|
|
|
} catch (ExceptionInterface $e) { |
83
|
|
|
throw new WorkerProcessException( |
84
|
|
|
'Caught exception while processing queue ' . $name, |
85
|
|
|
$e->getCode(), |
86
|
|
|
$e |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
$output->writeln(sprintf('Finished worker for queue <info>%s</info>', $name)); |
90
|
|
|
$output->writeln($messages); |
91
|
|
|
|
92
|
|
|
return 0; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @deprecated |
97
|
|
|
* |
98
|
|
|
* @param $queueName |
99
|
|
|
* @param array $messages |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
protected function formatOutput($queueName, array $messages = []) |
104
|
|
|
{ |
105
|
|
|
$messages = implode("\n", array_map(function ($m) { |
106
|
|
|
return sprintf(' - %s', $m); |
107
|
|
|
}, $messages)); |
108
|
|
|
|
109
|
|
|
return sprintf( |
110
|
|
|
"Finished worker for queue '%s':\n%s\n", |
111
|
|
|
$queueName, |
112
|
|
|
$messages |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.