-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGetQueuedMessagesCommand.php
More file actions
69 lines (55 loc) · 1.88 KB
/
GetQueuedMessagesCommand.php
File metadata and controls
69 lines (55 loc) · 1.88 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
<?php
declare(strict_types=1);
namespace Queue\Swoole\Command;
use Dot\DependencyInjection\Attribute\Inject;
use Redis;
use RedisException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use function count;
use function json_encode;
use function str_repeat;
use const JSON_PRETTY_PRINT;
use const JSON_UNESCAPED_UNICODE;
#[AsCommand(
name: 'inventory',
description: 'Get all queued messages from Redis stream "messages"',
)]
class GetQueuedMessagesCommand extends Command
{
/** @var string $defaultName */
protected static $defaultName = 'inventory';
private Redis $redis;
#[Inject('redis')]
public function __construct(Redis $redis)
{
parent::__construct(self::$defaultName);
$this->redis = $redis;
}
protected function configure(): void
{
$this->setDescription('Get all queued messages from Redis stream "messages"');
}
/**
* @throws RedisException
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$entries = $this->redis->xRange('messages', '-', '+');
if (empty($entries)) {
$output->writeln('<info>No messages queued found in Redis stream "messages".</info>');
return Command::SUCCESS;
}
foreach ($entries as $id => $entry) {
$output->writeln("<info>Message ID:</info> $id");
$output->writeln(json_encode($entry, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
$output->writeln(str_repeat('-', 40));
}
$total = count($entries);
$output->writeln("<info>Total queued messages in stream 'messages':</info> $total");
$output->writeln(str_repeat('-', 40));
return Command::SUCCESS;
}
}