@myassis/gateway 1.0.47 → 1.0.48
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -103,11 +103,35 @@ function getStoredPid() {
|
|
|
103
103
|
}
|
|
104
104
|
/**
|
|
105
105
|
* 停止 Gateway 用户进程(优雅终止)
|
|
106
|
+
*
|
|
107
|
+
* 注意:当 pid 指向当前进程自己时,绝不能使用 process.kill(pid)。
|
|
108
|
+
* - Windows 上 Node 的 process.kill 等同 TerminateProcess,无清理机会,
|
|
109
|
+
* server.close() / WebSocket / 文件句柄都不会被释放,会导致后续 exe 解锁超时。
|
|
110
|
+
* - 因此自杀场景改走 process.emit('SIGTERM'),复用 main.ts 已注册的优雅关闭逻辑,
|
|
111
|
+
* 并在响应返回后异步退出,避免 HTTP 调用方读不到结果。
|
|
106
112
|
*/
|
|
107
113
|
async function stopGatewayProcess() {
|
|
108
114
|
const pid = getStoredPid();
|
|
109
115
|
if (!pid)
|
|
110
116
|
return;
|
|
117
|
+
// —— 场景 1:要停的就是自己 —— 走优雅退出
|
|
118
|
+
if (pid === process.pid) {
|
|
119
|
+
try {
|
|
120
|
+
fs_1.default.unlinkSync(GATEWAY_PID_FILE);
|
|
121
|
+
}
|
|
122
|
+
catch { /* ignore */ }
|
|
123
|
+
// 异步触发,确保当前调用栈(含 HTTP 响应)有机会先返回
|
|
124
|
+
setTimeout(() => {
|
|
125
|
+
try {
|
|
126
|
+
process.emit('SIGTERM');
|
|
127
|
+
}
|
|
128
|
+
catch { /* ignore */ }
|
|
129
|
+
// 兜底:5s 内 SIGTERM handler 没把进程关掉,再强退
|
|
130
|
+
setTimeout(() => process.exit(0), 5000).unref();
|
|
131
|
+
}, 100).unref();
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
// —— 场景 2:停的是别人(孤儿进程 / 旧实例)—— 才真正发信号
|
|
111
135
|
try {
|
|
112
136
|
process.kill(pid, 'SIGTERM');
|
|
113
137
|
await new Promise(r => setTimeout(r, 2000));
|