@wendongfly/myhi 1.3.51 → 1.3.52

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/bin/daemon.js CHANGED
@@ -17,6 +17,36 @@ const pidFile = join(configDir, 'daemon.pid');
17
17
  const serverEntry = join(__dirname, '../dist/index.js');
18
18
 
19
19
  mkdirSync(configDir, { recursive: true });
20
+
21
+ // 单例守卫:如果已有活 daemon,先 SIGTERM 它再启动。否则双 daemon 会让各自的
22
+ // server child 因互读 server.pid 而互杀,触发"连续崩溃 5 次"熔断(pc41 1.3.51 升级踩到)
23
+ (function killOldDaemon() {
24
+ let oldPid;
25
+ try { oldPid = parseInt(readFileSync(pidFile, 'utf8').trim(), 10); } catch { return; }
26
+ if (!oldPid || oldPid === process.pid) return;
27
+ let alive = false;
28
+ try { process.kill(oldPid, 0); alive = true; } catch {}
29
+ if (!alive) return;
30
+ process.stderr.write(`[daemon] 检测到已运行的 daemon (PID ${oldPid}),先停止...\n`);
31
+ try {
32
+ if (process.platform === 'win32') execSync(`taskkill /PID ${oldPid} /F /T`, { stdio: 'pipe', timeout: 5000 });
33
+ else process.kill(oldPid, 'SIGTERM');
34
+ } catch {}
35
+ // 等最多 5 秒
36
+ const deadline = Date.now() + 5000;
37
+ while (Date.now() < deadline) {
38
+ try { process.kill(oldPid, 0); } catch { return; }
39
+ const end = Date.now() + 200;
40
+ while (Date.now() < end) {} // busy-wait, 此处无事件循环可用
41
+ }
42
+ let stillAlive = false;
43
+ try { process.kill(oldPid, 0); stillAlive = true; } catch {}
44
+ if (stillAlive) {
45
+ process.stderr.write(`[daemon] 无法停止旧 daemon (PID ${oldPid}),本进程退出避免冲突\n`);
46
+ process.exit(1);
47
+ }
48
+ })();
49
+
20
50
  writeFileSync(pidFile, String(process.pid));
21
51
 
22
52
  let child = null;
@@ -35,7 +65,7 @@ function startServer() {
35
65
  log('启动 server 子进程...');
36
66
  child = fork(serverEntry, process.argv.slice(2), {
37
67
  stdio: ['ignore', 'inherit', 'inherit', 'ipc'],
38
- env: { ...process.env, MYHI_DAEMON: '1' },
68
+ env: { ...process.env, MYHI_DAEMON: '1', MYHI_DAEMON_PID: String(process.pid) },
39
69
  windowsHide: true,
40
70
  });
41
71