|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RabbitMqModule\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use PhpAmqpLib\Exception\AMQPTimeoutException; |
|
6
|
|
|
use Zend\Console\ColorInterface; |
|
7
|
|
|
use RabbitMqModule\Consumer; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class ConsumerController |
|
11
|
|
|
* |
|
12
|
|
|
* @package RabbitMqModule\Controller |
|
13
|
|
|
*/ |
|
14
|
|
|
class ConsumerController extends AbstractConsoleController |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Consumer |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $consumer; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Default action if none provided |
|
23
|
|
|
* |
|
24
|
|
|
* @return \Zend\Console\Response |
|
25
|
|
|
*/ |
|
26
|
3 |
|
public function indexAction() |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var \Zend\Console\Request $request */ |
|
29
|
3 |
|
$request = $this->getRequest(); |
|
30
|
|
|
/** @var \Zend\Console\Response $response */ |
|
31
|
3 |
|
$response = $this->getResponse(); |
|
32
|
|
|
|
|
33
|
3 |
|
$this->getConsole()->writeLine(sprintf('Starting consumer %s', $request->getParam('name'))); |
|
34
|
|
|
|
|
35
|
3 |
|
$withoutSignals = $request->getParam('without-signals') || $request->getParam('w'); |
|
36
|
|
|
|
|
37
|
3 |
|
$serviceName = sprintf('rabbitmq_module.consumer.%s', $request->getParam('name')); |
|
38
|
|
|
|
|
39
|
3 |
|
if (!$this->container->has($serviceName)) { |
|
40
|
1 |
|
$this->getConsole()->writeLine( |
|
41
|
1 |
|
sprintf('No consumer with name "%s" found', $request->getParam('name')), |
|
42
|
|
|
ColorInterface::RED |
|
43
|
1 |
|
); |
|
44
|
1 |
|
$response->setErrorLevel(1); |
|
45
|
|
|
|
|
46
|
1 |
|
return $response; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/* @var \RabbitMqModule\Consumer $consumer */ |
|
50
|
2 |
|
$this->consumer = $this->container->get($serviceName); |
|
51
|
2 |
|
$this->consumer->setSignalsEnabled(!$withoutSignals); |
|
52
|
|
|
|
|
53
|
2 |
|
if ($withoutSignals) { |
|
54
|
1 |
|
define('AMQP_WITHOUT_SIGNALS', true); |
|
55
|
1 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
// @codeCoverageIgnoreStart |
|
58
|
|
|
if (!$withoutSignals && extension_loaded('pcntl')) { |
|
59
|
|
|
if (!function_exists('pcntl_signal')) { |
|
60
|
|
|
throw new \BadFunctionCallException( |
|
61
|
|
|
'Function \'pcntl_signal\' is referenced in the php.ini \'disable_functions\' and can\'t be called.' |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
pcntl_signal(SIGTERM, [$this, 'stopConsumer']); |
|
66
|
|
|
pcntl_signal(SIGINT, [$this, 'stopConsumer']); |
|
67
|
|
|
} |
|
68
|
|
|
// @codeCoverageIgnoreEnd |
|
69
|
|
|
|
|
70
|
2 |
|
$this->consumer->consume(); |
|
71
|
|
|
|
|
72
|
2 |
|
return $response; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* List available consumers |
|
77
|
|
|
* |
|
78
|
|
|
* @return string|null |
|
79
|
|
|
*/ |
|
80
|
3 |
|
public function listAction() |
|
81
|
|
|
{ |
|
82
|
|
|
/** @var array $config */ |
|
83
|
3 |
|
$config = $this->container->get('Configuration'); |
|
84
|
|
|
|
|
85
|
3 |
|
if (!array_key_exists('rabbitmq_module', $config) |
|
86
|
3 |
|
|| !array_key_exists('consumer', $config['rabbitmq_module'])) { |
|
87
|
1 |
|
return 'No \'rabbitmq_module.consumer\' configuration key found!'; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
$consumers = $config['rabbitmq_module']['consumer']; |
|
91
|
|
|
|
|
92
|
2 |
|
if (!is_array($consumers) || count($consumers) === 0) { |
|
93
|
1 |
|
return 'No consumers defined!'; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
foreach ($consumers as $name => $configuration) { |
|
97
|
1 |
|
$description = array_key_exists('description', $configuration) ? (string)$configuration['description'] : ''; |
|
98
|
1 |
|
$this->getConsole()->writeLine(sprintf( |
|
99
|
1 |
|
'- %s: %s', |
|
100
|
1 |
|
$this->getConsole()->colorize($name, ColorInterface::LIGHT_GREEN), |
|
101
|
1 |
|
$this->getConsole()->colorize($description, ColorInterface::LIGHT_YELLOW) |
|
102
|
1 |
|
)); |
|
103
|
1 |
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
return null; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Stop consumer. |
|
110
|
|
|
*/ |
|
111
|
1 |
|
public function stopConsumer() |
|
112
|
|
|
{ |
|
113
|
1 |
|
if ($this->consumer instanceof Consumer) { |
|
114
|
1 |
|
$this->consumer->forceStopConsumer(); |
|
115
|
|
|
try { |
|
116
|
1 |
|
$this->consumer->stopConsuming(); |
|
117
|
1 |
|
} catch (AMQPTimeoutException $e) { |
|
118
|
|
|
// ignore |
|
119
|
|
|
} |
|
120
|
1 |
|
} |
|
121
|
1 |
|
$this->callExit(0); |
|
122
|
1 |
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param Consumer $consumer |
|
126
|
|
|
* |
|
127
|
|
|
* @return $this |
|
128
|
|
|
*/ |
|
129
|
1 |
|
public function setConsumer(Consumer $consumer) |
|
130
|
|
|
{ |
|
131
|
1 |
|
$this->consumer = $consumer; |
|
132
|
|
|
|
|
133
|
1 |
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param int $code |
|
138
|
|
|
* @codeCoverageIgnore |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function callExit($code) |
|
141
|
|
|
{ |
|
142
|
|
|
exit($code); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|