优化重启逻辑

This commit is contained in:
2026-09-22 19:47:03 +08:00
parent 485a604d2d
commit ab667c136c
3 changed files with 98 additions and 2 deletions
+35 -1
View File
@@ -10,6 +10,26 @@ const ZHIPU_API_URL =
"https://open.bigmodel.cn/api/paas/v4/images/generations";
const DEFAULT_MODEL = "CogView-3-Flash";
// ── 优雅退出 ──────────────────────────────────────────────────────────────
// 容器里 node 以 PID 1 运行且未注册 SIGTERM 处理器时,内核不会对其执行默认
// 终止动作,docker stop/restart 会白等满 10s 宽限期才被 SIGKILL。
// 显式注册处理器,关闭 MCP session 与长连接后立即退出;3s 定时器为兑底。
let httpServer = null;
let closeAllSessions = () => {};
let shuttingDown = false;
function shutdown(sig) {
if (shuttingDown) return;
shuttingDown = true;
console.log(`[image_gen] 收到 ${sig},停止服务`);
closeAllSessions();
if (!httpServer) process.exit(0); // stdio 模式无监听,直接退
httpServer.close(() => process.exit(0));
setTimeout(() => process.exit(0), 3000).unref();
}
process.on("SIGTERM", () => shutdown("SIGTERM"));
process.on("SIGINT", () => shutdown("SIGINT"));
function parseArgs(argv) {
const args = {
http: false,
@@ -206,6 +226,20 @@ async function startHttp({ port, host }) {
// session -> { server, transport } 映射
const sessions = new Map();
closeAllSessions = () => {
for (const { server, transport } of sessions.values()) {
try {
transport.close();
} catch {}
try {
server.close();
} catch {}
}
sessions.clear();
// 掰掉未关闭的 SSE / keep-alive 连接,否则 httpServer.close() 回调不会触发
httpServer?.closeAllConnections?.();
};
app.get("/health", (_req, res) => {
res.json({ status: "ok", service: "image_gen", sessions: sessions.size });
});
@@ -268,7 +302,7 @@ async function startHttp({ port, host }) {
app.get("/mcp", handleOther);
app.delete("/mcp", handleOther);
app.listen(port, host, () => {
httpServer = app.listen(port, host, () => {
console.log(
`[image_gen] MCP HTTP server listening on http://${host}:${port}/mcp`
);
+35 -1
View File
@@ -19,6 +19,26 @@ const DEFAULT_QUALITY = "speed"; // speed | quality
const DEFAULT_FPS = 30; // 30 | 60
const DEFAULT_WATERMARK_ENABLED = true; // 平台政策默认 true
// ── 优雅退出 ──────────────────────────────────────────────────────────────
// 容器里 node 以 PID 1 运行且未注册 SIGTERM 处理器时,内核不会对其执行默认
// 终止动作,docker stop/restart 会白等满 10s 宽限期才被 SIGKILL。
// 显式注册处理器,关闭 MCP session 与长连接后立即退出;3s 定时器为兑底。
let httpServer = null;
let closeAllSessions = () => {};
let shuttingDown = false;
function shutdown(sig) {
if (shuttingDown) return;
shuttingDown = true;
console.log(`[video_gen] 收到 ${sig},停止服务`);
closeAllSessions();
if (!httpServer) process.exit(0); // stdio 模式无监听,直接退
httpServer.close(() => process.exit(0));
setTimeout(() => process.exit(0), 3000).unref();
}
process.on("SIGTERM", () => shutdown("SIGTERM"));
process.on("SIGINT", () => shutdown("SIGINT"));
function parseArgs(argv) {
const args = {
http: false,
@@ -369,6 +389,20 @@ async function startHttp({ port, host }) {
// session -> { server, transport } 映射
const sessions = new Map();
closeAllSessions = () => {
for (const { server, transport } of sessions.values()) {
try {
transport.close();
} catch {}
try {
server.close();
} catch {}
}
sessions.clear();
// 掰掉未关闭的 SSE / keep-alive 连接,否则 httpServer.close() 回调不会触发
httpServer?.closeAllConnections?.();
};
app.get("/health", (_req, res) => {
res.json({ status: "ok", service: "video_gen", sessions: sessions.size });
});
@@ -431,7 +465,7 @@ async function startHttp({ port, host }) {
app.get("/mcp", handleOther);
app.delete("/mcp", handleOther);
app.listen(port, host, () => {
httpServer = app.listen(port, host, () => {
console.log(
`[video_gen] MCP HTTP server listening on http://${host}:${port}/mcp`
);
+28
View File
@@ -18,6 +18,26 @@ import { createServer } from 'node:http';
import { randomUUID } from 'node:crypto';
import { SERVER_NAME, VERSION, dispatch } from './mcp.js';
// ── 优雅退出 ────────────────────────────────────────────────────────────────
// 容器里 node 以 PID 1 运行且未注册 SIGTERM 处理器时,内核不会对其执行默认
// 终止动作,docker stop/restart 会白等满 10s 宽限期才被 SIGKILL。
// 显式注册处理器立即关服务;3s 兜底定时器防止长连接卡住 close 回调。
let httpServer = null;
let forceCloseConnections = () => {};
let shuttingDown = false;
function shutdown(sig) {
if (shuttingDown) return;
shuttingDown = true;
process.stdout.write(`[web_search] 收到 ${sig},停止服务\n`);
forceCloseConnections();
if (httpServer === null) process.exit(0); // stdio 模式无监听,直接退
httpServer.close(() => process.exit(0));
setTimeout(() => process.exit(0), 3000).unref();
}
process.on('SIGTERM', () => shutdown('SIGTERM'));
process.on('SIGINT', () => shutdown('SIGINT'));
// ── stdio 传输(默认)───────────────────────────────────────────────────────
function runStdio() {
const rl = createInterface({ input: process.stdin, crlfDelay: Infinity });
@@ -107,6 +127,14 @@ function runHttp(host, port) {
}));
});
httpServer = server;
forceCloseConnections = () => {
// 掐掉老式 SSE 长连接与 keep-alive 连接,否则 server.close() 回调不会触发
for (const res of sseChannels.values()) res.destroy();
sseChannels.clear();
server.closeAllConnections?.();
};
server.listen(port, host, () => {
process.stdout.write(`[web_search] HTTP/SSE listening on http://${host}:${port} (MCP: POST /, GET /sse + POST /messages)\n`);
});