Skip to content

Commit b9a5628

Browse files
committed
Updated example with a cleaner and clearer output
1 parent 3fc56d9 commit b9a5628

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

examples/queues-subscribe-rates-async.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,43 @@
1111
$loop = Factory::create();
1212
$config = require 'resolve_config.php';
1313

14+
$height = 0;
15+
$length = 0;
16+
$queues = [];
17+
1418
$client = AsyncClient::create($loop, $config['baseUrl'], $config['username'], $config['password']);
15-
$queues = $client->queues(1)->filter(function (QueueInterface $queue) {
19+
$client->queues(1)->filter(function (QueueInterface $queue) {
1620
return strpos($queue->name(), 'amq.gen') !== 0;
17-
})->subscribe(new CallbackObserver(function (QueueInterface $queue) {
18-
echo $queue->name(), ': ', $queue->messageStats()->deliverDetails()->rate(), PHP_EOL;
21+
})->subscribe(new CallbackObserver(function (QueueInterface $queue) use (&$queues) {
22+
$queues[$queue->name()] = $queue->messageStats()->deliverDetails()->rate();
1923
}));
2024

25+
$loop->addPeriodicTimer(1, function () use (&$height, &$length, &$queues) {
26+
for ($i = 0; $i < $height; $i++) {
27+
echo "\033[1A";
28+
}
29+
30+
if ($height < count($queues)) {
31+
$height = count($queues);
32+
}
33+
34+
ksort($queues);
35+
36+
$i = 0;
37+
foreach ($queues as $key => $value) {
38+
$line = $key . ': ' . $value . ' sec';
39+
if ($length < strlen($line)) {
40+
$length = strlen($line);
41+
}
42+
$line = str_pad($line, $length, ' ');
43+
echo $line, PHP_EOL;
44+
$i++;
45+
}
46+
for (; $i < $height; $i++) {
47+
echo "\r";
48+
}
49+
50+
$queues = [];
51+
});
52+
2153
$loop->run();

0 commit comments

Comments
 (0)