|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of dispositif/wikibot application |
|
4
|
|
|
* 2019 : Philippe M. <[email protected]> |
|
5
|
|
|
* For the full copyright and MIT license information, please view the LICENSE file. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace App\Application; |
|
11
|
|
|
|
|
12
|
|
|
use App\Infrastructure\ServiceFactory; |
|
13
|
|
|
use ErrorException; |
|
14
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @notused |
|
18
|
|
|
* todo not used |
|
19
|
|
|
* Consume RabbitMQ queue and acknowledge each message. |
|
20
|
|
|
* Class AbstractWorker. |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class AbstractQueueWorker |
|
23
|
|
|
{ |
|
24
|
|
|
public const QUEUE_NAME = 'AbstractQueueWorker'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Connect and obtain the messages from queue. |
|
28
|
|
|
* |
|
29
|
|
|
* @throws ErrorException |
|
30
|
|
|
*/ |
|
31
|
|
|
public function run() |
|
32
|
|
|
{ |
|
33
|
|
|
$channel = ServiceFactory::queueChannel(static::QUEUE_NAME); |
|
34
|
|
|
|
|
35
|
|
|
echo " [*] Waiting for messages. To exit press CTRL+C\n"; |
|
36
|
|
|
|
|
37
|
|
|
// qos : one message to a worker at a time (next one after acknowledge) |
|
38
|
|
|
$channel->basic_qos(null, 1, null); |
|
39
|
|
|
// callback to $this->msgProcess(). |
|
40
|
|
|
// TODO check Rector refac |
|
41
|
|
|
$channel->basic_consume(static::QUEUE_NAME, '', false, false, false, false, function (\PhpAmqpLib\Message\AMQPMessage $msg) : void { |
|
42
|
|
|
$this->msgProcess($msg); |
|
43
|
|
|
}); |
|
44
|
|
|
|
|
45
|
|
|
while ($channel->is_consuming()) { |
|
46
|
|
|
$channel->wait(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$channel->close(); |
|
50
|
|
|
//$connection->close(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Process with one of the queue message. |
|
55
|
|
|
* Note: Callback needs a public function. See call_user_func(). |
|
56
|
|
|
*/ |
|
57
|
|
|
abstract public function msgProcess(AMQPMessage $msg): void; |
|
58
|
|
|
|
|
59
|
|
|
protected function acknowledge(AMQPMessage $msg) |
|
60
|
|
|
{ |
|
61
|
|
|
$msg->delivery_info['channel'] |
|
|
|
|
|
|
62
|
|
|
->basic_ack($msg->delivery_info['delivery_tag']); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|