WorkCommand::execute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace SfCod\QueueBundle\Command;
4
5
use SfCod\QueueBundle\Worker\Options;
6
use SfCod\QueueBundle\Worker\Worker;
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\Output\OutputInterface;
11
use Symfony\Component\Console\Style\SymfonyStyle;
12
13
/**
14
 * Class WorkCommand
15
 * Job queue worker. Use pm2 (http://pm2.keymetrics.io/) for fork command.
16
 *
17
 * @author Alexey Orlov <[email protected]>
18
 *
19
 * @package SfCod\QueueBundle\Command
20
 */
21
class WorkCommand extends Command
22
{
23
    /**
24
     * @var Worker
25
     */
26
    protected $worker;
27
28
    /**
29
     * WorkCommand constructor.
30
     */
31
    public function __construct(Worker $worker)
32
    {
33
        $this->worker = $worker;
34
35
        parent::__construct();
36
    }
37
38
    /**
39
     * Configure command
40
     */
41
    protected function configure()
42
    {
43
        $this->setName('job-queue:work')
44
            ->addOption('delay', null, InputArgument::OPTIONAL, 'Delay before retry failed job.', 3)
45
            ->addOption('memory', null, InputArgument::OPTIONAL, 'Maximum memory usage limit.', 128)
46
            ->addOption('sleep', null, InputArgument::OPTIONAL, 'Sleep time before getting new job.', 3)
47
            ->addOption('maxTries', null, InputArgument::OPTIONAL, 'Max tries to run job.', 1)
48
            ->addOption('timeout', null, InputArgument::OPTIONAL, 'Daemon timeout.', 60)
49
            ->addOption('connection', null, InputArgument::OPTIONAL, 'The name of the connection.', 'default')
50
            ->addOption('queue', null, InputArgument::OPTIONAL, 'The name of the queue.', 'default')
51
            ->setDescription('Run worker.');
52
    }
53
54
    /**
55
     * Execute command
56
     *
57
     * @return int|void|null
58
     */
59
    public function execute(InputInterface $input, OutputInterface $output)
60
    {
61
        $io = new SymfonyStyle($input, $output);
62
63
        $workerOptions = new Options(
64
            $input->getOption('delay'),
65
            $input->getOption('memory'),
66
            $input->getOption('timeout'),
67
            $input->getOption('sleep'),
68
            $input->getOption('maxTries')
69
        );
70
        $connection = $input->getOption('connection');
71
        $queue = $input->getOption('queue');
72
73
        $io->success(sprintf('Worker daemon has started.'));
74
75
        $this->worker->daemon($connection, $queue, $workerOptions);
76
77
        return 0;
78
    }
79
}
80