1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WillRy\RabbitRun; |
4
|
|
|
|
5
|
|
|
use PhpAmqpLib\Connection\AMQPStreamConnection; |
6
|
|
|
use PhpAmqpLib\Exception\AMQPIOException; |
7
|
|
|
use PhpAmqpLib\Exception\AMQPRuntimeException; |
8
|
|
|
use PhpAmqpLib\Exception\AMQPTimeoutException; |
9
|
|
|
use WillRy\RabbitRun\Connections\Connect; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class Base |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** @var AMQPStreamConnection Instância de conexão */ |
17
|
|
|
protected $instance; |
18
|
|
|
|
19
|
|
|
/** @var \PhpAmqpLib\Channel\AMQPChannel Canal de comunicação */ |
20
|
|
|
protected $channel; |
21
|
|
|
|
22
|
|
|
/** @var \PDO Conexão do PDO */ |
23
|
|
|
protected $db; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Graceful shutdown |
31
|
|
|
* Faz a execucao parar ao enviar um sinal do linux para matar o script |
32
|
|
|
*/ |
33
|
|
|
if (php_sapi_name() == "cli") { |
34
|
|
|
\pcntl_signal(SIGTERM, function ($signal) { |
35
|
|
|
$this->shutdown($signal); |
36
|
|
|
}, false); |
37
|
|
|
\pcntl_signal(SIGINT, function ($signal) { |
38
|
|
|
$this->shutdown($signal); |
39
|
|
|
}, false); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Garante o desligamento correto dos workers |
47
|
|
|
* via sinal no sistema operacional |
48
|
|
|
* eliminando loops e conexões |
49
|
|
|
* @param $signal |
50
|
|
|
*/ |
51
|
|
|
public function shutdown($signal) |
52
|
|
|
{ |
53
|
|
|
$data = date('Y-m-d H:i:s'); |
54
|
|
|
switch ($signal) { |
55
|
|
|
case SIGTERM: |
56
|
|
|
print "Caught SIGTERM {$data}" . PHP_EOL; |
57
|
|
|
exit; |
|
|
|
|
58
|
|
|
case SIGKILL: |
59
|
|
|
print "Caught SIGKILL {$data}" . PHP_EOL;; |
60
|
|
|
exit; |
|
|
|
|
61
|
|
|
case SIGINT: |
62
|
|
|
print "Caught SIGINT {$data}" . PHP_EOL;; |
63
|
|
|
exit; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Configura o rabbitmq e gera conexão ativa |
69
|
|
|
* @param $host |
70
|
|
|
* @param $port |
71
|
|
|
* @param $user |
72
|
|
|
* @param $pass |
73
|
|
|
* @param $vhost |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
|
public function configRabbit($host, $port, $user, $pass, $vhost): Base |
77
|
|
|
{ |
78
|
|
|
Connect::config($host, $port, $user, $pass, $vhost); |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Gera uma conexão no rabbitmq e gera um canal(opcional) |
85
|
|
|
* @param bool $createChannel |
86
|
|
|
* @return AMQPStreamConnection|void |
87
|
|
|
*/ |
88
|
|
|
public function getConnection(bool $createChannel = true) |
89
|
|
|
{ |
90
|
|
|
$this->instance = Connect::getInstance(); |
91
|
|
|
|
92
|
|
|
if ($createChannel) { |
93
|
|
|
$this->getChannel(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $this->instance; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Retorna canal ativo ou cria um novo |
101
|
|
|
* @return \PhpAmqpLib\Channel\AMQPChannel|void |
102
|
|
|
*/ |
103
|
|
|
public function getChannel() |
104
|
|
|
{ |
105
|
|
|
$this->channel = Connect::getChannel(); |
106
|
|
|
return $this->channel; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Fecha a conexão com o RabbitMQ |
112
|
|
|
* @throws \Exception |
113
|
|
|
*/ |
114
|
|
|
public function cleanConnection() |
115
|
|
|
{ |
116
|
|
|
try { |
117
|
|
|
Connect::closeChannel(); |
118
|
|
|
Connect::closeInstance(); |
119
|
|
|
} catch (\Exception $e) { |
120
|
|
|
echo '[ERROR CLOSE CHANNEL|INSTANCE]' . $e->getMessage() . "|file:" . $e->getFile() . "|line:" . $e->getLine() . PHP_EOL; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Cria uma exchange |
126
|
|
|
* |
127
|
|
|
* @param string $exchange |
128
|
|
|
* @param string $type |
129
|
|
|
* @param bool $passive |
130
|
|
|
* @param bool $durable |
131
|
|
|
* @param bool $auto_delete |
132
|
|
|
* @return $this |
133
|
|
|
*/ |
134
|
|
|
public function exchange( |
135
|
|
|
string $exchange, |
136
|
|
|
string $type = "direct", |
137
|
|
|
bool $passive = false, |
138
|
|
|
bool $durable = true, |
139
|
|
|
bool $auto_delete = false |
140
|
|
|
) { |
141
|
|
|
$this->channel->exchange_declare( |
142
|
|
|
$exchange, |
143
|
|
|
$type, |
144
|
|
|
$passive, |
145
|
|
|
$durable, |
146
|
|
|
$auto_delete, |
147
|
|
|
); |
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Cria uma fila |
153
|
|
|
* |
154
|
|
|
* @param string $queue |
155
|
|
|
* @param false $passive |
156
|
|
|
* @param false $durable |
157
|
|
|
* @param false $exclusive |
158
|
|
|
* @param bool $auto_delete |
159
|
|
|
* @return array|null |
160
|
|
|
*/ |
161
|
|
|
public function queue( |
162
|
|
|
string $queue = '', |
163
|
|
|
bool $passive = false, |
164
|
|
|
bool $durable = true, |
165
|
|
|
bool $exclusive = false, |
166
|
|
|
bool $auto_delete = false |
167
|
|
|
) { |
168
|
|
|
return $this->channel->queue_declare( |
169
|
|
|
$queue, |
170
|
|
|
$passive, |
171
|
|
|
$durable, |
172
|
|
|
$exclusive, |
173
|
|
|
$auto_delete, |
174
|
|
|
false |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Vincula a fila e a exchange criada |
180
|
|
|
* @return $this |
181
|
|
|
*/ |
182
|
|
|
public function bind( |
183
|
|
|
$queue, |
184
|
|
|
$exchange |
185
|
|
|
) { |
186
|
|
|
$this->channel->queue_bind($queue, $exchange); |
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function randomConsumer($len = 30): string |
191
|
|
|
{ |
192
|
|
|
$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
193
|
|
|
return substr(str_shuffle(str_repeat($pool, (int)ceil($len / strlen($pool)))), 0, $len); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Mantém a conexão, mesmo em caso de erro |
198
|
|
|
* de rede ou conexão |
199
|
|
|
* @param $callback |
200
|
|
|
* @throws \Exception |
201
|
|
|
*/ |
202
|
|
|
public function loopConnection($callback) |
203
|
|
|
{ |
204
|
|
|
while (true) { |
205
|
|
|
try { |
206
|
|
|
$this->getConnection(true); |
207
|
|
|
$callback(); |
208
|
|
|
} catch (\Exception $e) { |
209
|
|
|
echo get_class($e).':' . $e->getMessage() . " | file:" . $e->getFile() . " | line:" . $e->getLine() . PHP_EOL; |
210
|
|
|
$this->cleanConnection(); |
211
|
|
|
sleep(2); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.