WorkerCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 90
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 34 2
A execute() 0 15 4
A doExecute() 0 20 3
1
<?php
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Console;
9
10
use Deployer\Deployer;
11
use Deployer\Exception\GracefulShutdownException;
12
use Deployer\Exception\NonFatalException;
13
use Deployer\Host\Host;
14
use Deployer\Host\Storage;
15
use Deployer\Task\Context;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Input\InputOption;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
/**
23
 * @codeCoverageIgnore
24
 */
25
class WorkerCommand extends Command
26
{
27
    /**
28
     * @var Deployer
29
     */
30
    private $deployer;
31
32
    /**
33
     * @var Host
34
     */
35
    private $host;
36
37
    /**
38
     * @param Deployer $deployer
39
     */
40
    public function __construct(Deployer $deployer)
41
    {
42
        parent::__construct('worker');
43
        $this->setDescription('Deployer uses workers for parallel deployment');
44
        if (method_exists($this, 'setHidden')) {
45
            $this->setHidden(true);
46
        }
47
        $this->deployer = $deployer;
48
        $this->addArgument(
49
            'stage',
50
            InputArgument::OPTIONAL,
51
            'Stage or hostname'
52
        );
53
        $this->addOption(
54
            'hostname',
55
            null,
56
            InputOption::VALUE_REQUIRED
57
        );
58
        $this->addOption(
59
            'task',
60
            null,
61
            InputOption::VALUE_REQUIRED
62
        );
63
        $this->addOption(
64
            'config-file',
65
            null,
66
            InputOption::VALUE_REQUIRED
67
        );
68
        $this->addOption(
69
            'log',
70
            null,
71
            InputOption::VALUE_REQUIRED
72
        );
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    protected function execute(InputInterface $input, OutputInterface $output)
79
    {
80
        try {
81
            $this->doExecute($input, $output);
82
        } catch (GracefulShutdownException $e) {
83
            $this->deployer->informer->taskException($e, $this->host);
84
            return 1;
85
        } catch (NonFatalException $e) {
86
            $this->deployer->informer->taskException($e, $this->host);
87
            return 2;
88
        } catch (\Throwable $e) {
89
            $this->deployer->informer->taskException($e, $this->host);
90
            return 255;
91
        }
92
    }
93
94
    private function doExecute(InputInterface $input, OutputInterface $output)
95
    {
96
        $hostname = $input->getOption('hostname');
97
        $host = $this->host = $this->deployer->hosts->get($hostname);
98
99
        Storage::setup($host, $input->getOption('config-file'));
100
101
        $task = $input->getOption('task');
102
        $task = $this->deployer->tasks->get($task);
103
        if (!empty($input->getOption('log'))) {
104
            $this->deployer->config['log_file'] = $input->getOption('log');
105
        }
106
107
        if ($task->shouldBePerformed($host)) {
108
            $task->run(new Context($host, $input, $output));
109
            $this->deployer->informer->endOnHost($hostname);
110
        }
111
112
        Storage::flush($host);
113
    }
114
}
115