|
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\Console\Output\RemoteOutput; |
|
11
|
|
|
use Deployer\Deployer; |
|
12
|
|
|
use Deployer\Server\Environment; |
|
13
|
|
|
use Deployer\Task\Context; |
|
14
|
|
|
use Deployer\Task\NonFatalException; |
|
15
|
|
|
use Pure\Client; |
|
16
|
|
|
use Symfony\Component\Console\Command\Command; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
18
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
20
|
|
|
|
|
21
|
|
|
class WorkerCommand extends Command |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var Deployer |
|
25
|
|
|
*/ |
|
26
|
|
|
private $deployer; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param Deployer $deployer |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(Deployer $deployer) |
|
32
|
|
|
{ |
|
33
|
|
|
parent::__construct('worker'); |
|
34
|
|
|
$this->setDescription('Deployer uses workers for parallel deployment.'); |
|
35
|
|
|
|
|
36
|
|
|
$this->deployer = $deployer; |
|
37
|
|
|
|
|
38
|
|
|
$this->addOption( |
|
39
|
|
|
'master', |
|
40
|
|
|
null, |
|
41
|
|
|
InputOption::VALUE_REQUIRED |
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
|
|
$this->addOption( |
|
45
|
|
|
'server', |
|
46
|
|
|
null, |
|
47
|
|
|
InputOption::VALUE_REQUIRED |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
55
|
|
|
{ |
|
56
|
|
|
$serverName = $input->getOption('server'); |
|
57
|
|
|
list($host, $port) = explode(':', $input->getOption('master')); |
|
58
|
|
|
$pure = new Client($port, $host); |
|
59
|
|
|
|
|
60
|
|
|
try { |
|
61
|
|
|
$server = $this->deployer->servers->get($serverName); |
|
62
|
|
|
$environment = isset($this->deployer->environments[$serverName]) ? $this->deployer->environments[$serverName] : new Environment(); |
|
63
|
|
|
$output = new RemoteOutput($output, $pure, $serverName); |
|
64
|
|
|
|
|
65
|
|
|
while ($pure->ping()) { |
|
66
|
|
|
// Get task to do |
|
67
|
|
|
$taskName = $pure->map('tasks_to_do')->get($serverName); |
|
68
|
|
|
|
|
69
|
|
|
if (null !== $taskName) { |
|
70
|
|
|
$task = $this->deployer->tasks->get($taskName); |
|
71
|
|
|
|
|
72
|
|
|
try { |
|
73
|
|
|
$task->run(new Context($server, $environment, $input, $output)); |
|
74
|
|
|
} catch (NonFatalException $e) { |
|
75
|
|
|
$pure->queue('exception')->push([$serverName, get_class($e), $e->getMessage()]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$pure->map('tasks_to_do')->delete($serverName); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} catch (\Exception $exception) { |
|
82
|
|
|
$pure->queue('exception')->push([$serverName, get_class($exception), $exception->getMessage()]); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|