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
+18 -7
View File
@@ -17,6 +17,8 @@ abstract class AbstractProcess implements OnProcessInterface
private bool $stop = false;
private bool $stopping = false;
public Process $process;
@@ -132,10 +134,11 @@ abstract class AbstractProcess implements OnProcessInterface
$array['deadlock_check_disable_trace'] = false;
$array['exit_condition'] = [$this, 'exit_condition'];
Coroutine::set($array);
$process::signal(SIGINT, static function (): void {});
Coroutine::create(fn() => $this->coroutineWaitSignal());
} else {
$process::signal(SIGTERM, [$this, 'pointWaitSignal']);
$process::signal(SIGINT, [$this, 'pointWaitSignal']);
$process::signal(SIGINT, static function (): void {});
}
return $this;
}
@@ -153,9 +156,7 @@ abstract class AbstractProcess implements OnProcessInterface
*/
public function pointWaitSignal($signal): void
{
$this->stop = true;
$this->onSigterm();
$this->requestStop();
}
@@ -164,10 +165,20 @@ abstract class AbstractProcess implements OnProcessInterface
*/
public function coroutineWaitSignal(): void
{
$value = Coroutine::waitSignal(SIGTERM);
if ($value) {
$this->stop = true;
Coroutine::waitSignal(SIGTERM);
$this->requestStop();
}
private function requestStop(): void
{
if ($this->stopping) {
return;
}
$this->stopping = true;
$this->stop = true;
$this->onSigterm();
}
+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);
}
}
}
}