Passed
Pull Request — master (#167)
by Viktor
12:32 queued 27s
created

ListenAllCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 68
ccs 0
cts 39
cp 0
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 25 1
A __construct() 0 7 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
    private array $channels;
27
28
    public function __construct(QueueFactoryInterface $queueFactory, LoopInterface $loop, array $channels)
29
    {
30
        parent::__construct();
31
32
        $this->queueFactory = $queueFactory;
33
        $this->loop = $loop;
34
        $this->channels = $channels;
35
    }
36
37
    public function configure(): void
38
    {
39
        $this->addArgument(
40
            'channel',
41
            InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
42
            'Queue channel name list to connect to',
43
            $this->channels,
44
        )
45
            ->addOption(
46
                'pause',
47
                'p',
48
                InputOption::VALUE_REQUIRED,
49
                'Pause between queue channel iterations in seconds. May save some CPU. Default: 1',
50
                1,
51
            )
52
            ->addOption(
53
                'maximum',
54
                'm',
55
                InputOption::VALUE_REQUIRED,
56
                'Maximum number of messages to process in each channel before switching to another channel. ' .
57
                   'Default is 0 (no limits).',
58
                0,
59
            );
60
61
        $this->addUsage('[channel1 [channel2 [...]]] [--timeout=<timeout>] [--maximum=<maximum>]');
62
    }
63
64
    protected function execute(InputInterface $input, OutputInterface $output): int
65
    {
66
        $queues = [];
67
        /** @var string $channel */
68
        foreach ($input->getArgument('channel') as $channel) {
69
            $queues[] = $this->queueFactory->get($channel);
70
        }
71
72
        while ($this->loop->canContinue()) {
73
            $hasMessages = false;
74
            foreach ($queues as $queue) {
75
                $hasMessages = $queue->run((int)$input->getOption('maximum')) > 0 || $hasMessages;
76
            }
77
78
            if (!$hasMessages) {
79
                sleep((int)$input->getOption('pause'));
80
            }
81
        }
82
83
        return ExitCode::OK;
84
    }
85
}
86