|
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 Aist\Queue\Console\Helper\QueueHelper; |
|
13
|
|
|
use SlmQueue\Controller\Exception\WorkerProcessException; |
|
14
|
|
|
use SlmQueue\Exception\ExceptionInterface; |
|
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\Output\OutputInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Queue Worker |
|
22
|
|
|
*/ |
|
23
|
|
|
class StartQueueWorkerCommand extends Command |
|
24
|
|
|
{ |
|
25
|
|
|
const NAME = 'queue:worker:start'; |
|
26
|
|
|
|
|
27
|
|
|
const DESCRIPTION = 'Process queue worker.'; |
|
28
|
|
|
|
|
29
|
|
|
const HELP = <<< 'EOT' |
|
30
|
|
|
Process queue worker. |
|
31
|
|
|
EOT; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function configure() |
|
37
|
|
|
{ |
|
38
|
|
|
$this |
|
39
|
|
|
->setName(self::NAME) |
|
40
|
|
|
->setDescription(self::DESCRIPTION) |
|
41
|
|
|
->setHelp(self::HELP) |
|
42
|
|
|
->setDefinition([ |
|
43
|
|
|
new InputArgument( |
|
44
|
|
|
'name', |
|
45
|
|
|
InputArgument::REQUIRED, |
|
46
|
|
|
'The queue name.' |
|
47
|
|
|
), |
|
48
|
|
|
]) |
|
49
|
|
|
; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
56
|
|
|
{ |
|
57
|
|
|
/** |
|
58
|
|
|
* Strange issue |
|
59
|
|
|
* Without this we can't use $this->getHelper('name') |
|
60
|
|
|
* but $this->getApplication()->getHelperSet()->get('name') |
|
61
|
|
|
*/ |
|
62
|
|
|
$this->setApplication($this->getApplication()); |
|
63
|
|
|
$queuePluginManager = $this->getHelper('qm'); |
|
64
|
|
|
$worker = $this->getHelper('worker'); |
|
65
|
|
|
$logger = $this->getHelper('logger'); |
|
66
|
|
|
|
|
67
|
|
|
$this->setProcessTitle('queue.' . $input->getArgument('name')); |
|
68
|
|
|
|
|
69
|
|
|
$output->writeln( |
|
70
|
|
|
'Processing queue <info>' . $input->getArgument('name') . '</info> PID <info>' . getmypid() . '</info>' |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
return $this->executeWorkerCommand($input, $output, $queuePluginManager, $worker, $logger); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Execute worker |
|
78
|
|
|
* |
|
79
|
|
|
* @param InputInterface $input |
|
80
|
|
|
* @param OutputInterface $output |
|
81
|
|
|
* @param QueueHelper $queuePluginManager |
|
82
|
|
|
* @param $worker |
|
83
|
|
|
* |
|
84
|
|
|
* @return int |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function executeWorkerCommand( |
|
87
|
|
|
InputInterface $input, |
|
88
|
|
|
OutputInterface $output, |
|
89
|
|
|
QueueHelper $queuePluginManager, |
|
90
|
|
|
$worker, |
|
91
|
|
|
$logger |
|
92
|
|
|
) { |
|
93
|
|
|
$options = $input->getOptions(); |
|
94
|
|
|
$name = $input->getArgument('name'); |
|
95
|
|
|
$queue = $queuePluginManager->get($name); |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
try { |
|
98
|
|
|
$messages = $worker->processQueue($queue, $options); |
|
99
|
|
|
} catch (ExceptionInterface $e) { |
|
100
|
|
|
$logger->error( |
|
101
|
|
|
self::class, |
|
102
|
|
|
[ |
|
103
|
|
|
'queue' => $name, |
|
104
|
|
|
'code' => $e->getCode(), |
|
105
|
|
|
'message' => $e->getMessage(), |
|
106
|
|
|
] |
|
107
|
|
|
); |
|
108
|
|
|
throw new WorkerProcessException( |
|
109
|
|
|
'Caught exception while processing queue ' . $name, |
|
110
|
|
|
$e->getCode(), |
|
111
|
|
|
$e |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
$output->writeln(sprintf('Finished worker for queue <info>%s</info>', $name)); |
|
115
|
|
|
$output->writeln($messages); |
|
116
|
|
|
|
|
117
|
|
|
return 0; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.