Fix Sentinel UI shutdown flow
This commit is contained in:
+83
-12
@@ -44,6 +44,51 @@ function Get-ListeningProcessId {
|
||||
return 0
|
||||
}
|
||||
|
||||
function Invoke-UiShutdown {
|
||||
param(
|
||||
[string]$Url,
|
||||
[string]$ShutdownToken
|
||||
)
|
||||
|
||||
if ([string]::IsNullOrWhiteSpace($Url) -or [string]::IsNullOrWhiteSpace($ShutdownToken)) {
|
||||
return $false
|
||||
}
|
||||
|
||||
try {
|
||||
$headers = @{
|
||||
"X-S1DL-Shutdown-Token" = $ShutdownToken
|
||||
}
|
||||
$shutdownUrl = $Url.TrimEnd("/") + "/api/shutdown"
|
||||
$null = Invoke-RestMethod -Uri $shutdownUrl -Method Post -Headers $headers -TimeoutSec 5 -ErrorAction Stop
|
||||
return $true
|
||||
} catch {
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
function Wait-ForUiShutdown {
|
||||
param(
|
||||
[int]$ProcessId,
|
||||
[int]$Port,
|
||||
[int]$TimeoutSeconds = 10
|
||||
)
|
||||
|
||||
$deadline = (Get-Date).AddSeconds($TimeoutSeconds)
|
||||
while ((Get-Date) -lt $deadline) {
|
||||
$running = Get-RunningProcess -Id $ProcessId
|
||||
$listenerProcessId = 0
|
||||
if ($Port -gt 0) {
|
||||
$listenerProcessId = Get-ListeningProcessId -Port $Port
|
||||
}
|
||||
if (-not $running -and $listenerProcessId -le 0) {
|
||||
return $true
|
||||
}
|
||||
Start-Sleep -Milliseconds 500
|
||||
}
|
||||
|
||||
return $false
|
||||
}
|
||||
|
||||
$projectRoot = Split-Path -Parent $PSCommandPath
|
||||
$statePath = Join-Path $projectRoot ".s1dl\web-ui-state.json"
|
||||
|
||||
@@ -53,23 +98,49 @@ if (-not (Test-Path -LiteralPath $statePath)) {
|
||||
}
|
||||
|
||||
$state = Get-Content -LiteralPath $statePath -Raw | ConvertFrom-Json
|
||||
$process = Get-RunningProcess -Id ([int]$state.pid)
|
||||
$stopped = $false
|
||||
|
||||
if ($process) {
|
||||
Write-Info "Stopping UI process $($process.Id)"
|
||||
Stop-Process -Id $process.Id -Force
|
||||
} else {
|
||||
$listenerProcessId = 0
|
||||
if ($state.port) {
|
||||
$listenerProcessId = Get-ListeningProcessId -Port ([int]$state.port)
|
||||
if ($state.url -and $state.shutdown_token) {
|
||||
Write-Info "Requesting UI shutdown via API"
|
||||
if (Invoke-UiShutdown -Url $state.url -ShutdownToken $state.shutdown_token) {
|
||||
$stopped = Wait-ForUiShutdown -ProcessId ([int]$state.pid) -Port ([int]$state.port)
|
||||
}
|
||||
if ($listenerProcessId -gt 0) {
|
||||
Write-Info "Stopping listener process $listenerProcessId"
|
||||
Stop-Process -Id $listenerProcessId -Force
|
||||
}
|
||||
|
||||
if (-not $stopped) {
|
||||
$process = Get-RunningProcess -Id ([int]$state.pid)
|
||||
|
||||
if ($process) {
|
||||
Write-Info "Stopping UI process $($process.Id)"
|
||||
try {
|
||||
Stop-Process -Id $process.Id -Force -ErrorAction Stop
|
||||
$stopped = $true
|
||||
} catch {
|
||||
throw "Failed to stop UI process $($process.Id). It may have been started from an elevated shell. Start or stop it with the same privilege level, or restart it with the updated launcher once so it can use API shutdown. $($_.Exception.Message)"
|
||||
}
|
||||
} else {
|
||||
Write-Info "Tracked process is not running."
|
||||
$listenerProcessId = 0
|
||||
if ($state.port) {
|
||||
$listenerProcessId = Get-ListeningProcessId -Port ([int]$state.port)
|
||||
}
|
||||
if ($listenerProcessId -gt 0) {
|
||||
Write-Info "Stopping listener process $listenerProcessId"
|
||||
try {
|
||||
Stop-Process -Id $listenerProcessId -Force -ErrorAction Stop
|
||||
$stopped = $true
|
||||
} catch {
|
||||
throw "Failed to stop UI listener process $listenerProcessId. It may have been started from an elevated shell. Start or stop it with the same privilege level, or restart it with the updated launcher once so it can use API shutdown. $($_.Exception.Message)"
|
||||
}
|
||||
} else {
|
||||
Write-Info "Tracked process is not running."
|
||||
$stopped = $true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (-not $stopped) {
|
||||
throw "UI shutdown did not complete."
|
||||
}
|
||||
|
||||
Remove-Item -LiteralPath $statePath -Force
|
||||
Write-Info "Done."
|
||||
|
||||
Reference in New Issue
Block a user