-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsyncConsumer.php
153 lines (136 loc) · 3.78 KB
/
AsyncConsumer.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
<?php declare(strict_types=1);
namespace RabbitMqBundle\Consumer;
use Exception;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Wire\AMQPTable;
use RabbitMqBundle\Connection\Configurator;
use RabbitMqBundle\Connection\ConnectionManager;
use RabbitMqBundle\Consumer\Callback\Exception\CallbackException;
use RabbitMqBundle\Utils\Message;
use Throwable;
/**
* Class AsyncConsumer
*
* @package RabbitMqBundle\Consumer
*/
class AsyncConsumer extends ConsumerAbstract
{
use DebugMessageTrait;
/**
* @var AsyncCallbackInterface
*/
protected $callback;
/**
* @var int
*/
private int $timer = 2;
/**
* AsyncConsumer constructor.
*
* @param ConnectionManager $connectionManager
* @param Configurator $configurator
* @param AsyncCallbackInterface $callback
* @param string $queue
* @param string $consumerTag
* @param bool $noLocal
* @param bool $noAck
* @param bool $exclusive
* @param bool $nowait
* @param int $prefetchCount
* @param int $prefetchSize
*/
public function __construct(
ConnectionManager $connectionManager,
Configurator $configurator,
AsyncCallbackInterface $callback,
string $queue = '',
string $consumerTag = '',
bool $noLocal = FALSE,
bool $noAck = FALSE,
bool $exclusive = FALSE,
bool $nowait = FALSE,
int $prefetchCount = 0,
int $prefetchSize = 0
)
{
parent::__construct(
$connectionManager,
$configurator,
$queue,
$consumerTag,
$noLocal,
$noAck,
$exclusive,
$nowait,
$prefetchCount,
$prefetchSize
);
$this->callback = $callback;
}
/**
* @throws Exception
*/
public function consume(): void
{
$this->runAsyncConsumer();
}
/**
* @throws Exception
*/
public function restart(): void
{
$this->wait();
$this->consume();
}
/**
* @throws Exception
*/
private function runAsyncConsumer(): void
{
/** @var mixed[] $arguments */
$arguments = new AMQPTable($this->arguments);
$channel = $this->getChannel();
$channel->basic_consume(
$this->queue,
$this->consumerTag,
$this->noLocal,
$this->noAck,
$this->exclusive,
$this->nowait,
function (AMQPMessage $message): void {
try {
$this->callback
->processMessage(
$message,
$this->connectionManager->getConnection(),
(int) $this->channelId
)
->wait();
} catch (Throwable $e) {
$m = sprintf('RabbitMq callback error: %s', $e->getMessage());
$this->logger->error($m, ['Message' => $message]);
Message::nack($message, $this->connectionManager->getConnection(), (int) $this->channelId, TRUE);
throw new CallbackException($m, $e->getCode(), $e);
}
},
NULL,
$arguments
);
while ($channel->is_consuming()) {
$channel->wait();
}
}
/**
*
*/
private function wait(): void
{
sleep($this->timer);
if ($this->timer < 10) {
$this->timer += 2;
if ($this->timer > 10) {
$this->timer = 10;
}
}
}
}