|
|
|
@ -16,18 +16,32 @@ use Composer\IO\IOInterface;
|
|
|
|
|
use Symfony\Component\Process\Process;
|
|
|
|
|
use Symfony\Component\Process\ProcessUtils;
|
|
|
|
|
use Symfony\Component\Process\Exception\RuntimeException;
|
|
|
|
|
use React\Promise\Promise;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author Robert Schönthal <seroscho@googlemail.com>
|
|
|
|
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
|
*/
|
|
|
|
|
class ProcessExecutor
|
|
|
|
|
{
|
|
|
|
|
const STATUS_QUEUED = 1;
|
|
|
|
|
const STATUS_STARTED = 2;
|
|
|
|
|
const STATUS_COMPLETED = 3;
|
|
|
|
|
const STATUS_FAILED = 4;
|
|
|
|
|
const STATUS_ABORTED = 5;
|
|
|
|
|
|
|
|
|
|
protected static $timeout = 300;
|
|
|
|
|
|
|
|
|
|
protected $captureOutput;
|
|
|
|
|
protected $errorOutput;
|
|
|
|
|
protected $io;
|
|
|
|
|
|
|
|
|
|
private $jobs = array();
|
|
|
|
|
private $runningJobs = 0;
|
|
|
|
|
private $maxJobs = 10;
|
|
|
|
|
private $idGen = 0;
|
|
|
|
|
private $allowAsync = false;
|
|
|
|
|
|
|
|
|
|
public function __construct(IOInterface $io = null)
|
|
|
|
|
{
|
|
|
|
|
$this->io = $io;
|
|
|
|
@ -112,6 +126,196 @@ class ProcessExecutor
|
|
|
|
|
return $process->getExitCode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* starts a process on the commandline in async mode
|
|
|
|
|
*
|
|
|
|
|
* @param string $command the command to execute
|
|
|
|
|
* @param mixed $output the output will be written into this var if passed by ref
|
|
|
|
|
* if a callable is passed it will be used as output handler
|
|
|
|
|
* @param string $cwd the working directory
|
|
|
|
|
* @return int statuscode
|
|
|
|
|
*/
|
|
|
|
|
public function executeAsync($command, $cwd = null)
|
|
|
|
|
{
|
|
|
|
|
if (!$this->allowAsync) {
|
|
|
|
|
throw new \LogicException('You must use the ProcessExecutor instance which is part of a Composer\Loop instance to be able to run async processes');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$job = array(
|
|
|
|
|
'id' => $this->idGen++,
|
|
|
|
|
'status' => self::STATUS_QUEUED,
|
|
|
|
|
'command' => $command,
|
|
|
|
|
'cwd' => $cwd,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$resolver = function ($resolve, $reject) use (&$job) {
|
|
|
|
|
$job['status'] = ProcessExecutor::STATUS_QUEUED;
|
|
|
|
|
$job['resolve'] = $resolve;
|
|
|
|
|
$job['reject'] = $reject;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$self = $this;
|
|
|
|
|
$io = $this->io;
|
|
|
|
|
|
|
|
|
|
$canceler = function () use (&$job) {
|
|
|
|
|
if ($job['status'] === self::STATUS_QUEUED) {
|
|
|
|
|
$job['status'] = self::STATUS_ABORTED;
|
|
|
|
|
}
|
|
|
|
|
if ($job['status'] !== self::STATUS_STARTED) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$job['status'] = self::STATUS_ABORTED;
|
|
|
|
|
try {
|
|
|
|
|
if (defined('SIGINT')) {
|
|
|
|
|
$job['process']->signal(SIGINT);
|
|
|
|
|
}
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
// signal can throw in various conditions, but we don't care if it fails
|
|
|
|
|
}
|
|
|
|
|
$job['process']->stop(1);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$promise = new Promise($resolver, $canceler);
|
|
|
|
|
$promise = $promise->then(function () use (&$job, $self) {
|
|
|
|
|
if ($job['process']->isSuccessful()) {
|
|
|
|
|
$job['status'] = ProcessExecutor::STATUS_COMPLETED;
|
|
|
|
|
} else {
|
|
|
|
|
$job['status'] = ProcessExecutor::STATUS_FAILED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO 3.0 this should be done directly on $this when PHP 5.3 is dropped
|
|
|
|
|
$self->markJobDone();
|
|
|
|
|
|
|
|
|
|
return $job['process'];
|
|
|
|
|
}, function () use (&$job, $self) {
|
|
|
|
|
$job['status'] = ProcessExecutor::STATUS_FAILED;
|
|
|
|
|
|
|
|
|
|
$self->markJobDone();
|
|
|
|
|
|
|
|
|
|
return \React\Promise\reject($job['process']);
|
|
|
|
|
});
|
|
|
|
|
$this->jobs[$job['id']] =& $job;
|
|
|
|
|
|
|
|
|
|
if ($this->runningJobs < $this->maxJobs) {
|
|
|
|
|
$this->startJob($job['id']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $promise;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function startJob($id)
|
|
|
|
|
{
|
|
|
|
|
$job =& $this->jobs[$id];
|
|
|
|
|
if ($job['status'] !== self::STATUS_QUEUED) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// start job
|
|
|
|
|
$job['status'] = self::STATUS_STARTED;
|
|
|
|
|
$this->runningJobs++;
|
|
|
|
|
|
|
|
|
|
$command = $job['command'];
|
|
|
|
|
$cwd = $job['cwd'];
|
|
|
|
|
|
|
|
|
|
if ($this->io && $this->io->isDebug()) {
|
|
|
|
|
$safeCommand = preg_replace_callback('{://(?P<user>[^:/\s]+):(?P<password>[^@\s/]+)@}i', function ($m) {
|
|
|
|
|
if (preg_match('{^[a-f0-9]{12,}$}', $m['user'])) {
|
|
|
|
|
return '://***:***@';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return '://'.$m['user'].':***@';
|
|
|
|
|
}, $command);
|
|
|
|
|
$safeCommand = preg_replace("{--password (.*[^\\\\]\') }", '--password \'***\' ', $safeCommand);
|
|
|
|
|
$this->io->writeError('Executing async command ('.($cwd ?: 'CWD').'): '.$safeCommand);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// make sure that null translate to the proper directory in case the dir is a symlink
|
|
|
|
|
// and we call a git command, because msysgit does not handle symlinks properly
|
|
|
|
|
if (null === $cwd && Platform::isWindows() && false !== strpos($command, 'git') && getcwd()) {
|
|
|
|
|
$cwd = realpath(getcwd());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO in v3, commands should be passed in as arrays of cmd + args
|
|
|
|
|
if (method_exists('Symfony\Component\Process\Process', 'fromShellCommandline')) {
|
|
|
|
|
$process = Process::fromShellCommandline($command, $cwd, null, null, static::getTimeout());
|
|
|
|
|
} else {
|
|
|
|
|
$process = new Process($command, $cwd, null, null, static::getTimeout());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$job['process'] = $process;
|
|
|
|
|
|
|
|
|
|
$process->start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function wait($index = null)
|
|
|
|
|
{
|
|
|
|
|
while (true) {
|
|
|
|
|
if (!$this->hasActiveJob($index)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
usleep(1000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @internal
|
|
|
|
|
*/
|
|
|
|
|
public function enableAsync()
|
|
|
|
|
{
|
|
|
|
|
$this->allowAsync = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @internal
|
|
|
|
|
*/
|
|
|
|
|
public function hasActiveJob($index = null)
|
|
|
|
|
{
|
|
|
|
|
// tick
|
|
|
|
|
foreach ($this->jobs as &$job) {
|
|
|
|
|
if ($job['status'] === self::STATUS_STARTED) {
|
|
|
|
|
if (!$job['process']->isRunning()) {
|
|
|
|
|
call_user_func($job['resolve'], $job['process']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (null !== $index) {
|
|
|
|
|
if ($this->jobs[$index]['status'] === self::STATUS_COMPLETED || $this->jobs[$index]['status'] === self::STATUS_FAILED || $this->jobs[$index]['status'] === self::STATUS_ABORTED) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($this->jobs as $job) {
|
|
|
|
|
if (!in_array($job['status'], array(self::STATUS_COMPLETED, self::STATUS_FAILED, self::STATUS_ABORTED), true)) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
unset($this->jobs[$job['id']]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
public function markJobDone()
|
|
|
|
|
{
|
|
|
|
|
$this->runningJobs--;
|
|
|
|
|
|
|
|
|
|
foreach ($this->jobs as $job) {
|
|
|
|
|
if ($job['status'] === self::STATUS_QUEUED) {
|
|
|
|
|
$this->startJob($job['id']);
|
|
|
|
|
if ($this->runningJobs >= $this->maxJobs) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function splitLines($output)
|
|
|
|
|
{
|
|
|
|
|
$output = trim($output);
|
|
|
|
|