This commit is contained in:
2026-07-08 11:39:27 +08:00
parent 1f443958e8
commit 27ffa05515
6 changed files with 221 additions and 30 deletions
+38 -1
View File
@@ -5,6 +5,8 @@ namespace Kiri\Server\Processes;
use Exception;
use Kiri;
use Swoole\Process;
use const SIGKILL;
use const SIGTERM;
trait TraitProcess
{
@@ -66,4 +68,39 @@ trait TraitProcess
{
return $this->_process;
}
}
/**
* @return void
*/
private function terminateProcesses(): void
{
$pids = [];
foreach ($this->_process as $process) {
$pid = (int)($process->pid ?? 0);
if ($pid > 1 && $pid !== getmypid()) {
$pids[] = $pid;
}
}
$pids = array_values(array_unique($pids));
foreach ($pids as $pid) {
if (@Process::kill($pid, 0)) {
@Process::kill($pid, SIGTERM);
}
}
for ($i = 0; $i < 10; $i++) {
$alive = array_values(array_filter($pids, static fn(int $pid): bool => @Process::kill($pid, 0)));
if ($alive === []) {
return;
}
usleep(100000);
}
foreach ($pids as $pid) {
if (@Process::kill($pid, 0)) {
@Process::kill($pid, SIGKILL);
}
}
}
}