Passed
Pull Request — master (#167)
by Alexander
09:35 queued 06:35
created

ListenAllCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 38
c 2
b 0
f 0
dl 0
loc 66
ccs 0
cts 38
cp 0
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 25 1
A __construct() 0 6 1
A execute() 0 20 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Queue\Command;
6
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Yiisoft\Yii\Console\ExitCode;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Yii\Console\ExitCode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Yiisoft\Yii\Queue\Cli\LoopInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Yii\Queue\Cli\LoopInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Yiisoft\Yii\Queue\QueueFactoryInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Yii\Queue\QueueFactoryInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
final class ListenAllCommand extends Command
17
{
18
    protected static $defaultName = 'queue:listen-all';
19
    protected static $defaultDescription = 'Listens the all the given queues and executes messages as they come. ' .
20
    'Meant to be used in development environment only. ' .
21
    'Listens all configured queues by default in case you\'re using yiisoft/config. ' .
22
    'Needs to be stopped manually.';
23
24
    private QueueFactoryInterface $queueFactory;
25
    private LoopInterface $loop;
26
27
    public function __construct(QueueFactoryInterface $queueFactory, LoopInterface $loop, private array $channels)
28
    {
29
        parent::__construct();
30
31
        $this->queueFactory = $queueFactory;
32
        $this->loop = $loop;
33
    }
34
35
    public function configure(): void
36
    {
37
        $this->addArgument(
38
            'channel',
39
            InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
40
            'Queue channel name list to connect to',
41
            $this->channels,
42
        )
43
            ->addOption(
44
                'pause',
45
                'p',
46
                InputOption::VALUE_REQUIRED,
47
                'Pause between queue channel iterations in seconds. May save some CPU. Default: 1',
48
                1,
49
            )
50
            ->addOption(
51
                'maximum',
52
                'm',
53
                InputOption::VALUE_REQUIRED,
54
                'Maximum number of messages to process in each channel before switching to another channel. ' .
55
                   'Default is 0 (no limits).',
56
                0,
57
            );
58
59
        $this->addUsage('[channel1 [channel2 [...]]] [--timeout=<timeout>] [--maximum=<maximum>]');
60
    }
61
62
    protected function execute(InputInterface $input, OutputInterface $output): int
63
    {
64
        $queues = [];
65
        /** @var string $channel */
66
        foreach ($input->getArgument('channel') as $channel) {
67
            $queues[] = $this->queueFactory->get($channel);
68
        }
69
70
        while ($this->loop->canContinue()) {
71
            $hasMessages = false;
72
            foreach ($queues as $queue) {
73
                $hasMessages = $queue->run((int)$input->getOption('maximum')) > 0 || $hasMessages;
74
            }
75
76
            if (!$hasMessages) {
77
                sleep((int)$input->getOption('pause'));
78
            }
79
        }
80
81
        return ExitCode::OK;
82
    }
83
}
84