|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sonata Project package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sonata\NotificationBundle\Backend; |
|
13
|
|
|
|
|
14
|
|
|
use PhpAmqpLib\Channel\AMQPChannel; |
|
15
|
|
|
use PhpAmqpLib\Connection\AMQPConnection; |
|
16
|
|
|
use Sonata\NotificationBundle\Exception\BackendNotFoundException; |
|
17
|
|
|
use Sonata\NotificationBundle\Model\MessageInterface; |
|
18
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
19
|
|
|
use ZendDiagnostics\Result\Failure; |
|
20
|
|
|
use ZendDiagnostics\Result\Success; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Producer side of the rabbitmq backend. |
|
24
|
|
|
*/ |
|
25
|
|
|
class AMQPBackendDispatcher extends QueueBackendDispatcher |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $settings; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var AMQPChannel |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $channel; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var AMQPConnection |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $connection; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param array $settings |
|
44
|
|
|
* @param array $queues |
|
45
|
|
|
* @param string $defaultQueue |
|
46
|
|
|
* @param array $backends |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct(array $settings, array $queues, $defaultQueue, array $backends) |
|
49
|
|
|
{ |
|
50
|
|
|
parent::__construct($queues, $defaultQueue, $backends); |
|
51
|
|
|
|
|
52
|
|
|
$this->settings = $settings; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return AMQPChannelannel |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getChannel() |
|
59
|
|
|
{ |
|
60
|
|
|
if (!$this->channel) { |
|
61
|
|
|
$this->connection = new AMQPConnection( |
|
|
|
|
|
|
62
|
|
|
$this->settings['host'], |
|
63
|
|
|
$this->settings['port'], |
|
64
|
|
|
$this->settings['user'], |
|
65
|
|
|
$this->settings['pass'], |
|
66
|
|
|
$this->settings['vhost'] |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
$this->channel = $this->connection->channel(); |
|
70
|
|
|
|
|
71
|
|
|
register_shutdown_function(array($this, 'shutdown')); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $this->channel; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritdoc} |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getBackend($type) |
|
81
|
|
|
{ |
|
82
|
|
|
$default = null; |
|
83
|
|
|
|
|
84
|
|
|
if (count($this->queues) === 0) { |
|
85
|
|
|
foreach ($this->backends as $backend) { |
|
86
|
|
|
if ($backend['type'] === 'default') { |
|
87
|
|
|
return $backend['backend']; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
foreach ($this->backends as $backend) { |
|
93
|
|
|
if ('all' === $type && $backend['type'] === '') { |
|
94
|
|
|
return $backend['backend']; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if ($backend['type'] === $type) { |
|
98
|
|
|
return $backend['backend']; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if ($backend['type'] === $this->defaultQueue) { |
|
102
|
|
|
$default = $backend['backend']; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if ($default === null) { |
|
107
|
|
|
throw new BackendNotFoundException('Could not find a message backend for the type '.$type); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $default; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* {@inheritdoc} |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getIterator() |
|
117
|
|
|
{ |
|
118
|
|
|
throw new \RuntimeException('You need to use a specific rabbitmq backend supporting the selected queue to run a consumer.'); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* {@inheritdoc} |
|
123
|
|
|
*/ |
|
124
|
|
|
public function handle(MessageInterface $message, EventDispatcherInterface $dispatcher) |
|
125
|
|
|
{ |
|
126
|
|
|
throw new \RuntimeException('You need to use a specific rabbitmq backend supporting the selected queue to run a consumer.'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* {@inheritdoc} |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getStatus() |
|
133
|
|
|
{ |
|
134
|
|
|
try { |
|
135
|
|
|
$this->getChannel(); |
|
136
|
|
|
$output = $this->getApiQueueStatus(); |
|
137
|
|
|
$checked = 0; |
|
138
|
|
|
$missingConsumers = array(); |
|
139
|
|
|
|
|
140
|
|
|
foreach ($this->queues as $queue) { |
|
141
|
|
|
foreach ($output as $q) { |
|
142
|
|
|
if ($q['name'] === $queue['queue']) { |
|
143
|
|
|
++$checked; |
|
144
|
|
|
if ($q['consumers'] === 0) { |
|
145
|
|
|
$missingConsumers[] = $queue['queue']; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
if ($checked !== count($this->queues)) { |
|
152
|
|
|
return new Failure('Not all queues for the available notification types registered in the rabbitmq broker. Are the consumer commands running?'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
if (count($missingConsumers) > 0) { |
|
156
|
|
|
return new Failure('There are no rabbitmq consumers running for the queues: '.implode(', ', $missingConsumers)); |
|
157
|
|
|
} |
|
158
|
|
|
} catch (\Exception $e) { |
|
159
|
|
|
return new Failure($e->getMessage()); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return new Success('Channel is running (RabbitMQ) and consumers for all queues available.'); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* {@inheritdoc} |
|
167
|
|
|
*/ |
|
168
|
|
|
public function cleanup() |
|
169
|
|
|
{ |
|
170
|
|
|
throw new \RuntimeException('You need to use a specific rabbitmq backend supporting the selected queue to run a consumer.'); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function shutdown() |
|
174
|
|
|
{ |
|
175
|
|
|
if ($this->channel) { |
|
176
|
|
|
$this->channel->close(); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
if ($this->connection) { |
|
180
|
|
|
$this->connection->close(); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* {@inheritdoc} |
|
186
|
|
|
*/ |
|
187
|
|
|
public function initialize() |
|
188
|
|
|
{ |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Calls the rabbitmq management api /api/<vhost>/queues endpoint to list the available queues. |
|
193
|
|
|
* |
|
194
|
|
|
* @see http://hg.rabbitmq.com/rabbitmq-management/raw-file/3646dee55e02/priv/www-api/help.html |
|
195
|
|
|
* |
|
196
|
|
|
* @return array |
|
197
|
|
|
*/ |
|
198
|
|
|
protected function getApiQueueStatus() |
|
199
|
|
|
{ |
|
200
|
|
|
if (class_exists('Guzzle\Http\Client') === false) { |
|
201
|
|
|
throw new \RuntimeException('The guzzle http client library is required to run rabbitmq health checks. Make sure to add guzzle/guzzle to your composer.json.'); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
$client = new \Guzzle\Http\Client(); |
|
205
|
|
|
$client->setConfig(array('curl.options' => array(CURLOPT_CONNECTTIMEOUT_MS => 3000))); |
|
206
|
|
|
$request = $client->get(sprintf('%s/queues', $this->settings['console_url'])); |
|
207
|
|
|
$request->setAuth($this->settings['user'], $this->settings['pass']); |
|
208
|
|
|
|
|
209
|
|
|
return json_decode($request->send()->getBody(true), true); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
This class, trait or interface has been deprecated.