How to start background processes
Thomblin opened this issue · comments
Sebastian Detert commented
What is the recommended way to start and stop a process that shall run for some time in the background (windows and linux)? I was not able to find a stable solution so far.
My current approach is
public function runNonBlocking(): void
{
Loop::run(function () {
yield $this->process->start();
$this->process->getStdout()->read()->onResolve(function (\Throwable $reason = null, $value) {
$this->output .= $value;
});
$this->process->getStderr()->read()->onResolve(function (\Throwable $reason = null, $value) {
$this->output .= $value;
});
Loop::stop();
};)
}
public function stop(): bool
{
if (!$this->process->isRunning()) {
return true;
}
$pid = $this->process->getPid();
if ('\\' === \DIRECTORY_SEPARATOR) {
\exec(\sprintf('taskkill /F /T /PID %d 2>&1', $pid), $output, $exitCode);
if ($exitCode) {
throw new ProcessException(\sprintf('Unable to kill the process (%s).', \implode(' ', $output)));
}
}
$this->process->kill(); // this is not reliable on windows
if ($this->process->isRunning()) {
throw new ProcessException(\sprintf('Error while killing process "%s".', $pid));
}
return true;
}
Two connected errors:
Reason 1: https://ci.appveyor.com/project/mattermack/pact-php/builds/25020714
Reason 2: https://ci.appveyor.com/project/mattermack/pact-php/builds/25020861
Sebastian Detert commented
Alright, I found the problem.
$this->process->kill()
does not kill child processes
taskkill /F /T /PID %d
does kill child processes
maybe we could add that?
Niklas Keller commented
v1.1.1 uses taskkill
in Process::kill()
.