AdvisoryBuilderWorkshop / scripts /restart-api.ps1
NeonClary's picture
Deploy tutorial feature and recent fixes
d8808e7 verified
Raw
History Blame Contribute Delete
1.55 kB
# Restart the FastAPI service (Docker Compose "api" service, or a local process on port 8000).
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$root = Split-Path -Parent $PSScriptRoot
Set-Location $root
function Test-DockerCompose {
$docker = Get-Command docker -ErrorAction SilentlyContinue
if (-not $docker) { return $false }
try {
docker compose version 2>$null | Out-Null
return $true
} catch { return $false }
}
if (Test-DockerCompose) {
Write-Host 'Restarting Docker Compose service: api ...'
docker compose restart api
Write-Host 'Done. API should be at http://localhost:8000 (check /api/health).'
exit 0
}
Write-Host 'Docker Compose not available; trying to stop whatever is listening on port 8000 ...'
$listeners = Get-NetTCPConnection -LocalPort 8000 -ErrorAction SilentlyContinue |
Where-Object { $_.State -eq 'Listen' }
$pids = $listeners | Select-Object -ExpandProperty OwningProcess -Unique
foreach ($procId in $pids) {
try {
Stop-Process -Id $procId -Force -ErrorAction Stop
Write-Host "Stopped process $procId"
} catch {
Write-Warning "Could not stop PID ${procId}: $_"
}
}
Write-Host @'
No Docker Compose detected. Start the API manually in another terminal, from the repo:
cd backend
.\.venv\Scripts\activate # if you use a venv
pip install -r requirements.txt # first time only
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
Or use Docker Desktop, then from repo root: docker compose up --build
'@