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 all 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
19 changes: 19 additions & 0 deletions lib/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,23 @@ 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<Process> new process instance
*/
public function restart($force = false): Promise
{
return call(function () use ($force) {
if ($force) {
$this->kill();
} else {
yield $this->join();
}
$instance = new static($this->command, $this->cwd, $this->env, $this->options);
yield $instance->start();
return $instance;
});
}
}
30 changes: 30 additions & 0 deletions test/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,34 @@ public function testDebugInfo()
$this->assertSame(ProcessStatus::RUNNING, $debug['status']);
});
}

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

$process = yield $process->restart();

$this->assertNotSame($process, $original);
}
});
}

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

$process = yield $process->restart(true);

$this->assertNotSame($process, $original);
}
});
}
}