@myassis/gateway 1.0.43 → 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.
@@ -503,31 +503,49 @@ async function updateService() {
503
503
  response.data.on('error', reject);
504
504
  });
505
505
  console.log('✅ 下载完成');
506
- // 生成延迟替换脚本,Windows 通过 nssm 管理服务并使用 execAsync 派发脚本
506
+ // 生成延迟替换脚本:Windows 已改为用户级进程(PID 文件 + Run key + launcher.ps1)
507
+ // 因此更新脚本不再依赖 nssm,而是按 PID 文件 kill 旧进程 -> 替换 exe -> 调 launcher.ps1 重启
507
508
  if (isWin) {
508
- const nssmPath = getNssmPath();
509
- if (!nssmPath || !fs_1.default.existsSync(nssmPath)) {
510
- throw new Error('未找到 nssm.exe,无法执行 Windows 服务更新');
511
- }
512
509
  const escapePowerShellString = (value) => value.replace(/'/g, "''");
513
510
  const updateScriptPath = path_1.default.join(os_1.default.tmpdir(), 'myassis-gateway-update.ps1');
514
511
  const psScript = [
515
512
  `$ErrorActionPreference = 'Stop'`,
516
- `$nssm = '${escapePowerShellString(nssmPath)}'`,
517
513
  `$serviceName = '${escapePowerShellString(exports.SERVICE_NAME)}'`,
518
514
  `$currentExe = '${escapePowerShellString(currentExe)}'`,
519
515
  `$tmpExe = '${escapePowerShellString(tmpExe)}'`,
520
516
  `$backupExe = '${escapePowerShellString(`${currentExe}.bak`)}'`,
517
+ `$pidFile = '${escapePowerShellString(GATEWAY_PID_FILE)}'`,
518
+ `$launcher = '${escapePowerShellString(GATEWAY_LAUNCHER_FILE)}'`,
521
519
  `$logPath = Join-Path $env:TEMP 'myassis-gateway-update.log'`,
522
520
  `$logDir = Split-Path -Parent $logPath`,
523
521
  `if (-not (Test-Path $logDir)) { New-Item -ItemType Directory -Path $logDir -Force | Out-Null }`,
524
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
+ `}`,
525
544
  `try {`,
526
545
  ` Write-UpdateLog 'update script started'`,
527
546
  ` Start-Sleep -Seconds 5`,
528
- ` Write-UpdateLog 'stopping service by nssm'`,
529
- ` & $nssm stop $serviceName | Out-Null`,
530
- ` Start-Sleep -Seconds 2`,
547
+ ` Stop-GatewayProcess`,
548
+ ` Start-Sleep -Seconds 1`,
531
549
  ` Write-UpdateLog 'waiting executable unlock'`,
532
550
  ` $moved = $false`,
533
551
  ` for ($i = 0; $i -lt 60; $i++) {`,
@@ -544,19 +562,18 @@ async function updateService() {
544
562
  ` Write-UpdateLog 'moving new executable'`,
545
563
  ` Move-Item $tmpExe $currentExe -Force`,
546
564
  ` Start-Sleep -Seconds 1`,
547
- ` Write-UpdateLog 'starting service by nssm'`,
548
- ` & $nssm start $serviceName | Out-Null`,
565
+ ` Start-GatewayProcess`,
549
566
  ` Remove-Item $backupExe -Force -ErrorAction SilentlyContinue`,
550
567
  ` Remove-Item $tmpExe -Force -ErrorAction SilentlyContinue`,
551
568
  ` Write-UpdateLog 'update script finished'`,
552
569
  `} catch {`,
553
570
  ` Write-UpdateLog \"update script failed: $($_.Exception.Message)\"`,
554
- ` try { & $nssm start $serviceName | Out-Null } catch { Write-UpdateLog \"restart service failed: $($_.Exception.Message)\" }`,
571
+ ` try { Start-GatewayProcess } catch { Write-UpdateLog \"restart gateway failed: $($_.Exception.Message)\" }`,
555
572
  ` throw`,
556
573
  `}`,
557
574
  ].join('\r\n');
558
575
  fs_1.default.writeFileSync(updateScriptPath, `\uFEFF${psScript}`, 'utf8');
559
- 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}\"`, {
560
577
  timeout: 10000,
561
578
  windowsHide: true
562
579
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@myassis/gateway",
3
- "version": "1.0.43",
3
+ "version": "1.0.44",
4
4
  "description": "我的助手 Gateway Service - 本地 AI 网关服务,支持认证、WebSocket 实时通信和任务调度",
5
5
  "main": "dist/index.js",
6
6
  "bin": {