Skip to content

Commit c37fa3d

Browse files
committed
Remove SupervisorDriver
1 parent e9d1103 commit c37fa3d

File tree

3 files changed

+36
-185
lines changed

3 files changed

+36
-185
lines changed

src/Internal/MasterProcess.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PHPUnit\Runner\ErrorException;
1818
use Psr\Log\LoggerInterface;
1919
use Revolt\EventLoop;
20+
use Revolt\EventLoop\Driver\StreamSelectDriver;
2021
use Revolt\EventLoop\Suspension;
2122
use function Amp\Future\awaitAll;
2223

@@ -157,7 +158,7 @@ private function initServer(): void
157158
}
158159

159160
// Init event loop.
160-
EventLoop::setDriver(new SupervisorDriver());
161+
EventLoop::setDriver(new StreamSelectDriver());
161162
EventLoop::setErrorHandler(ErrorHandler::handleException(...));
162163
$this->suspension = EventLoop::getDriver()->getSuspension();
163164

src/Internal/SupervisorDriver.php

Lines changed: 0 additions & 160 deletions
This file was deleted.

src/Plugin/Supervisor/Supervisor.php

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,35 +55,20 @@ public function start(MasterProcess $masterProcess, Suspension $suspension): voi
5555
$this->workerRelay = new Relay($masterPipe);
5656
$this->serverStatus->subscribeToWorkerMessages($this->workerRelay);
5757

58-
EventLoop::getDriver()->onChildProcessExit($this->onChildStop(...));
59-
EventLoop::repeat(WorkerProcess::HEARTBEAT_PERIOD, fn() => $this->monitorWorkerStatus());
60-
58+
$this->watchChildProcesses();
6159
$this->spawnWorkers();
6260
}
6361

64-
public function stop(): Future
62+
public function watchChildProcesses(): void
6563
{
66-
$this->stopFuture = new DeferredFuture();
67-
68-
foreach ($this->workerPool->getAlivePids() as $pid) {
69-
\posix_kill($pid, SIGTERM);
70-
}
71-
72-
if ($this->workerPool->getWorkerCount() === 0) {
73-
$this->stopFuture->complete();
74-
} else {
75-
EventLoop::delay($this->stopTimeout, function (): void {
76-
// Send SIGKILL signal to all child processes ater timeout
77-
foreach ($this->workerPool->getAlivePids() as $pid) {
78-
\posix_kill($pid, SIGKILL);
79-
$worker = $this->workerPool->getWorkerByPid($pid);
80-
$this->logger->notice(\sprintf('Worker %s[pid:%s] killed after %ss timeout', $worker->name, $pid, $this->stopTimeout));
81-
}
82-
$this->stopFuture->complete();
83-
});
84-
}
64+
EventLoop::onSignal(SIGCHLD, function () {
65+
while (($pid = \pcntl_wait($status, WNOHANG)) > 0) {
66+
$exitCode = \pcntl_wexitstatus($status) ?: 0;
67+
$this->onChildStop($pid, $exitCode);
68+
}
69+
});
8570

86-
return $this->stopFuture->getFuture();
71+
EventLoop::repeat(WorkerProcess::HEARTBEAT_PERIOD, fn() => $this->monitorWorkerStatus());
8772
}
8873

8974
private function spawnWorkers(): void
@@ -156,6 +141,31 @@ private function onChildStop(int $pid, int $exitCode): void
156141
}
157142
}
158143

144+
public function stop(): Future
145+
{
146+
$this->stopFuture = new DeferredFuture();
147+
148+
foreach ($this->workerPool->getAlivePids() as $pid) {
149+
\posix_kill($pid, SIGTERM);
150+
}
151+
152+
if ($this->workerPool->getWorkerCount() === 0) {
153+
$this->stopFuture->complete();
154+
} else {
155+
EventLoop::delay($this->stopTimeout, function (): void {
156+
// Send SIGKILL signal to all child processes ater timeout
157+
foreach ($this->workerPool->getAlivePids() as $pid) {
158+
\posix_kill($pid, SIGKILL);
159+
$worker = $this->workerPool->getWorkerByPid($pid);
160+
$this->logger->notice(\sprintf('Worker %s[pid:%s] killed after %ss timeout', $worker->name, $pid, $this->stopTimeout));
161+
}
162+
$this->stopFuture->complete();
163+
});
164+
}
165+
166+
return $this->stopFuture->getFuture();
167+
}
168+
159169
public function reload(): void
160170
{
161171
foreach ($this->workerPool->getAlivePids() as $pid) {

0 commit comments

Comments
 (0)