function ProcessExecutor::executeAsync
starts a process on the commandline in async mode
@phpstan-return PromiseInterface<Process>
Parameters
string|list<string> $command the command to execute:
string $cwd the working directory:
File
-
vendor/
composer/ composer/ src/ Composer/ Util/ ProcessExecutor.php, line 222
Class
- ProcessExecutor
- @author Robert Schönthal <seroscho@googlemail.com> @author Jordi Boggiano <j.boggiano@seld.be>
Namespace
Composer\UtilCode
public function executeAsync($command, ?string $cwd = null) : PromiseInterface {
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 = [
'id' => $this->idGen++,
'status' => self::STATUS_QUEUED,
'command' => $command,
'cwd' => $cwd,
];
$resolver = static function ($resolve, $reject) use (&$job) : void {
$job['status'] = ProcessExecutor::STATUS_QUEUED;
$job['resolve'] = $resolve;
$job['reject'] = $reject;
};
$canceler = static function () use (&$job) : void {
if ($job['status'] === ProcessExecutor::STATUS_QUEUED) {
$job['status'] = ProcessExecutor::STATUS_ABORTED;
}
if ($job['status'] !== ProcessExecutor::STATUS_STARTED) {
return;
}
$job['status'] = ProcessExecutor::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);
throw new \RuntimeException('Aborted process');
};
$promise = new Promise($resolver, $canceler);
$promise = $promise->then(function () use (&$job) {
if ($job['process']->isSuccessful()) {
$job['status'] = ProcessExecutor::STATUS_COMPLETED;
}
else {
$job['status'] = ProcessExecutor::STATUS_FAILED;
}
$this->markJobDone();
return $job['process'];
}, function ($e) use (&$job) : void {
$job['status'] = ProcessExecutor::STATUS_FAILED;
$this->markJobDone();
throw $e;
});
$this->jobs[$job['id']] =& $job;
if ($this->runningJobs < $this->maxJobs) {
$this->startJob($job['id']);
}
return $promise;
}