From 5656c7c6bddc3db10eea6e0d4d26cef5542a3f45 Mon Sep 17 00:00:00 2001 From: kicer Date: Mon, 28 Sep 2026 22:16:49 +0800 Subject: [PATCH] =?UTF-8?q?wxbot:=20=E8=A1=A5=20SIGTERM/SIGINT=20=E4=BC=98?= =?UTF-8?q?=E9=9B=85=E5=81=9C=E6=9C=BA=EF=BC=8C=E5=AF=B9=E9=BD=90=20execut?= =?UTF-8?q?or=20shutdown=20=E6=A8=A1=E5=BC=8F=E2=80=94=E2=80=94=E6=AD=A4?= =?UTF-8?q?=E5=89=8D=E6=9C=AA=E6=B3=A8=E5=86=8C=E4=BB=BB=E4=BD=95=E4=BF=A1?= =?UTF-8?q?=E5=8F=B7=E5=A4=84=E7=90=86=EF=BC=8Cbot.run()=20=E9=95=BF?= =?UTF-8?q?=E8=BD=AE=E8=AF=A2=E4=B8=8E=20SDK=20=E9=95=BF=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E5=90=8A=E7=9D=80=E4=BA=8B=E4=BB=B6=E5=BE=AA=E7=8E=AF=EF=BC=8C?= =?UTF-8?q?docker=20stop/restart=20=E7=AD=89=E6=BB=A1=2010s=20=E5=AE=BD?= =?UTF-8?q?=E9=99=90=E6=9C=9F=E5=90=83=20SIGKILL=EF=BC=8C=E9=87=8D?= =?UTF-8?q?=E5=90=AF=E8=82=89=E7=9C=BC=E5=8F=AF=E8=A7=81=E5=9C=B0=E6=85=A2?= =?UTF-8?q?=EF=BC=88node=20=E4=B8=BA=20PID1=E3=80=81CMD=20exec=20=E5=BD=A2?= =?UTF-8?q?=E5=BC=8F=EF=BC=8C=E4=BF=A1=E5=8F=B7=E6=9C=AC=E5=B0=B1=E7=9B=B4?= =?UTF-8?q?=E8=BE=BE=EF=BC=8C=E7=BC=BA=E7=9A=84=E5=8F=AA=E6=98=AF=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=99=A8=EF=BC=89=E3=80=82=E7=8E=B0=E8=87=AA=E6=8E=A5?= =?UTF-8?q?=E7=AE=A1=EF=BC=9A=E9=81=8D=E5=8E=86=20instances=20=E8=B0=83=20?= =?UTF-8?q?destroyInstance(id,=20false)=20=E5=81=9C=20bot=EF=BC=88?= =?UTF-8?q?=E4=BF=9D=E7=95=99=E7=99=BB=E5=BD=95=E5=87=AD=E8=AF=81=EF=BC=8C?= =?UTF-8?q?=E4=B8=8B=E6=AC=A1=E5=90=AF=E5=8A=A8=E7=85=A7=E5=B8=B8=E6=81=A2?= =?UTF-8?q?=E5=A4=8D=EF=BC=9Bstop=20=E4=B8=BA=20fire-and-forget=20?= =?UTF-8?q?=E4=B8=8D=E9=98=BB=E5=A1=9E=E9=80=80=E5=87=BA=EF=BC=89=EF=BC=8C?= =?UTF-8?q?httpServer.close=20=E5=90=8E=20exit=EF=BC=8C=E5=8F=A6=E6=8C=82?= =?UTF-8?q?=203s=20unref=20=E5=85=9C=E5=BA=95=E5=AE=9A=E6=97=B6=E5=99=A8?= =?UTF-8?q?=E6=97=A0=E6=9D=A1=E4=BB=B6=E9=80=80=E5=87=BA=E2=80=94=E2=80=94?= =?UTF-8?q?SDK=20=E7=AC=AC=E4=B8=89=E6=96=B9=E9=BB=91=E7=9B=92=E5=86=85=20?= =?UTF-8?q?stop/=E5=85=B3=E9=97=AD=E8=8B=A5=E6=82=AC=E6=8C=82=EF=BC=8C?= =?UTF-8?q?=E5=85=9C=E5=BA=95=E6=98=AF=E5=94=AF=E4=B8=80=E5=8F=AF=E4=BF=A1?= =?UTF-8?q?=E4=BF=9D=E9=99=A9=EF=BC=8C=E6=9C=80=E5=9D=8F=E5=81=9C=E6=9C=BA?= =?UTF-8?q?=203s=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wxbot/app/src/server.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/wxbot/app/src/server.js b/wxbot/app/src/server.js index 67ff64b..0ffa953 100644 --- a/wxbot/app/src/server.js +++ b/wxbot/app/src/server.js @@ -414,6 +414,19 @@ for (const ent of fs.readdirSync(CONFIG.bot.storage_dir, { withFileTypes: true } if (ent.isDirectory() && ID_RE.test(ent.name)) createInstance(ent.name); } -app.listen(CONFIG.server.port, "0.0.0.0", () => { - console.log(`[wxbot] listening on :${CONFIG.server.port}, core=${CONFIG.core.url || "(未配置)"}, log_level=${CONFIG.bot.log_level}, 恢复实例 ${instances.size} 个`); +const httpServer = app.listen(CONFIG.server.port, "0.0.0.0", () => { + console.log(`[wxbot] listening on :${CONFIG.server.port}, core=${CONFIG.core.url || "(未配置)"}, log_level=${CONFIG.bot.log_level}, 恢复实例 ${instances.size} 个}`); }); + +// 优雅停机(对齐 executor/server.js 的 shutdown 模式):此前未注册任何信号处理,bot.run() 的 +// 长轮询与 SDK 长连接吊着事件循环,docker stop/restart 只能等满 10s 宽限期吃 SIGKILL,重启肉眼可见地慢。 +// 现自接管:停掉全部实例(保留登录凭证,下次启动照常恢复)、关 HTTP,3s 兜底定时器无条件退出—— +// stop 自身若悬挂(SDK 第三方黑盒)兜底是唯一可信保险;最坏停机 3s。 +function shutdown(sig) { + console.log(`[wxbot] 收到 ${sig},停止服务`); + for (const id of Array.from(instances.keys())) destroyInstance(id, false); + httpServer.close(() => process.exit(0)); + setTimeout(() => process.exit(0), 3000).unref(); +} +process.on("SIGTERM", () => shutdown("SIGTERM")); +process.on("SIGINT", () => shutdown("SIGINT"));