Skip to content

Commit 2de0f1a

Browse files
authored
确保PM2 restart/stop一定出发结束守护进程
确保PM2 restart/stop一定出发结束守护进程
1 parent ac7d6f0 commit 2de0f1a

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,20 @@ export default async function handler(req, res) {
213213
const currentFile = path.normalize(fileURLToPath(import.meta.url)); // 使用 normalize 确保路径一致
214214
const indexFile = path.normalize(path.resolve(__dirname, 'index.js')); // 标准化路径
215215

216+
// 保证 PM2 restart / stop 一定会触发
217+
['SIGINT', 'SIGTERM', 'SIGUSR2'].forEach(sig =>
218+
process.on(sig, async () => {
219+
console.log(`[${sig}] 开始优雅关闭...`);
220+
try {
221+
await daemon.stopDaemon();
222+
console.log('Python 守护进程已停止');
223+
} catch (e) {
224+
console.error(e);
225+
}
226+
process.exit(0);
227+
})
228+
);
229+
216230
if (currentFile === indexFile) {
217231
start();
218232
}

0 commit comments

Comments
 (0)