This commit is contained in:
Anton Medvedev 2020-07-26 12:07:22 +03:00
parent 1b816a45f5
commit 728bf55165
4 changed files with 43 additions and 56 deletions

View File

@ -114,7 +114,7 @@ class MainCommand extends SelectCommand
throw new Exception('No task will be executed, because the selected hosts do not meet the conditions of the tasks');
}
$exitCode = $this->deployer->executor->run($tasks, $hosts, $plan);
$exitCode = $this->deployer->master->run($tasks, $hosts, $plan);
if ($plan) {
$plan->render();
@ -132,7 +132,7 @@ class MainCommand extends SelectCommand
if ($this->deployer['fail']->has($this->getName())) {
$taskName = $this->deployer['fail']->get($this->getName());
$tasks = $this->deployer->scriptManager->getTasks($taskName);
$this->deployer->executor->run($tasks, $hosts);
$this->deployer->master->run($tasks, $hosts);
}
return $exitCode;

View File

@ -56,7 +56,7 @@ use Throwable;
* @property ProcessRunner $processRunner
* @property Task\ScriptManager $scriptManager
* @property Selector $selector
* @property Master $executor
* @property Master $master
* @property Messenger $messenger
* @property Messenger $logger
* @property Printer $pop
@ -145,7 +145,7 @@ class Deployer extends Container
$this['messenger'] = function ($c) {
return new Messenger($c['input'], $c['output']);
};
$this['executor'] = function ($c) {
$this['master'] = function ($c) {
return new Master(
$c['input'],
$c['output'],

View File

@ -9,7 +9,6 @@ namespace Deployer\Executor;
use Deployer\Component\Ssh\Client;
use Deployer\Configuration\Configuration;
use Deployer\Console\WorkerCommand;
use Deployer\Deployer;
use Deployer\Host\Host;
use Deployer\Host\Localhost;
@ -50,37 +49,6 @@ class Master
$this->config = $config;
}
/**
* @param Host[] $hosts
*/
private function connect(array $hosts)
{
$callback = function (string $output) {
$output = preg_replace('/\n$/', '', $output);
if (strlen($output) !== 0) {
$this->output->writeln($output);
}
};
// Connect to each host sequentially, to prevent getting locked.
foreach ($hosts as $host) {
if ($host instanceof Localhost) {
continue;
}
$process = $this->getProcess($host, new Task('connect'));
$process->start();
while ($process->isRunning()) {
$this->gatherOutput([$process], $callback);
$this->output->write(spinner(str_pad("connect {$host->getTag()}", intval(getenv('COLUMNS')) - 1)));
usleep(1000);
}
}
// Clear spinner.
$this->output->write(str_repeat(' ', intval(getenv('COLUMNS')) - 1) . "\r");
}
/**
* @param Task[] $tasks
* @param Host[] $hosts
@ -164,6 +132,37 @@ class Master
return 0;
}
/**
* @param Host[] $hosts
*/
private function connect(array $hosts)
{
$callback = function (string $output) {
$output = preg_replace('/\n$/', '', $output);
if (strlen($output) !== 0) {
$this->output->writeln($output);
}
};
// Connect to each host sequentially, to prevent getting locked.
foreach ($hosts as $host) {
if ($host instanceof Localhost) {
continue;
}
$process = $this->getProcess($host, new Task('connect'));
$process->start();
while ($process->isRunning()) {
$this->gatherOutput([$process], $callback);
$this->output->write(spinner(str_pad("connect {$host->getTag()}", intval(getenv('COLUMNS')) - 1)));
usleep(1000);
}
}
// Clear spinner.
$this->output->write(str_repeat(' ', intval(getenv('COLUMNS')) - 1) . "\r");
}
/**
* @param Task $task
* @param Host[] $hosts
@ -199,7 +198,9 @@ class Master
}
};
$this->startProcesses($processes);
foreach ($processes as $process) {
$process->start();
}
while ($this->areRunning($processes)) {
$this->gatherOutput($processes, $callback);
@ -207,11 +208,8 @@ class Master
usleep(1000);
}
// Clear spinner.
$this->output->write(" \r");
$this->output->write(" \r"); // clear spinner
$this->gatherOutput($processes, $callback);
return $this->cumulativeExitCode($processes);
}
@ -231,16 +229,7 @@ class Master
/**
* @param Process[] $processes
*/
protected function startProcesses(array $processes)
{
foreach ($processes as $process) {
$process->start();
}
}
/**
* @param Process[] $processes
* @return bool
*/
protected function areRunning(array $processes): bool
{
@ -249,12 +238,12 @@ class Master
return true;
}
}
return false;
}
/**
* @param Process[] $processes
* @param callable $callback
*/
protected function gatherOutput(array $processes, callable $callback)
{
@ -272,7 +261,6 @@ class Master
}
/**
* Gather the cumulative exit code for the processes.
* @param Process[] $processes
* @return int
*/
@ -283,7 +271,6 @@ class Master
return $process->getExitCode();
}
}
return 0;
}
}

View File

@ -430,8 +430,8 @@ function invoke($task)
$hosts = [Context::get()->getHost()];
$tasks = Deployer::get()->scriptManager->getTasks($task, $hosts);
$executor = Deployer::get()->executor;
$executor->run($tasks, $hosts);
$master = Deployer::get()->master;
$master->run($tasks, $hosts);
}
/*