[CmdletBinding()] param([string]$InstallRoot = (Split-Path -Parent $PSScriptRoot), [string]$JavaHome) . (Join-Path $PSScriptRoot 'Common.ps1') $root = [System.IO.Path]::GetFullPath($InstallRoot) $processFile = Join-Path $root 'service-process.json' $deployment = Get-Content -LiteralPath (Join-Path $root 'deployment.json') -Raw | ConvertFrom-Json if (Test-Path -LiteralPath $processFile) { $record = Get-Content -LiteralPath $processFile -Raw | ConvertFrom-Json if (Get-XtrhOwnedProcess -InstallRoot $root -Record $record) { Write-Output '本部署实例已在运行。' return } } $java = Get-XtrhJava -JavaHome $JavaHome $jar = Join-Path $root 'xtrh-services.jar' if (-not (Test-Path -LiteralPath $jar)) { throw '未找到部署 JAR。' } $arguments = @('-Djava.awt.headless=true', '-jar', ('"' + $jar + '"'), '--spring.config.additional-location=file:./config/service.local.properties') $process = Start-Process -FilePath $java -ArgumentList $arguments -WorkingDirectory $root -WindowStyle Hidden -PassThru ` -RedirectStandardOutput (Join-Path $root 'logs/stdout.log') -RedirectStandardError (Join-Path $root 'logs/stderr.log') $record = @{pid = $process.Id; startTicks = $process.StartTime.ToUniversalTime().Ticks.ToString(); javaPath = $java; jarPath = $jar} Write-XtrhText -Path $processFile -Content ($record | ConvertTo-Json) $ready = $false $deadline = [DateTime]::UtcNow.AddSeconds(40) while ([DateTime]::UtcNow -lt $deadline) { $process.Refresh() if ($process.HasExited) { break } try { $health = Invoke-RestMethod -Uri ('http://127.0.0.1:' + $deployment.port + '/health') -TimeoutSec 1 $identityFile = Join-Path $root 'data/identity.txt' if ((Test-Path -LiteralPath $identityFile) -and $health.service -eq 'xtrh-services' -and $health.role -eq $deployment.role -and $health.serviceId -eq (Get-Content -LiteralPath $identityFile -Raw).Trim()) { $ready = $true; break } } catch { } Start-Sleep -Milliseconds 500 } if (-not $ready) { $owned = Get-XtrhOwnedProcess -InstallRoot $root -Record $record if ($owned) { Stop-Process -Id $owned.Id -Force } throw ('服务未通过启动校验;请查看 ' + (Join-Path $root 'logs')) } Write-Output ('服务已启动并通过身份校验:' + $deployment.role + ',端口 ' + $deployment.port)