mirror of
https://github.com/hansjone/netx.git
synced 2026-10-09 03:10:46 +08:00
fix(scripts): harden stop_netx and tweak start_netx
- Add timeout around Get-NetTCPConnection to avoid hangs; filter invalid PIDs; exit 0 on completion. - Minor start_netx adjustments. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
27c1826fc5
commit
d48cd000d3
2 changed files with 46 additions and 14 deletions
|
|
@ -91,6 +91,8 @@ if ($Background) {
|
||||||
Write-Host "Log = $webLogFile"
|
Write-Host "Log = $webLogFile"
|
||||||
Write-Host "Err = $webErrFile"
|
Write-Host "Err = $webErrFile"
|
||||||
}
|
}
|
||||||
|
Write-Host ""
|
||||||
|
Write-Host "==> Background services started; this script exits (API/web keep running)." -ForegroundColor Cyan
|
||||||
exit 0
|
exit 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,34 @@ $runDir = Join-Path $PSScriptRoot ".run"
|
||||||
$pidFile = Join-Path $runDir "netx.pid"
|
$pidFile = Join-Path $runDir "netx.pid"
|
||||||
$webPidFile = Join-Path $runDir "web.pid"
|
$webPidFile = Join-Path $runDir "web.pid"
|
||||||
|
|
||||||
|
function Get-ListenPids {
|
||||||
|
param([int]$LocalPort)
|
||||||
|
$ids = [System.Collections.Generic.HashSet[int]]::new()
|
||||||
|
$job = $null
|
||||||
|
try {
|
||||||
|
$job = Start-Job -ScriptBlock {
|
||||||
|
param($pt)
|
||||||
|
Get-NetTCPConnection -LocalPort $pt -State Listen -ErrorAction SilentlyContinue |
|
||||||
|
Select-Object -ExpandProperty OwningProcess -Unique
|
||||||
|
} -ArgumentList $LocalPort
|
||||||
|
$null = Wait-Job $job -Timeout 5
|
||||||
|
foreach ($x in @(Receive-Job $job -ErrorAction SilentlyContinue)) {
|
||||||
|
try {
|
||||||
|
$n = [int]$x
|
||||||
|
if ($n -gt 4) { [void]$ids.Add($n) }
|
||||||
|
} catch {}
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
Write-Host "[WARN] port query failed for $LocalPort : $($_.Exception.Message)"
|
||||||
|
} finally {
|
||||||
|
if ($job) {
|
||||||
|
Stop-Job $job -Force -ErrorAction SilentlyContinue
|
||||||
|
Remove-Job $job -Force -ErrorAction SilentlyContinue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@($ids)
|
||||||
|
}
|
||||||
|
|
||||||
Write-Host "==> Stopping netx"
|
Write-Host "==> Stopping netx"
|
||||||
|
|
||||||
if (Test-Path $pidFile) {
|
if (Test-Path $pidFile) {
|
||||||
|
|
@ -44,33 +72,35 @@ if (Test-Path $webPidFile) {
|
||||||
Remove-Item -Path $webPidFile -Force -ErrorAction SilentlyContinue
|
Remove-Item -Path $webPidFile -Force -ErrorAction SilentlyContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
$listeners = Get-NetTCPConnection -LocalPort $Port -State Listen -ErrorAction SilentlyContinue
|
$owningApi = @(Get-ListenPids -LocalPort $Port)
|
||||||
if ($listeners) {
|
if ($owningApi.Count -gt 0) {
|
||||||
$owning = $listeners | Select-Object -ExpandProperty OwningProcess -Unique
|
foreach ($procId in $owningApi) {
|
||||||
foreach ($p in $owning) {
|
if ($procId -eq $PID) { continue }
|
||||||
try {
|
try {
|
||||||
Stop-Process -Id $p -Force:$Force -ErrorAction Stop
|
Stop-Process -Id $procId -Force:$Force -ErrorAction Stop
|
||||||
Write-Host "Stopped by port PID=$p"
|
Write-Host "Stopped by port PID=$procId"
|
||||||
} catch {
|
} catch {
|
||||||
Write-Host "[WARN] Failed to stop PID=$p by port"
|
Write-Host "[WARN] Failed to stop PID=$procId by port"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Write-Host "[INFO] No listener found on port $Port"
|
Write-Host "[INFO] No listener found on port $Port"
|
||||||
}
|
}
|
||||||
|
|
||||||
$webListeners = Get-NetTCPConnection -LocalPort $WebPort -State Listen -ErrorAction SilentlyContinue
|
$owningWeb = @(Get-ListenPids -LocalPort $WebPort)
|
||||||
if ($webListeners) {
|
if ($owningWeb.Count -gt 0) {
|
||||||
$webOwning = $webListeners | Select-Object -ExpandProperty OwningProcess -Unique
|
foreach ($procId in $owningWeb) {
|
||||||
foreach ($p in $webOwning) {
|
if ($procId -eq $PID) { continue }
|
||||||
try {
|
try {
|
||||||
Stop-Process -Id $p -Force:$Force -ErrorAction Stop
|
Stop-Process -Id $procId -Force:$Force -ErrorAction Stop
|
||||||
Write-Host "Stopped web by port PID=$p"
|
Write-Host "Stopped web by port PID=$procId"
|
||||||
} catch {
|
} catch {
|
||||||
Write-Host "[WARN] Failed to stop web PID=$p by port"
|
Write-Host "[WARN] Failed to stop web PID=$procId by port"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Write-Host "[INFO] No listener found on web port $WebPort"
|
Write-Host "[INFO] No listener found on web port $WebPort"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Write-Host "==> netx stop finished" -ForegroundColor Green
|
||||||
|
exit 0
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue