Skip to content
Open
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: 13 additions & 6 deletions src/Manager/Flexible.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use React\EventLoop\LoopInterface;
use WyriHaximus\React\ChildProcess\Messenger\Messages\Message;
use WyriHaximus\React\ChildProcess\Messenger\Messenger;
use WyriHaximus\React\ChildProcess\Messenger\ProcessUnexpectedEndException;
use WyriHaximus\React\ChildProcess\Messenger\MessengerInterface;
use WyriHaximus\React\ChildProcess\Pool\ManagerInterface;
use WyriHaximus\React\ChildProcess\Pool\Options;
Expand Down Expand Up @@ -88,7 +89,11 @@ protected function spawn()
$worker->on('message', function ($message) {
$this->emit('message', [$message]);
});
$worker->on('error', function ($error) {
$worker->on('error', function ($error) use ($worker) {
if ($error instanceof ProcessUnexpectedEndException) {
$worker->terminate();
$this->ping();
}
$this->emit('error', [$error]);
});
$this->workerAvailable($worker);
Expand Down Expand Up @@ -116,18 +121,20 @@ protected function spawnAndGetMessenger(callable $current)

public function ping()
{
if (count($this->workers) + $this->startingProcesses < $this->options[Options::MIN_SIZE]) {
for ($i = count($this->workers) + $this->startingProcesses; $i <= $this->options[Options::MIN_SIZE]; $i++) {
$this->spawn();
}
return;
}

foreach ($this->workers as $worker) {
if (!$worker->isBusy()) {
$this->workerAvailable($worker);
return;
}
}

if (count($this->workers) + $this->startingProcesses < $this->options[Options::MIN_SIZE]) {
$this->spawn();
return;
}

if (count($this->workers) + $this->startingProcesses < $this->options[Options::MAX_SIZE]) {
$this->spawn();
}
Expand Down