优化重启逻辑

This commit is contained in:
2026-09-22 19:38:53 +08:00
parent bcc1367291
commit 07d12ec470
2 changed files with 20 additions and 1 deletions
+13
View File
@@ -162,3 +162,16 @@ mcp.init(cfg).then(async () => {
}
});
});
// 优雅退出(照抄 executor/server.js 的 shutdown 形态):node 在容器里是 PID 1,
// 不显式注册监听时 SIGTERM 会被直接忽略,docker stop/restart 只能白等满
// stop_grace_period(默认 10s)后被 SIGKILL。scheduler/holdSweeper 的 timer
// 均已 unref,退出只需收掉监听与 SSE 长连接。
function shutdown(sig) {
log.info('shutdown', '收到 ' + sig + ',停止服务');
bus.closeAllSessions(); // 给前端干净的流结束(走重连/回放),而不是 TCP reset
server.close(() => process.exit(0));
setTimeout(() => process.exit(0), 3000).unref(); // 关不掉的连接 3s 后兑底硬退
}
process.on('SIGTERM', () => shutdown('SIGTERM'));
process.on('SIGINT', () => shutdown('SIGINT'));
+7 -1
View File
@@ -38,7 +38,13 @@ function createBus() {
return set ? set.size : 0;
}
return { addClient, removeClient, emit, closeAll, clientCount };
// 进程退出前用:掐断所有会话的 SSE 流,让 server.close() 能等到连接清零
// (SSE 是长连接,不主动结束的话 close 回调永远不来)。
function closeAllSessions() {
for (const sessionId of Array.from(clients.keys())) closeAll(sessionId);
}
return { addClient, removeClient, emit, closeAll, closeAllSessions, clientCount };
}
module.exports = { createBus };