-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAMQP.php
More file actions
226 lines (194 loc) · 8.39 KB
/
AMQP.php
File metadata and controls
226 lines (194 loc) · 8.39 KB
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
<?php
namespace Utopia\Queue\Broker;
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Connection\AbstractConnection;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Exchange\AMQPExchangeType;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Wire\AMQPTable;
use Utopia\Fetch\Client;
use Utopia\Queue\Consumer;
use Utopia\Queue\Error\Retryable;
use Utopia\Queue\Message;
use Utopia\Queue\Publisher;
use Utopia\Queue\Queue;
use Utopia\Queue\Result\Commit;
use Utopia\Queue\Result\NoCommit;
class AMQP implements Publisher, Consumer
{
private ?AMQPChannel $channel = null;
private array $exchangeArguments = [];
private array $queueArguments = [];
private array $consumerArguments = [];
/**
* @var callable(AbstractConnection $connection): void
*/
private $connectionConfigHook;
/**
* @var callable(AMQPChannel $channel): void
*/
private $channelConfigHook;
public function __construct(
private readonly string $host,
private readonly int $port = 5672,
private readonly int $httpPort = 15672,
private readonly ?string $user = null,
private readonly ?string $password = null,
private readonly string $vhost = '/',
private readonly int $heartbeat = 0,
private readonly float $connectTimeout = 3.0,
private readonly float $readWriteTimeout = 3.0,
) {
}
public function setExchangeArgument(string $key, string $value): void
{
$this->exchangeArguments[$key] = $value;
}
public function setQueueArgument(string $key, string $value): void
{
$this->queueArguments[$key] = $value;
}
public function setConsumerArguments(string $key, string $value): void
{
$this->consumerArguments[$key] = $value;
}
public function configureConnection(callable $callback): void
{
$this->connectionConfigHook = $callback;
}
public function configureChannel(callable $callback): void
{
$this->channelConfigHook = $callback;
}
public function consume(Queue $queue, callable $messageCallback, callable $successCallback, callable $errorCallback): void
{
$processMessage = function (AMQPMessage $amqpMessage) use ($messageCallback, $successCallback, $errorCallback) {
try {
$nextMessage = json_decode($amqpMessage->getBody(), associative: true) ?? false;
if (!$nextMessage) {
$amqpMessage->nack();
return;
}
$nextMessage['timestamp'] = (int)$nextMessage['timestamp'];
$message = new Message($nextMessage);
$result = $messageCallback($message);
match (true) {
$result instanceof Commit => $amqpMessage->ack(true),
$result instanceof NoCommit => null,
default => $amqpMessage->ack()
};
$successCallback($message);
} catch (Retryable $e) {
$amqpMessage->nack(requeue: true);
$errorCallback($message ?? null, $e);
} catch (\Throwable $th) {
$amqpMessage->nack();
$errorCallback($message ?? null, $th);
}
};
$this->withChannel(function (AMQPChannel $channel) use ($queue, $processMessage) {
// It's good practice for the consumer to set up exchange and queues.
// This approach uses TOPICs (https://www.rabbitmq.com/tutorials/amqp-concepts#exchange-topic) and
// dead-letter-exchanges (https://www.rabbitmq.com/docs/dlx) for failed messages.
// 1. Declare the exchange and a dead-letter-exchange.
$channel->exchange_declare($queue->namespace, AMQPExchangeType::TOPIC, durable: true, auto_delete: false, arguments: new AMQPTable($this->exchangeArguments));
$channel->exchange_declare("{$queue->namespace}.failed", AMQPExchangeType::TOPIC, durable: true, auto_delete: false, arguments: new AMQPTable($this->exchangeArguments));
// 2. Declare the working queue and configure the DLX for receiving rejected messages.
$channel->queue_declare($queue->name, durable: true, auto_delete: false, arguments: new AMQPTable(array_merge($this->queueArguments, ['x-dead-letter-exchange' => "{$queue->namespace}.failed"])));
$channel->queue_bind($queue->name, $queue->namespace, routing_key: $queue->name);
// 3. Declare the dead-letter-queue and bind it to the DLX.
$channel->queue_declare("{$queue->name}.failed", durable: true, auto_delete: false, arguments: new AMQPTable($this->queueArguments));
$channel->queue_bind("{$queue->name}.failed", "{$queue->namespace}.failed", routing_key: $queue->name);
// 4. Instruct to consume on the working queue.
$channel->basic_consume($queue->name, callback: $processMessage, arguments: new AMQPTable($this->consumerArguments));
});
// Run ->consume in own callback to avoid re-running queue creation flow on error.
$this->withChannel(function (AMQPChannel $channel) {
// 5. Consume. This blocks until the connection gets closed.
$channel->consume();
});
}
public function close(): void
{
$this->channel?->stopConsume();
$this->channel?->getConnection()?->close();
}
public function enqueue(Queue $queue, array $payload, bool $priority = false): bool
{
$payload = [
'pid' => \uniqid(more_entropy: true),
'queue' => $queue->name,
'timestamp' => time(),
'payload' => $payload
];
$message = new AMQPMessage(json_encode($payload), ['content_type' => 'application/json', 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT]);
$this->withChannel(function (AMQPChannel $channel) use ($message, $queue) {
$channel->basic_publish($message, $queue->namespace, routing_key: $queue->name);
});
return true;
}
public function retry(Queue $queue, ?int $limit = null): void
{
// This is a no-op for AMQP
}
/**
* @throws \Exception
*/
public function getQueueSize(Queue $queue, bool $failedJobs = false): int
{
$queueName = $queue->name;
if ($failedJobs) {
$queueName = $queueName . '.failed';
}
$client = new Client();
$response = $client->fetch(sprintf('http://%s:%s@%s:%s/api/queues/%s/%s', $this->user, $this->password, $this->host, $this->httpPort, urlencode($this->vhost), $queueName));
// If this queue does not exist (yet), the queue size is 0.
if ($response->getStatusCode() === 404) {
return 0;
}
if ($response->getStatusCode() !== 200) {
throw new \Exception(sprintf('Invalid status code %d: %s', $response->getStatusCode(), $response->getBody()));
}
$data = $response->json();
return $data['messages'] ?? 0;
}
/**
* @param callable(AMQPChannel $channel): void $callback
* @throws \Exception
*/
private function withChannel(callable $callback): void
{
$createChannel = function (): AMQPChannel {
$connection = new AMQPStreamConnection(
$this->host,
$this->port,
$this->user,
$this->password,
$this->vhost,
connection_timeout: $this->connectTimeout,
read_write_timeout: $this->readWriteTimeout,
heartbeat: $this->heartbeat,
);
if (is_callable($this->connectionConfigHook)) {
call_user_func($this->connectionConfigHook, $connection);
}
$channel = $connection->channel();
if (is_callable($this->channelConfigHook)) {
call_user_func($this->channelConfigHook, $channel);
}
return $channel;
};
if (!$this->channel) {
$this->channel = $createChannel();
}
try {
$callback($this->channel);
} catch (\Throwable) {
// createChannel() might throw, in that case set the channel to `null` first.
$this->channel = null;
// try creating a new connection once, if this still fails, throw the error
$this->channel = $createChannel();
$callback($this->channel);
}
}
}