@myassis/gateway 1.0.42 → 1.0.44
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/dist/services/ServiceManager.js +49 -43
- package/package.json +1 -1
|
@@ -56,13 +56,9 @@ function getNssmPath() {
|
|
|
56
56
|
}
|
|
57
57
|
// ─── Windows 实现 ───────────────────────────────────────────
|
|
58
58
|
async function queryServiceWindows() {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
catch {
|
|
64
|
-
return { installed: false, running: false };
|
|
65
|
-
}
|
|
59
|
+
const inRunKey = isRegisteredInRunKey();
|
|
60
|
+
const running = isGatewayRunning();
|
|
61
|
+
return { installed: inRunKey, running };
|
|
66
62
|
}
|
|
67
63
|
// ─── 用户级进程管理 ─────────────────────────────────────────
|
|
68
64
|
/**
|
|
@@ -137,18 +133,11 @@ function writeGatewayLauncherScript(exePath, scriptPath, workDir) {
|
|
|
137
133
|
"$exe = '" + exePath.replace(/\\/g, '\\\\') + "'",
|
|
138
134
|
"$workDir = '" + workDir.replace(/\\/g, '\\\\') + "'",
|
|
139
135
|
"$pidFile = '" + GATEWAY_PID_FILE.replace(/\\/g, '\\\\') + "'",
|
|
140
|
-
"$port = 19090",
|
|
141
|
-
"$svcPort = 19091",
|
|
142
|
-
"",
|
|
143
|
-
"# 防止重复启动(检查端口)",
|
|
144
|
-
"$listener = [System.Net.Sockets.TcpListener]::Start($port)",
|
|
145
|
-
"$listener.Stop()",
|
|
146
136
|
"",
|
|
147
|
-
"# 启动 Gateway
|
|
137
|
+
"# 启动 Gateway(使用 CLI start 命令,监听默认端口 3001)",
|
|
148
138
|
"$psi = New-Object System.Diagnostics.ProcessStartInfo",
|
|
149
139
|
"$psi.FileName = $exe",
|
|
150
140
|
"$psi.WorkingDirectory = $workDir",
|
|
151
|
-
"$psi.Arguments = '--gateway --port ' + $port + ' --service-port ' + $svcPort",
|
|
152
141
|
"$psi.UseShellExecute = $false",
|
|
153
142
|
"$psi.RedirectStandardOutput = $true",
|
|
154
143
|
"$psi.RedirectStandardError = $true",
|
|
@@ -299,21 +288,21 @@ async function installLinux() {
|
|
|
299
288
|
if (installed)
|
|
300
289
|
await stopService();
|
|
301
290
|
const execStart = isPkg ? exe : `${exe} ${script}`;
|
|
302
|
-
const unitContent = `[Unit]
|
|
303
|
-
Description=${exports.SERVICE_DISPLAY_NAME}
|
|
304
|
-
After=network.target
|
|
305
|
-
|
|
306
|
-
[Service]
|
|
307
|
-
Type=simple
|
|
308
|
-
User=${process.env.USER || 'root'}
|
|
309
|
-
WorkingDirectory=${workDir}
|
|
310
|
-
ExecStart=${execStart}
|
|
311
|
-
Restart=always
|
|
312
|
-
RestartSec=5
|
|
313
|
-
Environment=NODE_ENV=production
|
|
314
|
-
|
|
315
|
-
[Install]
|
|
316
|
-
WantedBy=multi-user.target
|
|
291
|
+
const unitContent = `[Unit]
|
|
292
|
+
Description=${exports.SERVICE_DISPLAY_NAME}
|
|
293
|
+
After=network.target
|
|
294
|
+
|
|
295
|
+
[Service]
|
|
296
|
+
Type=simple
|
|
297
|
+
User=${process.env.USER || 'root'}
|
|
298
|
+
WorkingDirectory=${workDir}
|
|
299
|
+
ExecStart=${execStart}
|
|
300
|
+
Restart=always
|
|
301
|
+
RestartSec=5
|
|
302
|
+
Environment=NODE_ENV=production
|
|
303
|
+
|
|
304
|
+
[Install]
|
|
305
|
+
WantedBy=multi-user.target
|
|
317
306
|
`;
|
|
318
307
|
await fs_1.default.promises.writeFile('/tmp/myassis-gateway.service', unitContent, 'utf8');
|
|
319
308
|
await execAsync('cp /tmp/myassis-gateway.service /etc/systemd/system/myassis-gateway.service', { timeout: 10000 });
|
|
@@ -514,31 +503,49 @@ async function updateService() {
|
|
|
514
503
|
response.data.on('error', reject);
|
|
515
504
|
});
|
|
516
505
|
console.log('✅ 下载完成');
|
|
517
|
-
//
|
|
506
|
+
// 生成延迟替换脚本:Windows 已改为用户级进程(PID 文件 + Run key + launcher.ps1)
|
|
507
|
+
// 因此更新脚本不再依赖 nssm,而是按 PID 文件 kill 旧进程 -> 替换 exe -> 调 launcher.ps1 重启
|
|
518
508
|
if (isWin) {
|
|
519
|
-
const nssmPath = getNssmPath();
|
|
520
|
-
if (!nssmPath || !fs_1.default.existsSync(nssmPath)) {
|
|
521
|
-
throw new Error('未找到 nssm.exe,无法执行 Windows 服务更新');
|
|
522
|
-
}
|
|
523
509
|
const escapePowerShellString = (value) => value.replace(/'/g, "''");
|
|
524
510
|
const updateScriptPath = path_1.default.join(os_1.default.tmpdir(), 'myassis-gateway-update.ps1');
|
|
525
511
|
const psScript = [
|
|
526
512
|
`$ErrorActionPreference = 'Stop'`,
|
|
527
|
-
`$nssm = '${escapePowerShellString(nssmPath)}'`,
|
|
528
513
|
`$serviceName = '${escapePowerShellString(exports.SERVICE_NAME)}'`,
|
|
529
514
|
`$currentExe = '${escapePowerShellString(currentExe)}'`,
|
|
530
515
|
`$tmpExe = '${escapePowerShellString(tmpExe)}'`,
|
|
531
516
|
`$backupExe = '${escapePowerShellString(`${currentExe}.bak`)}'`,
|
|
517
|
+
`$pidFile = '${escapePowerShellString(GATEWAY_PID_FILE)}'`,
|
|
518
|
+
`$launcher = '${escapePowerShellString(GATEWAY_LAUNCHER_FILE)}'`,
|
|
532
519
|
`$logPath = Join-Path $env:TEMP 'myassis-gateway-update.log'`,
|
|
533
520
|
`$logDir = Split-Path -Parent $logPath`,
|
|
534
521
|
`if (-not (Test-Path $logDir)) { New-Item -ItemType Directory -Path $logDir -Force | Out-Null }`,
|
|
535
522
|
`function Write-UpdateLog { param([string]$Message) Add-Content -Path $logPath -Encoding UTF8 -Value \"$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') $Message\" }`,
|
|
523
|
+
`function Stop-GatewayProcess {`,
|
|
524
|
+
` if (-not (Test-Path $pidFile)) { Write-UpdateLog 'pid file not found, skip stop'; return }`,
|
|
525
|
+
` try {`,
|
|
526
|
+
` $pidValue = (Get-Content $pidFile -Raw).Trim()`,
|
|
527
|
+
` if (-not $pidValue) { return }`,
|
|
528
|
+
` $pidInt = [int]$pidValue`,
|
|
529
|
+
` Write-UpdateLog \"stopping gateway process pid=$pidInt\"`,
|
|
530
|
+
` try { Stop-Process -Id $pidInt -Force -ErrorAction Stop } catch { Write-UpdateLog \"stop-process failed: $($_.Exception.Message)\" }`,
|
|
531
|
+
` for ($w = 0; $w -lt 30; $w++) {`,
|
|
532
|
+
` $p = Get-Process -Id $pidInt -ErrorAction SilentlyContinue`,
|
|
533
|
+
` if (-not $p) { break }`,
|
|
534
|
+
` Start-Sleep -Milliseconds 500`,
|
|
535
|
+
` }`,
|
|
536
|
+
` Remove-Item $pidFile -Force -ErrorAction SilentlyContinue`,
|
|
537
|
+
` } catch { Write-UpdateLog \"stop gateway error: $($_.Exception.Message)\" }`,
|
|
538
|
+
`}`,
|
|
539
|
+
`function Start-GatewayProcess {`,
|
|
540
|
+
` if (-not (Test-Path $launcher)) { Write-UpdateLog \"launcher script not found: $launcher\"; return }`,
|
|
541
|
+
` Write-UpdateLog 'starting gateway via launcher.ps1'`,
|
|
542
|
+
` Start-Process -FilePath 'powershell.exe' -ArgumentList @('-NoProfile','-ExecutionPolicy','Bypass','-WindowStyle','Hidden','-File',$launcher) -WindowStyle Hidden`,
|
|
543
|
+
`}`,
|
|
536
544
|
`try {`,
|
|
537
545
|
` Write-UpdateLog 'update script started'`,
|
|
538
546
|
` Start-Sleep -Seconds 5`,
|
|
539
|
-
`
|
|
540
|
-
`
|
|
541
|
-
` Start-Sleep -Seconds 2`,
|
|
547
|
+
` Stop-GatewayProcess`,
|
|
548
|
+
` Start-Sleep -Seconds 1`,
|
|
542
549
|
` Write-UpdateLog 'waiting executable unlock'`,
|
|
543
550
|
` $moved = $false`,
|
|
544
551
|
` for ($i = 0; $i -lt 60; $i++) {`,
|
|
@@ -555,19 +562,18 @@ async function updateService() {
|
|
|
555
562
|
` Write-UpdateLog 'moving new executable'`,
|
|
556
563
|
` Move-Item $tmpExe $currentExe -Force`,
|
|
557
564
|
` Start-Sleep -Seconds 1`,
|
|
558
|
-
`
|
|
559
|
-
` & $nssm start $serviceName | Out-Null`,
|
|
565
|
+
` Start-GatewayProcess`,
|
|
560
566
|
` Remove-Item $backupExe -Force -ErrorAction SilentlyContinue`,
|
|
561
567
|
` Remove-Item $tmpExe -Force -ErrorAction SilentlyContinue`,
|
|
562
568
|
` Write-UpdateLog 'update script finished'`,
|
|
563
569
|
`} catch {`,
|
|
564
570
|
` Write-UpdateLog \"update script failed: $($_.Exception.Message)\"`,
|
|
565
|
-
` try {
|
|
571
|
+
` try { Start-GatewayProcess } catch { Write-UpdateLog \"restart gateway failed: $($_.Exception.Message)\" }`,
|
|
566
572
|
` throw`,
|
|
567
573
|
`}`,
|
|
568
574
|
].join('\r\n');
|
|
569
575
|
fs_1.default.writeFileSync(updateScriptPath, `\uFEFF${psScript}`, 'utf8');
|
|
570
|
-
await execAsync(`cmd.exe /c start \"\" powershell.exe -NoProfile -ExecutionPolicy Bypass -File \"${updateScriptPath}\"`, {
|
|
576
|
+
await execAsync(`cmd.exe /c start \"\" powershell.exe -NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File \"${updateScriptPath}\"`, {
|
|
571
577
|
timeout: 10000,
|
|
572
578
|
windowsHide: true
|
|
573
579
|
});
|