panrouter 6.3.4 → 6.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/pool-worker.mjs +60 -10
package/package.json
CHANGED
package/pool-worker.mjs
CHANGED
|
@@ -266,6 +266,17 @@ async function doRestart() {
|
|
|
266
266
|
// ─── 处理来自主控的请求 ─────────────────────────────────────────────────────
|
|
267
267
|
|
|
268
268
|
function handleIncomingRequest(msg) {
|
|
269
|
+
// ponytail: 不阻塞请求,但如果 watchdog 标记了挂了就先重启
|
|
270
|
+
if (!_serverReady) {
|
|
271
|
+
isPortOpen(SERVER_PORT).then(open => {
|
|
272
|
+
if (!open) {
|
|
273
|
+
log(`9router 不在线,尝试重启`, "WARN");
|
|
274
|
+
killPort(SERVER_PORT);
|
|
275
|
+
trySpawn9router();
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
|
|
269
280
|
const body = msg.body || "";
|
|
270
281
|
|
|
271
282
|
const options = {
|
|
@@ -413,7 +424,7 @@ function ensureServer() {
|
|
|
413
424
|
function _ensureServer() {
|
|
414
425
|
const setupScript = path.join(__dirname, "setup-9router.cjs");
|
|
415
426
|
if (!fs.existsSync(setupScript)) {
|
|
416
|
-
log("找不到 setup-9router.cjs
|
|
427
|
+
log("找不到 setup-9router.cjs,直接启动 9router", "WARN");
|
|
417
428
|
trySpawn9router();
|
|
418
429
|
poll9router();
|
|
419
430
|
return;
|
|
@@ -423,19 +434,23 @@ function _ensureServer() {
|
|
|
423
434
|
const child = spawn(process.execPath, [setupScript], {
|
|
424
435
|
cwd: __dirname, stdio: "inherit",
|
|
425
436
|
});
|
|
437
|
+
// 等安装完再启动 9router — 统一用 --tray 模式,setup 自己开的 cmd 窗口让它去
|
|
426
438
|
child.on("exit", () => {
|
|
427
|
-
log("
|
|
439
|
+
log("安装完成,正在启动 9router...");
|
|
428
440
|
trySpawn9router();
|
|
429
441
|
});
|
|
430
|
-
|
|
431
442
|
poll9router();
|
|
432
443
|
}
|
|
433
444
|
|
|
434
445
|
function trySpawn9router() {
|
|
435
446
|
try {
|
|
436
|
-
|
|
437
|
-
|
|
447
|
+
// ponytail: --tray 跳过 TTY 菜单(否则无 TTY 环境 3 秒自杀)
|
|
448
|
+
// --skip-update 跳过 npm registry 检查(手机网络不稳定)
|
|
449
|
+
// 不用 npx,9router 已被 setup-9router.cjs 全局安装
|
|
450
|
+
const r = spawn("9router", ["--tray", "--skip-update"], {
|
|
451
|
+
detached: true, stdio: ["ignore", "ignore", "pipe"], shell: true,
|
|
438
452
|
});
|
|
453
|
+
r.stderr?.on("data", (d) => log(`9router 错误: ${d.toString().trim()}`, "ERR"));
|
|
439
454
|
r.unref();
|
|
440
455
|
} catch (e) {
|
|
441
456
|
log(`启动 9router 失败: ${e.message}`, "ERR");
|
|
@@ -443,14 +458,47 @@ function trySpawn9router() {
|
|
|
443
458
|
}
|
|
444
459
|
|
|
445
460
|
async function poll9router() {
|
|
446
|
-
|
|
461
|
+
const deadline = Date.now() + 30000;
|
|
462
|
+
while (Date.now() < deadline) {
|
|
447
463
|
if (await isPortOpen(SERVER_PORT)) {
|
|
448
464
|
_serverReady = true;
|
|
449
465
|
log(`9router 已就绪 (端口 ${SERVER_PORT})`, "OK");
|
|
450
|
-
return;
|
|
466
|
+
return true;
|
|
451
467
|
}
|
|
452
|
-
|
|
453
|
-
|
|
468
|
+
await new Promise((r) => setTimeout(r, 1000));
|
|
469
|
+
}
|
|
470
|
+
log(`等待 9router 超时,继续后台重试`, "WARN");
|
|
471
|
+
return false;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
// ponytail: 每隔 30 秒检查 9router 是否活着,死了就重启
|
|
475
|
+
let watchdogTimer = null;
|
|
476
|
+
|
|
477
|
+
function startWatchdog() {
|
|
478
|
+
if (watchdogTimer) return;
|
|
479
|
+
watchdogTimer = setInterval(async () => {
|
|
480
|
+
if (await isPortOpen(SERVER_PORT)) return;
|
|
481
|
+
log(`9router 无响应,正在重启...`, "WARN");
|
|
482
|
+
_serverReady = false;
|
|
483
|
+
killPort(SERVER_PORT);
|
|
484
|
+
trySpawn9router();
|
|
485
|
+
// 等待最多 15 秒
|
|
486
|
+
for (let i = 0; i < 15; i++) {
|
|
487
|
+
await new Promise((r) => setTimeout(r, 1000));
|
|
488
|
+
if (await isPortOpen(SERVER_PORT)) {
|
|
489
|
+
_serverReady = true;
|
|
490
|
+
log(`9router 已恢复`, "OK");
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
log(`9router 重启失败,下次再试`, "ERR");
|
|
495
|
+
}, 30000);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
function stopWatchdog() {
|
|
499
|
+
if (watchdogTimer) {
|
|
500
|
+
clearInterval(watchdogTimer);
|
|
501
|
+
watchdogTimer = null;
|
|
454
502
|
}
|
|
455
503
|
}
|
|
456
504
|
|
|
@@ -488,6 +536,7 @@ function gracefulShutdown() {
|
|
|
488
536
|
|
|
489
537
|
log("收到关机指令,正在安全退出...", "OFF");
|
|
490
538
|
|
|
539
|
+
stopWatchdog();
|
|
491
540
|
stopHeartbeat();
|
|
492
541
|
if (reconnectTimer) {
|
|
493
542
|
clearTimeout(reconnectTimer);
|
|
@@ -528,11 +577,12 @@ export async function start() {
|
|
|
528
577
|
|
|
529
578
|
log("节点看门狗已启动,等待公网入口...", "ON");
|
|
530
579
|
|
|
531
|
-
// 等 9router
|
|
580
|
+
// 等 9router 起来后打开守护
|
|
532
581
|
const checkReady = setInterval(async () => {
|
|
533
582
|
if (_serverReady || (await isPortOpen(SERVER_PORT))) {
|
|
534
583
|
_serverReady = true;
|
|
535
584
|
clearInterval(checkReady);
|
|
585
|
+
startWatchdog();
|
|
536
586
|
log("9router 已就绪,节点可正常处理请求", "ON");
|
|
537
587
|
}
|
|
538
588
|
}, 3000);
|