mirror of
https://github.com/danog/process.git
synced 2024-11-30 04:39:04 +01:00
Return promise from Process::start(); int from Process::getPid()
This commit is contained in:
parent
9460633c7d
commit
8ec002633b
@ -9,6 +9,7 @@ use Amp\Process\Internal\ProcessRunner;
|
|||||||
use Amp\Process\Internal\ProcessStatus;
|
use Amp\Process\Internal\ProcessStatus;
|
||||||
use Amp\Process\Internal\Windows\Runner as WindowsProcessRunner;
|
use Amp\Process\Internal\Windows\Runner as WindowsProcessRunner;
|
||||||
use Amp\Promise;
|
use Amp\Promise;
|
||||||
|
use function Amp\call;
|
||||||
|
|
||||||
class Process
|
class Process
|
||||||
{
|
{
|
||||||
@ -30,6 +31,9 @@ class Process
|
|||||||
/** @var ProcessHandle */
|
/** @var ProcessHandle */
|
||||||
private $handle;
|
private $handle;
|
||||||
|
|
||||||
|
/** @var int|null */
|
||||||
|
private $pid;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string|string[] $command Command to run.
|
* @param string|string[] $command Command to run.
|
||||||
* @param string|null $cwd Working directory or use an empty string to use the working directory of the
|
* @param string|null $cwd Working directory or use an empty string to use the working directory of the
|
||||||
@ -90,15 +94,20 @@ class Process
|
|||||||
/**
|
/**
|
||||||
* Start the process.
|
* Start the process.
|
||||||
*
|
*
|
||||||
|
* @return Promise<int> Resolves with the PID.
|
||||||
|
*
|
||||||
* @throws StatusError If the process has already been started.
|
* @throws StatusError If the process has already been started.
|
||||||
*/
|
*/
|
||||||
public function start()
|
public function start(): Promise
|
||||||
{
|
{
|
||||||
if ($this->handle) {
|
if ($this->handle) {
|
||||||
throw new StatusError("Process has already been started.");
|
throw new StatusError("Process has already been started.");
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->handle = $this->processRunner->start($this->command, $this->cwd, $this->env, $this->options);
|
return call(function () {
|
||||||
|
$this->handle = $this->processRunner->start($this->command, $this->cwd, $this->env, $this->options);
|
||||||
|
return $this->pid = yield $this->handle->pidDeferred->promise();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -152,17 +161,17 @@ class Process
|
|||||||
/**
|
/**
|
||||||
* Returns the PID of the child process.
|
* Returns the PID of the child process.
|
||||||
*
|
*
|
||||||
* @return Promise<int>
|
* @return int
|
||||||
*
|
*
|
||||||
* @throws StatusError If the process has not started.
|
* @throws StatusError If the process has not started or has not completed starting.
|
||||||
*/
|
*/
|
||||||
public function getPid(): Promise
|
public function getPid(): int
|
||||||
{
|
{
|
||||||
if (!$this->handle) {
|
if (!$this->pid) {
|
||||||
throw new StatusError("Process has not been started.");
|
throw new StatusError("Process has not been started or has not completed starting.");
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->handle->pidDeferred->promise();
|
return $this->pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -8,7 +8,6 @@ use Amp\Loop;
|
|||||||
use Amp\Process\Process;
|
use Amp\Process\Process;
|
||||||
use Amp\Process\ProcessInputStream;
|
use Amp\Process\ProcessInputStream;
|
||||||
use Amp\Process\ProcessOutputStream;
|
use Amp\Process\ProcessOutputStream;
|
||||||
use Amp\Promise;
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
class ProcessTest extends TestCase
|
class ProcessTest extends TestCase
|
||||||
@ -60,9 +59,7 @@ class ProcessTest extends TestCase
|
|||||||
{
|
{
|
||||||
Loop::run(function () {
|
Loop::run(function () {
|
||||||
$process = new Process(self::CMD_PROCESS);
|
$process = new Process(self::CMD_PROCESS);
|
||||||
$process->start();
|
$this->assertInternalType('int', yield $process->start());
|
||||||
|
|
||||||
$this->assertInternalType('int', yield $process->getPid());
|
|
||||||
$this->assertSame(0, yield $process->join());
|
$this->assertSame(0, yield $process->join());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -75,9 +72,8 @@ class ProcessTest extends TestCase
|
|||||||
|
|
||||||
Loop::run(function () {
|
Loop::run(function () {
|
||||||
$process = new Process(self::CMD_PROCESS_SLOW);
|
$process = new Process(self::CMD_PROCESS_SLOW);
|
||||||
$process->start();
|
yield $process->start();
|
||||||
$process->signal(0);
|
$process->signal(0);
|
||||||
$this->assertInstanceOf(Promise::class, $process->getPid());
|
|
||||||
$this->assertSame(0, yield $process->join());
|
$this->assertSame(0, yield $process->join());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -249,14 +245,12 @@ class ProcessTest extends TestCase
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \Amp\Process\StatusError
|
* @expectedException \Amp\Process\StatusError
|
||||||
* @expectedExceptionMessage Process has not been started.
|
* @expectedExceptionMessage Process has not been started or has not completed starting.
|
||||||
*/
|
*/
|
||||||
public function testProcessHasNotBeenStartedWithGetPid()
|
public function testProcessHasNotBeenStartedWithGetPid()
|
||||||
{
|
{
|
||||||
Loop::run(function () {
|
$process = new Process(self::CMD_PROCESS);
|
||||||
$process = new Process(self::CMD_PROCESS);
|
$process->getPid();
|
||||||
yield $process->getPid();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user