-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManager.php
More file actions
141 lines (116 loc) · 3.03 KB
/
Copy pathManager.php
File metadata and controls
141 lines (116 loc) · 3.03 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
<?php
namespace ADT\BackgroundQueue\Broker\PhpAmqpLib;
use Exception;
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
class Manager
{
const QUEUE_TOP_PRIORITY = 0;
private array $connectionParams;
private array $queueParams;
private ?AMQPStreamConnection $connection = null;
private ?AMQPChannel $channel = null;
private array $initQueues;
private array $initExchanges;
private bool $initQos = false;
public function __construct(array $connectionParams, array $queueParams)
{
$this->connectionParams = $connectionParams;
$this->queueParams = $queueParams;
}
private function getConnection(): AMQPStreamConnection
{
if (!$this->connection) {
$this->connection = new AMQPStreamConnection($this->connectionParams['host'], $this->connectionParams['port'] ?? 5672, $this->connectionParams['user'], $this->connectionParams['password']);
}
return $this->connection;
}
public function getChannel(): AMQPChannel
{
if (!$this->channel) {
$this->channel = $this->getConnection()->channel();
$this->channel->confirm_select();
$this->channel->set_nack_handler(function (AMQPMessage $message) {
throw new Exception('Internal error (basic.nack)');
});
$this->channel->set_return_listener(
function ($replyCode, $replyText, $exchange, $routingKey, AMQPMessage $message) {
throw new Exception("Code: $replyCode, Text: $replyText, Exchange: $exchange, Routing Key: $routingKey");
}
);
register_shutdown_function(function() {
$this->channel->wait_for_pending_acks_returns();
$this->channel->close();
$this->connection->close();
});
}
return $this->channel;
}
public function closeChannel(): void
{
$channel = $this->channel;
$this->channel = null;
$this->initQos = false;
if ($channel) {
$channel->close();
}
}
public function createExchange(string $exchange)
{
if (isset($this->initExchanges[$exchange])) {
return;
}
$this->getChannel()->exchange_declare(
$exchange,
'direct',
false,
true,
false,
);
$this->initExchanges[$exchange] = true;
}
public function createQueue(string $queue, ?string $exchange = null, array $additionalArguments = [])
{
if (isset($this->initQueues[$queue])) {
return;
}
$arguments = $this->queueParams['arguments'];
if ($additionalArguments) {
$arguments = array_merge($arguments, $additionalArguments);
}
$this->getChannel()->queue_declare(
$queue,
false,
true,
false,
false,
false,
$arguments
);
if ($exchange) {
$this->getChannel()->queue_bind($queue, $exchange, $queue);
}
$this->initQueues[$queue] = true;
}
public function setupQos()
{
if ($this->initQos) {
return;
}
$this->getChannel()->basic_qos(
0,
1,
false
);
$this->initQos = true;
}
public function getQueueWithPriority(string $queue, int $priority): string
{
return $queue . '_' . $priority;
}
public function parseQueueAndPriority(string $queueWithPriority): array
{
return explode('_', $queueWithPriority);
}
}