Skip to content

Commit

Permalink
Wait for process to end before starting it
Browse files Browse the repository at this point in the history
  • Loading branch information
fritz-gerneth committed Jan 24, 2022
1 parent 6154592 commit e8e5f53
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
21 changes: 18 additions & 3 deletions src/ProcessRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

use React\ChildProcess\Process as ReactPHPProcess;
use React\EventLoop\LoopInterface;
use React\Promise\Deferred;
use React\Promise\PromiseInterface;
use RuntimeException;
use seregazhuk\PhpWatcher\Screen\Screen;
use function React\Promise\resolve;

final class ProcessRunner
{
Expand All @@ -31,15 +34,27 @@ public function start(): void
$this->subscribeToProcessOutput();
}

public function stop(int $signal): void
public function stop(int $signal): PromiseInterface
{
if (false === $this->process->isRunning()) {
return resolve();
}

$defered = new Deferred();

$exitHandler = function () use ($defered, &$exitHandler) {
$this->process->removeAllListeners();
$defered->resolve();
};
$this->process->on('exit', $exitHandler);
$this->process->terminate($signal);
$this->process->removeAllListeners();

return $defered->promise();
}

public function restart(float $delayToRestart): void
{
$this->screen->restarting($this->process->getCommand());
$this->screen->restarting();
$this->loop->addTimer($delayToRestart, [$this, 'start']);
}

Expand Down
5 changes: 3 additions & 2 deletions src/Watcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public function startWatching(
$this->filesystemListener->start($watchList);
$this->filesystemListener->onChange(
static function () use ($processRunner, $signal, $delayToRestart) {
$processRunner->stop($signal);
$processRunner->restart($delayToRestart);
$processRunner
->stop($signal)
->then(fn() => $processRunner->restart($delayToRestart));
}
);

Expand Down

0 comments on commit e8e5f53

Please sign in to comment.