fix(进程管理): 添加Windows平台兼容性处理

修改数据库维护子进程和父进程的信号处理逻辑,增加对Windows平台的兼容性检查。在Windows平台下跳过不支持的POSIX函数调用,避免运行时错误。
This commit is contained in:
2026-01-24 17:57:29 +08:00
parent c9d4d1d797
commit 0a7301f39d

View File

@@ -479,9 +479,17 @@ if (extension_loaded('pcntl')) {
// 每30秒检查一次数据库连接
$checkInterval = 30; // 秒
// 保存父进程PID
// 检查是否在Windows平台
$isWindows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
// 保存父进程PID仅在非Windows平台
$parentPid = null;
if (!$isWindows && function_exists('getppid')) {
$parentPid = getppid();
ws_echo("[数据库维护子进程] 父进程PID: {$parentPid}");
} else {
ws_echo("[数据库维护子进程] 运行在Windows平台跳过父进程PID检查");
}
// 设置子进程的信号处理
pcntl_signal(SIGTERM, function() {
@@ -493,12 +501,14 @@ if (extension_loaded('pcntl')) {
// 检查是否有信号需要处理
pcntl_signal_dispatch();
// 检查父进程是否仍然存在
// 检查父进程是否仍然存在仅在非Windows平台
if (!$isWindows && function_exists('getppid')) {
$currentParentPid = getppid();
if ($currentParentPid === 1) {
ws_echo("[数据库维护子进程] 父进程已退出,正在退出...");
exit(0);
}
}
try {
// 尝试执行一个简单的数据库查询来测试连接
@@ -526,16 +536,23 @@ if (extension_loaded('pcntl')) {
// 父进程记录子进程PID并设置信号处理
$dbMaintenancePid = $pid;
// 检查是否在Windows平台
$isWindows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
// 设置父进程的信号处理
pcntl_signal(SIGINT, function() use ($dbMaintenancePid) {
pcntl_signal(SIGINT, function() use ($dbMaintenancePid, $isWindows) {
ws_echo("[WebSocket服务器] 收到终止信号,正在停止...");
// 如果子进程存在,发送终止信号
if ($dbMaintenancePid) {
ws_echo("[WebSocket服务器] 停止数据库连接维护进程");
if (!$isWindows && function_exists('posix_kill')) {
posix_kill($dbMaintenancePid, SIGTERM);
// 等待子进程退出
pcntl_wait($status);
} else {
ws_echo("[WebSocket服务器] 运行在Windows平台跳过子进程终止信号发送");
}
}
ws_echo("[WebSocket服务器] 已停止");