From 3781ed462988c0969044673ba279ffb18d7420b7 Mon Sep 17 00:00:00 2001 From: "QCQCQC@Debian" Date: Wed, 10 Dec 2025 10:13:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9EstopRoutines=E7=9A=84?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=EF=BC=8C=E5=9C=A8shutdown=E6=97=B6=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=EF=BC=8C=E5=B9=B6=E5=AE=8C=E5=96=84=E4=BF=A1=E5=8F=B7?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/server/start.js | 26 ++++++++++++++++++++++---- lib/server/watch.js | 1 + lib/template.js | 2 +- package.json | 2 +- src/server/start.ts | 31 ++++++++++++++++++++++++++----- 5 files changed, 51 insertions(+), 11 deletions(-) diff --git a/lib/server/start.js b/lib/server/start.js index e6d9e6f..a4d5e99 100644 --- a/lib/server/start.js +++ b/lib/server/start.js @@ -402,15 +402,33 @@ async function startup(path, connector, omitWatchers, omitTimers, routine) { if (!omitTimers) { appLoader.startTimers(); } - process.on('SIGINT', async () => { - await appLoader.unmount(); - process.exit(0); - }); const shutdown = async () => { await httpServer.close(); await koa.removeAllListeners(); + await appLoader.execStopRoutines(); await appLoader.unmount(); (0, polyfill_1.removePolyfill)("startup"); }; + // 监听终止信号进行优雅关闭 + // SIGINT - Ctrl+C 发送的中断信号 + process.on('SIGINT', async () => { + await shutdown(); + process.exit(0); + }); + // SIGTERM - 系统/容器管理器发送的终止信号(Docker、K8s、PM2等) + process.on('SIGTERM', async () => { + await shutdown(); + process.exit(0); + }); + // SIGQUIT - Ctrl+\ 发送的退出信号 + process.on('SIGQUIT', async () => { + await shutdown(); + process.exit(0); + }); + // SIGHUP - 终端关闭时发送(可选) + process.on('SIGHUP', async () => { + await shutdown(); + process.exit(0); + }); return shutdown; } diff --git a/lib/server/watch.js b/lib/server/watch.js index 8019e88..c801260 100644 --- a/lib/server/watch.js +++ b/lib/server/watch.js @@ -499,6 +499,7 @@ const watch = async (projectPath, config) => { resolveFullPaths: true, }); function treatFile(filePath) { + console.log(`Processing file for alias replacement: ${filePath}`); const fileContents = fs_1.default.readFileSync(filePath, 'utf8'); const newContents = runFile({ fileContents, filePath }); // do stuff with newContents diff --git a/lib/template.js b/lib/template.js index cb0e8e3..b1d2bfc 100644 --- a/lib/template.js +++ b/lib/template.js @@ -424,7 +424,7 @@ function tsConfigPathsJsonContent(deps) { compilerOptions: { baseUrl: "./", paths, - typeRoots: ["./typings"] + typeRoots: ["./typings", "node_modules/@types"] } }, null, '\t'); } diff --git a/package.json b/package.json index 2f4df11..14aa130 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "babel-plugin-module-resolver": "^5.0.0", "events": "^3.3.0", "fork-ts-checker-webpack-plugin": "^8.0.0", - "ts-node": "^10.9.1", + "ts-node": "^10.9.2", "typescript": "^5.2.2" }, "dependencies": { diff --git a/src/server/start.ts b/src/server/start.ts index 598ba37..63bbc38 100644 --- a/src/server/start.ts +++ b/src/server/start.ts @@ -492,18 +492,39 @@ export async function startup { - await appLoader.unmount(); - process.exit(0); - }); - const shutdown = async () => { await httpServer.close(); await koa.removeAllListeners(); + await appLoader.execStopRoutines(); await appLoader.unmount(); removePolyfill("startup"); } + // 监听终止信号进行优雅关闭 + // SIGINT - Ctrl+C 发送的中断信号 + process.on('SIGINT', async () => { + await shutdown(); + process.exit(0); + }); + + // SIGTERM - 系统/容器管理器发送的终止信号(Docker、K8s、PM2等) + process.on('SIGTERM', async () => { + await shutdown(); + process.exit(0); + }); + + // SIGQUIT - Ctrl+\ 发送的退出信号 + process.on('SIGQUIT', async () => { + await shutdown(); + process.exit(0); + }); + + // SIGHUP - 终端关闭时发送(可选) + process.on('SIGHUP', async () => { + await shutdown(); + process.exit(0); + }); + return shutdown } \ No newline at end of file