@yeaft/webchat-agent 0.1.25 → 0.1.27

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/cli.js CHANGED
@@ -185,7 +185,7 @@ function upgradeWindows(latestVersion) {
185
185
  'echo [Upgrade] Waiting for CLI process (PID %PID%) to exit... >> "%LOGFILE%"',
186
186
  '',
187
187
  ':WAIT_LOOP',
188
- 'tasklist /FI "PID eq %PID%" 2>NUL | find /I "%PID%" >NUL',
188
+ 'tasklist /FI "PID eq %PID%" 2>NUL | findstr /I "%PID%" >NUL',
189
189
  'if errorlevel 1 goto PID_EXITED',
190
190
  'set /A COUNT+=1',
191
191
  'if %COUNT% GEQ %MAX_WAIT% (',
@@ -1,4 +1,4 @@
1
- import { execFile, spawn } from 'child_process';
1
+ import { execFile, execFileSync, spawn } from 'child_process';
2
2
  import { writeFileSync, mkdirSync, existsSync, cpSync } from 'fs';
3
3
  import { join, dirname } from 'path';
4
4
  import { fileURLToPath } from 'url';
@@ -8,6 +8,8 @@ import { getConfigDir } from '../service.js';
8
8
  import { sendToServer } from './buffer.js';
9
9
  import { stopAgentHeartbeat } from './heartbeat.js';
10
10
 
11
+ const PM2_APP_NAME = 'yeaft-agent';
12
+
11
13
  // Shared cleanup logic for restart/upgrade
12
14
  function cleanupAndExit(exitCode) {
13
15
  setTimeout(() => {
@@ -87,6 +89,18 @@ export async function handleUpgradeAgent() {
87
89
  spawnUnixUpgradeScript(pkgName, installDir, isGlobalInstall, latestVersion);
88
90
  }
89
91
 
92
+ // On PM2: delete the app BEFORE exiting so PM2 won't auto-restart the old version.
93
+ // The upgrade script will re-register it with `pm2 start <ecosystem>` after replacing files.
94
+ const isPm2 = !!process.env.pm_id;
95
+ if (isPm2) {
96
+ try {
97
+ execFileSync('pm2', ['delete', PM2_APP_NAME], { shell: true, stdio: 'pipe' });
98
+ console.log(`[Agent] PM2 app deleted to prevent auto-restart during upgrade`);
99
+ } catch {
100
+ console.log(`[Agent] PM2 delete skipped (app may not be registered)`);
101
+ }
102
+ }
103
+
90
104
  // 清理并退出,让升级脚本接管
91
105
  cleanupAndExit(0);
92
106
  } catch (e) {
@@ -105,6 +119,7 @@ function spawnWindowsUpgradeScript(pkgName, installDir, isGlobalInstall, latestV
105
119
  const logPath = join(logDir, 'upgrade.log');
106
120
  const isPm2 = !!process.env.pm_id;
107
121
  const installDirWin = installDir.replace(/\//g, '\\');
122
+ const ecoPath = join(configDir, 'ecosystem.config.cjs').replace(/\//g, '\\');
108
123
 
109
124
  // Copy upgrade-worker-template.js to config dir (runs as CJS there, away from ESM context)
110
125
  const thisDir = dirname(fileURLToPath(import.meta.url));
@@ -134,9 +149,10 @@ function spawnWindowsUpgradeScript(pkgName, installDir, isGlobalInstall, latestV
134
149
  'echo [Upgrade] Started at %date% %time% > "%LOGFILE%"',
135
150
  ];
136
151
 
152
+ // Wait for old process to exit (PM2 already deleted before exit, so no auto-restart race)
137
153
  batLines.push(
138
154
  ':WAIT_LOOP',
139
- 'tasklist /FI "PID eq %PID%" 2>NUL | find /I "%PID%" >NUL',
155
+ 'tasklist /FI "PID eq %PID%" 2>NUL | findstr /I "%PID%" >NUL',
140
156
  'if errorlevel 1 goto PID_EXITED',
141
157
  'set /A COUNT+=1',
142
158
  'if %COUNT% GEQ %MAX_WAIT% (',
@@ -148,14 +164,12 @@ function spawnWindowsUpgradeScript(pkgName, installDir, isGlobalInstall, latestV
148
164
  ':PID_EXITED',
149
165
  );
150
166
 
151
- if (isPm2) {
152
- batLines.push(
153
- 'echo [Upgrade] Stopping pm2 to prevent autorestart... >> "%LOGFILE%"',
154
- 'call pm2 stop yeaft-agent >> "%LOGFILE%" 2>&1',
155
- ':: Wait for pm2 to fully terminate the process and release file locks',
156
- 'ping -n 6 127.0.0.1 >NUL',
157
- );
158
- }
167
+ // No need to pm2 stop — PM2 app was already deleted before process exit.
168
+ // Wait a moment for file locks to release.
169
+ batLines.push(
170
+ 'echo [Upgrade] Process exited, waiting for file locks... >> "%LOGFILE%"',
171
+ 'ping -n 3 127.0.0.1 >NUL',
172
+ );
159
173
 
160
174
  // Use Node.js worker for file-level upgrade (avoids EBUSY on directory rename)
161
175
  batLines.push(
@@ -171,9 +185,11 @@ function spawnWindowsUpgradeScript(pkgName, installDir, isGlobalInstall, latestV
171
185
  batLines.push(':CLEANUP');
172
186
 
173
187
  if (isPm2) {
188
+ // Re-register and start via ecosystem config (PM2 app was deleted pre-exit)
174
189
  batLines.push(
175
- 'echo [Upgrade] Starting agent via pm2... >> "%LOGFILE%"',
176
- 'call pm2 start yeaft-agent >> "%LOGFILE%" 2>&1',
190
+ 'echo [Upgrade] Re-registering agent via pm2... >> "%LOGFILE%"',
191
+ `call pm2 start "${ecoPath}" >> "%LOGFILE%" 2>&1`,
192
+ 'call pm2 save >> "%LOGFILE%" 2>&1',
177
193
  );
178
194
  }
179
195
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yeaft/webchat-agent",
3
- "version": "0.1.25",
3
+ "version": "0.1.27",
4
4
  "description": "Remote agent for Yeaft WebChat — connects worker machines to the central server",
5
5
  "main": "index.js",
6
6
  "type": "module",