1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Daikon\RabbitMq3\Migration; |
4
|
|
|
|
5
|
|
|
use Daikon\Dbal\Migration\MigrationTrait; |
6
|
|
|
use GuzzleHttp\Exception\RequestException; |
7
|
|
|
|
8
|
|
|
trait RabbitMq3MigrationTrait |
9
|
|
|
{ |
10
|
|
|
const WAITING_SUFFIX = '.waiting'; |
11
|
|
|
const UNROUTED_SUFFIX = '.unrouted'; |
12
|
|
|
const REPUB_SUFFIX = '.repub'; |
13
|
|
|
|
14
|
|
|
use MigrationTrait; |
15
|
|
|
|
16
|
|
|
private function createMigrationList(string $exchange): void |
17
|
|
|
{ |
18
|
|
|
$this->declareExchange($exchange, 'topic', false, true, false, true); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
private function createMessagePipeline(string $exchange, int $repubInterval = 30000): void |
22
|
|
|
{ |
23
|
|
|
$waitExchange = $exchange.self::WAITING_SUFFIX; |
|
|
|
|
24
|
|
|
$waitQueue = $waitExchange; |
25
|
|
|
$unroutedExchange = $exchange.self::UNROUTED_SUFFIX; |
|
|
|
|
26
|
|
|
$unroutedQueue = $unroutedExchange; |
27
|
|
|
$repubExchange = $exchange.self::REPUB_SUFFIX; |
|
|
|
|
28
|
|
|
$repubQueue = $repubExchange; |
29
|
|
|
|
30
|
|
|
// Setup the default exchange and queue pipelines |
31
|
|
|
$this->declareExchange($unroutedExchange, 'fanout', false, true, false, true); //internal |
32
|
|
|
$this->declareExchange($repubExchange, 'fanout', false, true, false, true); //internal |
33
|
|
|
$this->declareExchange($waitExchange, 'fanout', false, true, false); |
34
|
|
|
$this->declareExchange($exchange, 'topic', false, true, false, false, false, [ |
35
|
|
|
'alternate-exchange' => $unroutedExchange |
36
|
|
|
]); |
37
|
|
|
$this->declareQueue($waitQueue, false, true, false, false, false, [ |
38
|
|
|
'x-dead-letter-exchange' => $exchange |
39
|
|
|
]); |
40
|
|
|
$this->bindQueue($waitQueue, $waitExchange); |
41
|
|
|
$this->declareQueue($unroutedQueue, false, true, false, false, false, [ |
42
|
|
|
'x-dead-letter-exchange' => $repubExchange, |
43
|
|
|
'x-message-ttl' => $repubInterval |
44
|
|
|
]); |
45
|
|
|
$this->bindQueue($unroutedQueue, $unroutedExchange); |
46
|
|
|
$this->declareQueue($repubQueue, false, true, false, false); |
47
|
|
|
$this->bindQueue($repubQueue, $repubExchange); |
48
|
|
|
|
49
|
|
|
$this->createShovel($repubExchange, $exchange, $repubQueue); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function deleteMessagePipeline(string $exchange): void |
53
|
|
|
{ |
54
|
|
|
$waitExchange = $exchange.self::WAITING_SUFFIX; |
|
|
|
|
55
|
|
|
$waitQueue = $waitExchange; |
56
|
|
|
$unroutedExchange = $exchange.self::UNROUTED_SUFFIX; |
|
|
|
|
57
|
|
|
$unroutedQueue = $unroutedExchange; |
58
|
|
|
$repubExchange = $exchange.self::REPUB_SUFFIX; |
|
|
|
|
59
|
|
|
$repubQueue = $repubExchange; |
60
|
|
|
|
61
|
|
|
$this->deleteShovel($repubExchange); |
62
|
|
|
$this->deleteExchange($waitExchange); |
63
|
|
|
$this->deleteExchange($unroutedExchange); |
64
|
|
|
$this->deleteExchange($repubExchange); |
65
|
|
|
$this->deleteExchange($exchange); |
66
|
|
|
$this->deleteQueue($waitQueue); |
67
|
|
|
$this->deleteQueue($unroutedQueue); |
68
|
|
|
$this->deleteQueue($repubQueue); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
View Code Duplication |
private function declareExchange( |
|
|
|
|
72
|
|
|
string $exchange, |
73
|
|
|
string $type, |
74
|
|
|
bool $passive = false, |
75
|
|
|
bool $durable = false, |
76
|
|
|
bool $autoDelete = true, |
77
|
|
|
bool $internal = false, |
78
|
|
|
bool $noWait = false, |
79
|
|
|
array $arguments = [] |
80
|
|
|
): void { |
81
|
|
|
$uri = sprintf('/api/exchanges/%s/%s', $this->getVhost(), $exchange); |
82
|
|
|
$this->connector->getConnection()->put($uri, [ |
|
|
|
|
83
|
|
|
'body' => json_encode([ |
84
|
|
|
'type' => $type, |
85
|
|
|
'passive' => $passive, |
86
|
|
|
'durable' => $durable, |
87
|
|
|
'auto_delete' => $autoDelete, |
88
|
|
|
'internal' => $internal, |
89
|
|
|
'nowait' => $noWait, |
90
|
|
|
'arguments' => $arguments |
91
|
|
|
]) |
92
|
|
|
]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
View Code Duplication |
private function bindExchange( |
|
|
|
|
96
|
|
|
string $source, |
97
|
|
|
string $dest, |
98
|
|
|
string $routingKey = '', |
99
|
|
|
bool $noWait = false, |
100
|
|
|
array $arguments = [] |
101
|
|
|
): void { |
102
|
|
|
$uri = sprintf('/api/bindings/%s/e/%s/e/%s', $this->getVhost(), $source, $dest); |
103
|
|
|
$this->connector->getConnection()->post($uri, [ |
|
|
|
|
104
|
|
|
'body' => json_encode([ |
105
|
|
|
'routing_key' => $routingKey, |
106
|
|
|
'nowait' => $noWait, |
107
|
|
|
'arguments' => $arguments |
108
|
|
|
]) |
109
|
|
|
]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function deleteExchange(string $exchange): void |
113
|
|
|
{ |
114
|
|
|
$uri = sprintf('/api/exchanges/%s/%s', $this->getVhost(), $exchange); |
115
|
|
|
$this->connector->getConnection()->delete($uri); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
View Code Duplication |
private function declareQueue( |
|
|
|
|
119
|
|
|
string $queue, |
120
|
|
|
bool $passive = false, |
121
|
|
|
bool $durable = false, |
122
|
|
|
bool $exclusive = false, |
123
|
|
|
bool $autoDelete = true, |
124
|
|
|
bool $noWait = false, |
125
|
|
|
array $arguments = [] |
126
|
|
|
): void { |
127
|
|
|
$uri = sprintf('/api/queues/%s/%s', $this->getVhost(), $queue); |
128
|
|
|
$this->connector->getConnection()->put($uri, [ |
|
|
|
|
129
|
|
|
'body' => json_encode([ |
130
|
|
|
'passive' => $passive, |
131
|
|
|
'durable' => $durable, |
132
|
|
|
'exclusive' => $exclusive, |
133
|
|
|
'auto_delete' => $autoDelete, |
134
|
|
|
'nowait' => $noWait, |
135
|
|
|
'arguments' => $arguments |
136
|
|
|
]) |
137
|
|
|
]); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
View Code Duplication |
private function bindQueue( |
|
|
|
|
141
|
|
|
string $queue, |
142
|
|
|
string $exchange, |
143
|
|
|
string $routingKey = '', |
144
|
|
|
bool $noWait = false, |
145
|
|
|
array $arguments = [] |
146
|
|
|
): void { |
147
|
|
|
$uri = sprintf('/api/bindings/%s/e/%s/q/%s', $this->getVhost(), $exchange, $queue); |
148
|
|
|
$this->connector->getConnection()->post($uri, [ |
|
|
|
|
149
|
|
|
'body' => json_encode([ |
150
|
|
|
'routing_key' => $routingKey, |
151
|
|
|
'nowait' => $noWait, |
152
|
|
|
'arguments' => $arguments |
153
|
|
|
]) |
154
|
|
|
]); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
private function deleteQueue(string $queue): void |
158
|
|
|
{ |
159
|
|
|
$uri = sprintf('/api/queues/%s/%s', $this->getVhost(), $queue); |
160
|
|
|
$this->connector->getConnection()->delete($uri); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
private function createShovel(string $source, string $dest, string $queue): void |
164
|
|
|
{ |
165
|
|
|
$uri = sprintf('/api/parameters/shovel/%s/%s.shovel', $this->getVhost(), $source); |
166
|
|
|
$this->connector->getConnection()->put($uri, [ |
|
|
|
|
167
|
|
|
'body' => json_encode([ |
168
|
|
|
'value' => [ |
169
|
|
|
'src-uri' => 'amqp://', |
170
|
|
|
'src-queue' => $queue, |
171
|
|
|
'dest-uri' => 'amqp://', |
172
|
|
|
'dest-exchange' => $dest, |
173
|
|
|
'add-forward-headers' => false, |
174
|
|
|
'ack-mode' => 'on-confirm', |
175
|
|
|
'delete-after' => 'never' |
176
|
|
|
] |
177
|
|
|
]) |
178
|
|
|
]); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
private function deleteShovel(string $exchange): void |
182
|
|
|
{ |
183
|
|
|
$uri = sprintf('/api/parameters/shovel/%s/%s.shovel', $this->getVhost(), $exchange); |
184
|
|
|
$this->connector->getConnection()->delete($uri); |
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
private function getVhost(): string |
188
|
|
|
{ |
189
|
|
|
$connectorSettings = $this->connector->getSettings(); |
|
|
|
|
190
|
|
|
return $connectorSettings['vhost']; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|