Skip to content

Commit

Permalink
Handle SIGCHLD signal to fix zombie defunct
Browse files Browse the repository at this point in the history
  • Loading branch information
vtsykun committed Oct 3, 2023
1 parent 845e5ea commit 9a372ab
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions docker/supervisor.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class TaskManager
{
protected $isRunning = false;
protected $currentUid = null;
protected $wait4;

public function __construct(private readonly string $configPath)
{
Expand Down Expand Up @@ -103,6 +104,13 @@ public function run()
$this->reloadTasks($tasks);
});

pcntl_signal(SIGCHLD, function () {
$this->writeln("[signal-handler] receive signal SIGCHLD");
$p = pcntl_waitpid(-1, $status, WNOHANG);
$this->writeln("Child process with pid $p exit with status $status");
$this->wait4 = 0;
});

$priority = 0;
foreach ($tasks as $task) {
if ($priority !== $task->priority) {
Expand All @@ -119,7 +127,11 @@ public function run()
} catch (\Throwable $e) {
$this->writeln((string) $e);
}
usleep(300000);

$this->wait4 = 9;
while (--$this->wait4 > 0) {
usleep(300000);
}
}
}

Expand Down Expand Up @@ -202,11 +214,11 @@ protected function startProcess(Task $task): Process
$process = $task->process = Process::fromShellCommandline($cmd, $task->directory, $task->env);
$process->setTimeout(null);

$process->start(static function ($type, $buffer) use ($task): void {
$process->start(function ($type, $buffer) use ($task): void {
if (Process::ERR === $type) {
echo "[{$task->name}]: " . $buffer;
$this->writeln("[{$task->name}]+ $buffer");
} else if ($task->isOut()) {
echo "[{$task->name}]" . $buffer;
$this->writeln("[{$task->name}] $buffer");
}
});

Expand All @@ -215,7 +227,7 @@ protected function startProcess(Task $task): Process

protected function writeln(string $msg): void
{
echo "$msg\n";
echo (new \DateTime('now'))->format('Y-m-d H:i:s.u') . " $msg\n";
}

protected function loadConfiguration(string $file = null): array
Expand Down

0 comments on commit 9a372ab

Please sign in to comment.