Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#31 restart process #32

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion lib/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ public function join(): Promise
throw new StatusError("Process has not been started.");
}

return $this->processRunner->join($this->handle);
return call(function () {
$result = yield $this->processRunner->join($this->handle);
$this->handle = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to reset the PID as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now restart return new instance

return $result;
});
}

/**
Expand All @@ -139,6 +143,7 @@ public function kill()
}

$this->processRunner->kill($this->handle);
$this->handle = null;
}

/**
Expand Down Expand Up @@ -281,4 +286,21 @@ public function __debugInfo(): array
'status' => $this->handle ? $this->handle->status : -1,
];
}

/**
* Restart the process.
* @param bool $force Whether to kill process or wait finish
* @return Promise
*/
public function restart($force = true): Promise
{
return call(function () use ($force) {
if ($force) {
$this->kill();
} else {
yield $this->join();
}
return $this->start();
});
}
}
59 changes: 55 additions & 4 deletions test/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,9 @@ public function testProcessCantBeCloned()
}

/**
* @expectedException \Amp\Process\ProcessException
* @expectedExceptionMessage The process was killed
* @expectedException \Amp\Process\StatusError
* @expectedExceptionMessage Process has not been started.

*/
public function testKillImmediately()
{
Expand All @@ -212,8 +213,8 @@ public function testKillImmediately()
}

/**
* @expectedException \Amp\Process\ProcessException
* @expectedExceptionMessage The process was killed
* @expectedException \Amp\Process\StatusError
* @expectedExceptionMessage Process has not been started or has not completed starting.
*/
public function testKillThenReadStdout()
{
Expand Down Expand Up @@ -382,4 +383,54 @@ public function testDebugInfo()
$this->assertSame(ProcessStatus::RUNNING, $debug['status']);
});
}

public function testStartAfterJoin()
{
Loop::run(function () {
$process = new Process(self::CMD_PROCESS_SLOW);
for ($i=0; $i<=1; $i++) {
$process->start();
$this->assertTrue($process->isRunning());
yield $process->join();
$this->assertFalse($process->isRunning());
}
});
}

public function testStartAfterKill()
{
Loop::run(function () {
$process = new Process(self::CMD_PROCESS_SLOW);
for ($i=0; $i<=1; $i++) {
$process->start();
$this->assertTrue($process->isRunning());
$process->kill();
$this->assertFalse($process->isRunning());
}
});
}

public function testRestart()
{
Loop::run(function () {
$process = new Process(self::CMD_PROCESS_SLOW);
$process->start();
for ($i=0; $i<=1; $i++) {
$this->assertTrue($process->isRunning());
$process->restart();
}
});
}

public function testForceRestart()
{
Loop::run(function () {
$process = new Process(self::CMD_PROCESS_SLOW);
$process->start();
for ($i=0; $i<=1; $i++) {
$this->assertTrue($process->isRunning());
$process->restart(true);
}
});
}
}