/
queues-subscribe-rates-async.php
51 lines (40 loc) · 1.32 KB
/
queues-subscribe-rates-async.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
<?php declare(strict_types=1);
use ApiClients\Client\RabbitMQ\Management\AsyncClient;
use ApiClients\Client\RabbitMQ\Management\Resource\QueueInterface;
use React\EventLoop\Factory;
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';
$loop = Factory::create();
$config = require 'resolve_config.php';
$height = 0;
$length = 0;
$queues = [];
$client = AsyncClient::create($loop, $config['baseUrl'], $config['username'], $config['password']);
$client->queues(1)->filter(function (QueueInterface $queue) {
return strpos($queue->name(), 'amq.gen') !== 0;
})->subscribe(function (QueueInterface $queue) use (&$queues) {
$queues[$queue->name()] = $queue->messageStats()->deliverDetails()->rate();
});
$loop->addPeriodicTimer(1, function () use (&$height, &$length, &$queues) {
for ($i = 0; $i < $height; $i++) {
echo "\033[1A";
}
if ($height < count($queues)) {
$height = count($queues);
}
ksort($queues);
$i = 0;
foreach ($queues as $key => $value) {
$line = $key . ': ' . $value . ' sec';
if ($length < strlen($line)) {
$length = strlen($line);
}
$line = str_pad($line, $length, ' ');
echo $line, PHP_EOL;
$i++;
}
for (; $i < $height; $i++) {
echo "\r";
}
$queues = [];
});
$loop->run();