From ea273fbb9dcc6e9bef5e43e1ee808ea45d15f310 Mon Sep 17 00:00:00 2001 From: Pavel Buchnev Date: Tue, 28 Mar 2023 13:51:32 +0400 Subject: [PATCH] Updates style of roadrunner jobs list command. (#69) --- src/Console/Command/Queue/ListCommand.php | 64 ++++++++++++++----- .../Console/Command/Queue/ListCommandTest.php | 8 +-- 2 files changed, 53 insertions(+), 19 deletions(-) diff --git a/src/Console/Command/Queue/ListCommand.php b/src/Console/Command/Queue/ListCommand.php index d2b2926..f80f204 100644 --- a/src/Console/Command/Queue/ListCommand.php +++ b/src/Console/Command/Queue/ListCommand.php @@ -7,7 +7,10 @@ use Spiral\Console\Command; use Spiral\RoadRunner\Jobs\JobsInterface; use Spiral\RoadRunner\Jobs\Queue; +use Spiral\RoadRunner\Jobs\QueueInterface; use Symfony\Component\Console\Helper\Table; +use Symfony\Component\Console\Helper\TableCell; +use Symfony\Component\Console\Helper\TableCellStyle; final class ListCommand extends Command { @@ -24,26 +27,57 @@ public function perform(JobsInterface $jobs): int return self::SUCCESS; } + $queues = \array_map(static function (QueueInterface $queue): array { + \assert($queue instanceof Queue); + $stat = $queue->getPipelineStat(); + + $fontColor = $stat->getReady() ? 'green' : 'gray'; + $defaultColor = $stat->getReady() ? 'default' : 'gray'; + $activeFont = $stat->getReady() ? 'bold' : ''; + + return [ + 'name' => new TableCell($stat->getPipeline(), [ + 'style' => new TableCellStyle(['fg' => $fontColor, 'options' => $activeFont]), + ]), + 'driver' => new TableCell($stat->getDriver(), [ + 'style' => new TableCellStyle( + ['fg' => $defaultColor, 'options' => $activeFont] + ), + ]), + 'priority' => new TableCell((string)$stat->getPriority(), [ + 'style' => new TableCellStyle( + ['fg' => $defaultColor, 'options' => $activeFont] + ), + ]), + 'active_jobs' => new TableCell((string)$stat->getActive(), [ + 'style' => new TableCellStyle( + ['fg' => $stat->getActive() > 0 ? 'green' : $defaultColor, 'options' => $activeFont] + ), + ]), + 'delayed_jobs' => new TableCell((string)$stat->getDelayed(), [ + 'style' => new TableCellStyle( + ['fg' => $stat->getDelayed() > 0 ? 'green' : $defaultColor, 'options' => $activeFont] + ), + ]), + 'reserved_jobs' => new TableCell((string)$stat->getReserved(), [ + 'style' => new TableCellStyle( + ['fg' => $stat->getReserved() > 0 ? 'green' : $defaultColor, 'options' => $activeFont] + ), + ]), + 'is_active' => $stat->getReady() ? ' ✓ ' : ' ✖ ', + ]; + }, $queues); + + \ksort($queues); + $table = new Table($this->output); $table->setHeaders( - ['Name', 'Driver', 'Priority', 'Active jobs', 'Delayed jobs', 'Reserved jobs', 'Is active'], + ['Name', 'Driver', 'Priority', 'Active jobs', 'Delayed jobs', 'Reserved jobs', 'Is active',], ); - foreach ($queues as $queue) { - /** @var Queue $queue */ - - $stat = $queue->getPipelineStat(); - - $table->addRow([ - $stat->getPipeline(), - $stat->getDriver(), - $stat->getPriority(), - $stat->getActive(), - $stat->getDelayed(), - $stat->getReserved(), - $stat->getReady() ? ' ✓ ' : ' ✖ ', - ]); + foreach ($queues as $data) { + $table->addRow($data); } $table->render(); diff --git a/tests/src/Console/Command/Queue/ListCommandTest.php b/tests/src/Console/Command/Queue/ListCommandTest.php index bc32e3a..bb3bfdd 100644 --- a/tests/src/Console/Command/Queue/ListCommandTest.php +++ b/tests/src/Console/Command/Queue/ListCommandTest.php @@ -16,14 +16,14 @@ public function testGetsListOfAvailablePipelines() $jobs = \Mockery::mock(JobsInterface::class); $this->getContainer()->bind(JobsInterface::class, $jobs); - $jobs->shouldReceive('getIterator')->once()->andReturn( + $jobs->shouldReceive('getIterator')->andReturn( new \ArrayIterator([ - 'memory' => $memory = \Mockery::mock(QueueInterface::class), + 'memory' => $queue = \Mockery::mock(QueueInterface::class), 'amqp' => $amqp = \Mockery::mock(QueueInterface::class), ]) ); - $memory->shouldReceive('getPipelineStat')->once()->andReturn( + $queue->shouldReceive('getPipelineStat')->once()->andReturn( new Stat([ 'pipeline' => 'test', 'driver' => 'memory', @@ -54,8 +54,8 @@ public function testGetsListOfAvailablePipelines() +---------+--------+----------+-------------+--------------+---------------+-----------+ | Name | Driver | Priority | Active jobs | Delayed jobs | Reserved jobs | Is active | +---------+--------+----------+-------------+--------------+---------------+-----------+ -| test | memory | 200 | 100 | 55 | 8 | ✓ | | default | amqp | 250 | 110 | 88 | 56 | ✖ | +| test | memory | 200 | 100 | 55 | 8 | ✓ | +---------+--------+----------+-------------+--------------+---------------+-----------+ EOL, $this->runCommand('rr:jobs:list')