-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractWorker.php
431 lines (375 loc) · 13.7 KB
/
AbstractWorker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
<?php
/**
* @author Marwan Al-Soltany <MarwanAlsoltany@gmail.com>
* @copyright Marwan Al-Soltany 2020
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace MAKS\AmqpAgent\Worker;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Wire\AMQPTable;
use PhpAmqpLib\Exception\AMQPInvalidArgumentException;
use PhpAmqpLib\Exception\AMQPTimeoutException;
use PhpAmqpLib\Exception\AMQPConnectionClosedException;
use MAKS\AmqpAgent\Worker\AbstractWorkerInterface;
use MAKS\AmqpAgent\Worker\WorkerCommandTrait;
use MAKS\AmqpAgent\Worker\WorkerMutationTrait;
use MAKS\AmqpAgent\Exception\MagicMethodsExceptionsTrait;
use MAKS\AmqpAgent\Exception\PropertyDoesNotExistException;
use MAKS\AmqpAgent\Exception\AmqpAgentException as Exception;
use MAKS\AmqpAgent\Config\AbstractWorkerParameters as Parameters;
/**
* An abstract class implementing the basic functionality of a worker.
* @since 1.0.0
* @api
*/
abstract class AbstractWorker implements AbstractWorkerInterface
{
use MagicMethodsExceptionsTrait {
__get as private __get_MMET;
__set as private __set_MMET;
}
use WorkerMutationTrait;
use WorkerCommandTrait;
/**
* The default connection options that the worker should use when no overrides are provided.
* @var array
*/
protected $connectionOptions;
/**
* The default channel options that the worker should use when no overrides are provided.
* @var array
*/
protected $channelOptions;
/**
* The default queue options that the worker should use when no overrides are provided.
* @var array
*/
protected $queueOptions;
/**
* The default connection of the worker.
* @var AMQPStreamConnection
*/
public $connection;
/**
* The default channel of the worker.
* @var AMQPChannel
*/
public $channel;
/**
* All opened connections of the worker.
* @var AMQPStreamConnection[]
*/
public $connections = [];
/**
* All opened channels of the the worker.
* @var AMQPChannel[]
*/
public $channels = [];
/**
* AbstractWorker object constructor.
* @param array $connectionOptions [optional] The overrides for the default connection options of the worker.
* @param array $channelOptions [optional] The overrides for the default channel options of the worker.
* @param array $queueOptions [optional] The overrides for the default queue options of the worker.
*/
public function __construct(
array $connectionOptions = [],
array $channelOptions = [],
array $queueOptions = []
) {
$this->connectionOptions = Parameters::patch($connectionOptions, 'CONNECTION_OPTIONS');
$this->channelOptions = Parameters::patch($channelOptions, 'CHANNEL_OPTIONS');
$this->queueOptions = Parameters::patch($queueOptions, 'QUEUE_OPTIONS');
}
/**
* Closes the connection with RabbitMQ server before destroying the object.
*/
public function __destruct()
{
$this->disconnect();
}
/**
* Gets a class member via public property access notation.
* @param string $member Property name.
* @return mixed
* @throws PropertyDoesNotExistException
*/
public function __get(string $member)
{
$isMember = property_exists($this, $member);
if ($isMember) {
return $this->{$member};
}
$this->__get_MMET($member);
}
/**
* Sets a class member via public property assignment notation.
* @param string $member Property name.
* @param array $array Array of overrides. The array type here is important, because only *Options properties should be overridable.
* @return void
* @throws PropertyDoesNotExistException
*/
public function __set(string $member, array $array)
{
$isMember = property_exists($this, $member);
$notProtected = $member !== 'mutation';
if ($isMember && $notProtected) {
$acceptedKeys = array_keys($this->{$member});
foreach ($array as $key => $value) {
if (in_array($key, $acceptedKeys)) {
$this->{$member}[$key] = $value;
}
}
return;
}
$this->__set_MMET($member, $array);
}
/**
* Closes the connection or the channel or both with RabbitMQ server.
* @param AMQPStreamConnection|AMQPChannel|AMQPMessage ...$object The object that should be used to close the channel or the connection.
* @return bool True on success.
* @throws Exception
*/
public static function shutdown(...$object): bool
{
$successful = true;
$parameters = [];
foreach ($object as $class) {
$parameters[] = is_object($class) ? get_class($class) : gettype($class);
if (
$class instanceof AMQPStreamConnection ||
$class instanceof AMQPChannel ||
$class instanceof AMQPMessage
) {
try {
if (!($class instanceof AMQPMessage)) {
$class->close();
continue;
}
$class->getChannel()->close();
} catch (AMQPConnectionClosedException $e) {
// No need to throw the exception here as it's extraneous. This error
// happens when a channel gets closed multiple times in different ways.
}
} else {
$successful = false;
}
}
if ($successful) {
return $successful;
}
throw new Exception(
sprintf(
'The passed parameter must be of type %s, %s or %s or a combination of them. Given parameter(s) has/have the type(s): %s!',
AMQPStreamConnection::class,
AMQPChannel::class,
AMQPMessage::class,
implode(', ', $parameters)
)
);
}
/**
* Returns an AMQPTable object.
* @param array $array An array of the option wished to be turn into the an arguments object.
* @return AMQPTable
*/
public static function arguments(array $array): AMQPTable
{
return new AMQPTable($array);
}
/**
* Establishes a connection with RabbitMQ server and opens a channel for the worker in the opened connection, it also sets both of them as defaults.
* @return self
*/
public function connect()
{
if (empty($this->connection)) {
$this->connection = $this->getNewConnection();
}
if (empty($this->channel)) {
$this->channel = $this->getNewChannel();
}
return $this;
}
/**
* Closes all open channels and connections with RabbitMQ server.
* @return self
*/
public function disconnect()
{
if (count($this->channels)) {
foreach ($this->channels as $channel) {
$channel->close();
}
$this->channel = null;
$this->channels = [];
}
if (count($this->connections)) {
foreach ($this->connections as $connection) {
$connection->close();
}
$this->connection = null;
$this->connections = [];
}
return $this;
}
/**
* Executes `self::disconnect()` and `self::connect()` respectively. Note that this method will not restore old channels.
* @return self
*/
public function reconnect()
{
$this->disconnect();
$this->connect();
return $this;
}
/**
* Declares a queue on the default channel of the worker's connection with RabbitMQ server.
* @param array $parameters [optional] The overrides for the default queue options of the worker.
* @param AMQPChannel $_channel [optional] The channel that should be used instead of the default worker's channel.
* @return self
* @throws AMQPTimeoutException
*/
public function queue(?array $parameters = null, ?AMQPChannel $_channel = null)
{
$changes = null;
if ($parameters) {
$changes = $this->mutateClassMember('queueOptions', $parameters);
}
$channel = $_channel ?: $this->channel;
try {
$channel->queue_declare(
$this->queueOptions['queue'],
$this->queueOptions['passive'],
$this->queueOptions['durable'],
$this->queueOptions['exclusive'],
$this->queueOptions['auto_delete'],
$this->queueOptions['nowait'],
$this->queueOptions['arguments'],
$this->queueOptions['ticket']
);
} catch (AMQPTimeoutException $error) { // @codeCoverageIgnore
Exception::rethrow($error); // @codeCoverageIgnore
}
if ($changes) {
$this->mutateClassMember('queueOptions', $changes);
}
return $this;
}
/**
* Returns the default connection of the worker. If the worker is not connected, it returns null.
* @since 1.1.0
* @return AMQPStreamConnection|null
*/
public function getConnection(): ?AMQPStreamConnection
{
return $this->connection;
}
/**
* Sets the passed connection as the default connection of the worker.
* @since 1.1.0
* @param AMQPStreamConnection $connection The connection that should be as the default connection of the worker.
* @return self
*/
public function setConnection(AMQPStreamConnection $connection)
{
$this->connection = $connection;
return $this;
}
/**
* Opens a new connection to RabbitMQ server and returns it. Connections returned by this method pushed to connections array and are not set as default automatically.
* @since 1.1.0
* @param array|null $parameters
* @return AMQPStreamConnection
*/
public function getNewConnection(array $parameters = null): AMQPStreamConnection
{
$changes = null;
if ($parameters) {
$changes = $this->mutateClassMember('connectionOptions', $parameters);
}
$this->connections[] = $connection = new AMQPStreamConnection(
$this->connectionOptions['host'],
$this->connectionOptions['port'],
$this->connectionOptions['user'],
$this->connectionOptions['password'],
$this->connectionOptions['vhost'],
$this->connectionOptions['insist'],
$this->connectionOptions['login_method'],
$this->connectionOptions['login_response'],
$this->connectionOptions['locale'],
$this->connectionOptions['connection_timeout'],
$this->connectionOptions['read_write_timeout'],
$this->connectionOptions['context'],
$this->connectionOptions['keepalive'],
$this->connectionOptions['heartbeat'],
$this->connectionOptions['channel_rpc_timeout'],
$this->connectionOptions['ssl_protocol']
);
if ($changes) {
$this->mutateClassMember('connectionOptions', $changes);
}
return $connection;
}
/**
* Returns the default channel of the worker. If the worker is not connected, it returns null.
* @return AMQPChannel|null
*/
public function getChannel(): ?AMQPChannel
{
return $this->channel;
}
/**
* Sets the passed channel as the default channel of the worker.
* @since 1.1.0
* @param AMQPChannel $channel The channel that should be as the default channel of the worker.
* @return self
*/
public function setChannel(AMQPChannel $channel)
{
$this->channel = $channel;
return $this;
}
/**
* Returns a new channel on the the passed connection of the worker. If no connection is passed, it uses the default connection. If the worker is not connected, it returns null.
* @param array|null $parameters [optional] The overrides for the default channel options of the worker.
* @param AMQPStreamConnection|null $_connection [optional] The connection that should be used instead of the default worker's connection.
* @return AMQPChannel|null
*/
public function getNewChannel(array $parameters = null, ?AMQPStreamConnection $_connection = null): ?AMQPChannel
{
$changes = null;
if ($parameters) {
$changes = $this->mutateClassMember('channelOptions', $parameters);
}
$connection = $_connection ?: $this->connection;
$channel = null;
if (isset($connection)) {
$this->channels[] = $channel = $connection->channel(
$this->channelOptions['channel_id']
);
}
if ($changes) {
$this->mutateClassMember('channelOptions', $changes);
}
return $channel;
}
/**
* Fetches a channel object identified by the passed id (channel_id). If not found, it returns null.
* @param int $channelId The id of the channel wished to be fetched.
* @param AMQPStreamConnection|null $_connection [optional] The connection that should be used instead of the default worker's connection.
* @return AMQPChannel|null
*/
public function getChannelById(int $channelId, ?AMQPStreamConnection $_connection = null): ?AMQPChannel
{
$connection = $_connection ?: $this->connection;
$channels = $connection->channels;
if (array_key_exists($channelId, $channels)) {
return $channels[$channelId];
}
return null;
}
}