$ErrorActionPreference = 'Stop' $root = Split-Path -Parent $PSScriptRoot Set-Location -LiteralPath $root if (-not (Test-Path '.run\server.info')) { Write-Host "" Write-Host " [yt-scraper] no hay servidor registrado." -ForegroundColor Yellow Write-Host "" exit 0 } $firstLine = (Get-Content '.run\server.info' -TotalCount 1) $parts = $firstLine -split '\s+' $port = $parts[0] $pidv = $parts[1] Write-Host "" Write-Host " [yt-scraper] deteniendo servidor (PID $pidv, puerto $port)..." -ForegroundColor Cyan if ($pidv -and $pidv -ne 'pending') { # Kill the process tree (uvicorn + any workers). try { Stop-Process -Id ([int]$pidv) -Force -ErrorAction Stop } catch { # fallback: taskkill & taskkill /PID $pidv /T /F 2>$null | Out-Null } } # Confirm the port is freed. $freed = $false for ($i = 0; $i -lt 10; $i++) { try { Invoke-WebRequest -Uri "http://localhost:$port/healthz" -UseBasicParsing -TimeoutSec 1 | Out-Null } catch { $freed = $true; break } Start-Sleep -Milliseconds 400 } Remove-Item '.run\server.info' -Force -ErrorAction SilentlyContinue if ($freed) { Write-Host " [ok] puerto liberado, servidor detenido." -ForegroundColor Green } else { Write-Host " [warn] el puerto $port sigue ocupado; revisa procesos python." -ForegroundColor Yellow } Write-Host ""