@yeaft/webchat-agent 0.0.135 → 0.0.136

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 +27 -12
  2. package/package.json +1 -1
package/connection.js CHANGED
@@ -420,7 +420,10 @@ async function handleMessage(msg) {
420
420
  const pid = process.pid;
421
421
  const configDir = getConfigDir();
422
422
  mkdirSync(configDir, { recursive: true });
423
+ const logDir = join(configDir, 'logs');
424
+ mkdirSync(logDir, { recursive: true });
423
425
  const batPath = join(configDir, 'upgrade.bat');
426
+ const logPath = join(logDir, 'upgrade.log');
424
427
  const isPm2 = !!process.env.pm_id;
425
428
  const installDirWin = installDir.replace(/\//g, '\\');
426
429
 
@@ -430,14 +433,18 @@ async function handleMessage(msg) {
430
433
  `set PID=${pid}`,
431
434
  `set PKG=${pkgName}@latest`,
432
435
  `set INSTALL_DIR=${installDirWin}`,
436
+ `set LOGFILE=${logPath}`,
433
437
  `set MAX_WAIT=30`,
434
438
  `set COUNT=0`,
439
+ '',
440
+ ':: Redirect all output to log file',
441
+ 'echo [Upgrade] Started at %date% %time% > "%LOGFILE%"',
435
442
  ':WAIT_LOOP',
436
443
  'tasklist /FI "PID eq %PID%" 2>NUL | find /I "%PID%" >NUL',
437
444
  'if errorlevel 1 goto PID_EXITED',
438
445
  'set /A COUNT+=1',
439
446
  'if %COUNT% GEQ %MAX_WAIT% (',
440
- ' echo [Upgrade] Timeout waiting for PID %PID% to exit after 60s',
447
+ ' echo [Upgrade] Timeout waiting for PID %PID% to exit after 60s >> "%LOGFILE%"',
441
448
  ' goto PID_EXITED',
442
449
  ')',
443
450
  'timeout /T 2 /NOBREAK >NUL',
@@ -446,10 +453,10 @@ async function handleMessage(msg) {
446
453
  ];
447
454
 
448
455
  if (isPm2) {
449
- // pm2 可能已自动重启旧代码(exit 1 触发),先 stop 释放文件锁
456
+ // pm2 可能已自动重启旧代码(exit 触发),先 stop 释放文件锁
450
457
  batLines.push(
451
- 'echo [Upgrade] Stopping pm2 agent to release file locks...',
452
- 'call pm2 stop yeaft-agent 2>NUL',
458
+ 'echo [Upgrade] Stopping pm2 agent to release file locks... >> "%LOGFILE%"',
459
+ 'call pm2 stop yeaft-agent >> "%LOGFILE%" 2>&1',
453
460
  'timeout /T 3 /NOBREAK >NUL',
454
461
  );
455
462
  }
@@ -459,23 +466,26 @@ async function handleMessage(msg) {
459
466
  : 'cd /d "%INSTALL_DIR%" && call npm install %PKG%';
460
467
 
461
468
  batLines.push(
462
- 'echo [Upgrade] Installing %PKG%...',
463
- npmBatCmd,
469
+ 'echo [Upgrade] Installing %PKG%... >> "%LOGFILE%"',
470
+ `${npmBatCmd} >> "%LOGFILE%" 2>&1`,
464
471
  'if errorlevel 1 (',
465
- ' echo [Upgrade] npm install failed with exit code %errorlevel%',
466
- ') else (',
467
- ' echo [Upgrade] Successfully installed %PKG%',
472
+ ' echo [Upgrade] npm install failed with exit code %errorlevel% >> "%LOGFILE%"',
473
+ ' goto CLEANUP',
468
474
  ')',
475
+ 'echo [Upgrade] Successfully installed %PKG% >> "%LOGFILE%"',
469
476
  );
470
477
 
471
478
  if (isPm2) {
472
479
  batLines.push(
473
- 'echo [Upgrade] Starting agent via pm2...',
474
- 'call pm2 start yeaft-agent',
480
+ 'echo [Upgrade] Starting agent via pm2... >> "%LOGFILE%"',
481
+ 'call pm2 start yeaft-agent >> "%LOGFILE%" 2>&1',
475
482
  );
476
483
  }
477
484
 
478
- batLines.push(`del /F /Q "${batPath}"`);
485
+ batLines.push(
486
+ ':CLEANUP',
487
+ `del /F /Q "${batPath}"`,
488
+ );
479
489
 
480
490
  writeFileSync(batPath, batLines.join('\r\n'));
481
491
  const child = spawn('cmd.exe', ['/c', batPath], {
@@ -486,6 +496,11 @@ async function handleMessage(msg) {
486
496
  child.unref();
487
497
  console.log(`[Agent] Spawned upgrade script (PID wait for ${pid}, pm2=${isPm2}, dir=${installDir}): ${batPath}`);
488
498
  sendToServer({ type: 'upgrade_agent_ack', success: true, version: latestVersion, pendingRestart: true });
499
+
500
+ if (isPm2) {
501
+ // PM2 环境:先同步 pm2 stop 防止 autorestart 竞态,再 exit
502
+ try { execSync('pm2 stop yeaft-agent', { timeout: 5000 }); } catch {}
503
+ }
489
504
  } else {
490
505
  // Linux/macOS: 生成 detached shell 脚本,先停止服务再升级再启动
491
506
  // 避免在升级过程中 systemd 不断重启已删除的旧版本
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yeaft/webchat-agent",
3
- "version": "0.0.135",
3
+ "version": "0.0.136",
4
4
  "description": "Remote agent for Yeaft WebChat — connects worker machines to the central server",
5
5
  "main": "index.js",
6
6
  "type": "module",