amphp / process

An async process dispatcher for Amp.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to start background processes

Thomblin opened this issue · comments

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.

(see https://github.com/pact-foundation/pact-php/blob/master/src/PhpPact/Standalone/Runner/ProcessRunner.php)

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

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?

v1.1.1 uses taskkill in Process::kill().