Completed
Pull Request — master (#1)
by
unknown
38:22
created

WorkCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 2
cbo 4
dl 0
loc 58
rs 10
c 0
b 0
f 0

3 Methods

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