@yeaft/webchat-agent 0.0.169 → 0.0.170

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.
Files changed (2) hide show
  1. package/connection.js +19 -11
  2. package/package.json +1 -1
package/connection.js CHANGED
@@ -425,7 +425,8 @@ async function handleMessage(msg) {
425
425
 
426
426
  if (isWindows) {
427
427
  // Windows: 进程持有文件锁,npm install 无法覆盖正在运行的模块文件 (EBUSY)
428
- // 策略:生成 detached bat 脚本,等当前进程退出后再 npm install
428
+ // 策略:生成 detached bat 脚本,等当前进程退出后 pm2 stop(防止 autorestart),
429
+ // 再 npm install
429
430
  const pid = process.pid;
430
431
  const configDir = getConfigDir();
431
432
  mkdirSync(configDir, { recursive: true });
@@ -448,6 +449,13 @@ async function handleMessage(msg) {
448
449
  '',
449
450
  ':: Redirect all output to log file',
450
451
  'echo [Upgrade] Started at %date% %time% > "%LOGFILE%"',
452
+ ];
453
+
454
+ // 先等原始 agent 进程退出。
455
+ // 不能在这之前 pm2 stop,因为 pm2 stop 会发 SIGTERM 杀当前进程,
456
+ // 导致 agent 还没来得及发 upgrade_agent_ack 就被杀掉。
457
+ // PID 退出后再由 bat 脚本执行 pm2 stop 阻止 autorestart。
458
+ batLines.push(
451
459
  ':WAIT_LOOP',
452
460
  'tasklist /FI "PID eq %PID%" 2>NUL | find /I "%PID%" >NUL',
453
461
  'if errorlevel 1 goto PID_EXITED',
@@ -456,17 +464,20 @@ async function handleMessage(msg) {
456
464
  ' echo [Upgrade] Timeout waiting for PID %PID% to exit after 60s >> "%LOGFILE%"',
457
465
  ' goto PID_EXITED',
458
466
  ')',
459
- 'timeout /T 2 /NOBREAK >NUL',
467
+ 'ping -n 3 127.0.0.1 >NUL',
460
468
  'goto WAIT_LOOP',
461
469
  ':PID_EXITED',
462
- ];
470
+ );
463
471
 
464
472
  if (isPm2) {
465
- // pm2 可能已自动重启旧代码(exit 触发),先 stop 释放文件锁
473
+ // PID已退出。但如果 agent pm2 stop 失败,PM2 的 autorestart 可能
474
+ // 已在 restart_delay(5s) 后启动了新实例。这里 pm2 stop 会停掉新实例,
475
+ // 确保 npm install 时无进程锁文件。
466
476
  batLines.push(
467
- 'echo [Upgrade] Stopping pm2 agent to release file locks... >> "%LOGFILE%"',
477
+ 'echo [Upgrade] Stopping pm2 to prevent autorestart... >> "%LOGFILE%"',
468
478
  'call pm2 stop yeaft-agent >> "%LOGFILE%" 2>&1',
469
- 'timeout /T 3 /NOBREAK >NUL',
479
+ ':: Wait for pm2 to fully terminate the process and release file locks',
480
+ 'ping -n 6 127.0.0.1 >NUL',
470
481
  );
471
482
  }
472
483
 
@@ -504,11 +515,6 @@ async function handleMessage(msg) {
504
515
  child.unref();
505
516
  console.log(`[Agent] Spawned upgrade script (PID wait for ${pid}, pm2=${isPm2}, dir=${installDir}): ${batPath}`);
506
517
  sendToServer({ type: 'upgrade_agent_ack', success: true, version: latestVersion, pendingRestart: true });
507
-
508
- if (isPm2) {
509
- // PM2 环境:先同步 pm2 stop 防止 autorestart 竞态,再 exit
510
- try { execSync('pm2 stop yeaft-agent', { timeout: 5000 }); } catch {}
511
- }
512
518
  } else {
513
519
  // Linux/macOS: 生成 detached shell 脚本,先停止服务再升级再启动
514
520
  // 避免在升级过程中 systemd 不断重启已删除的旧版本
@@ -611,6 +617,8 @@ async function handleMessage(msg) {
611
617
  }
612
618
 
613
619
  // 清理并退出,让升级脚本接管
620
+ // 注意: PM2 环境下不能在进程内调用 pm2 stop(会杀死自身进程,后续代码不会执行)
621
+ // 由 bat 脚本在检测到 PID 退出后执行 pm2 stop,阻止 autorestart
614
622
  setTimeout(() => {
615
623
  for (const [, term] of ctx.terminals) {
616
624
  if (term.pty) { try { term.pty.kill(); } catch {} }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yeaft/webchat-agent",
3
- "version": "0.0.169",
3
+ "version": "0.0.170",
4
4
  "description": "Remote agent for Yeaft WebChat — connects worker machines to the central server",
5
5
  "main": "index.js",
6
6
  "type": "module",