# Runs app.py under a supervisor loop so the OAK-1 camera app survives # native crashes (e.g. DepthAI/USB device faults) that kill the Python # process outright. This window stays open and keeps restarting the app; # closing THIS window is the only thing that stops it for good. # # Usage: right-click -> "Run with PowerShell", or from a terminal: # powershell -ExecutionPolicy Bypass -File run_supervised.ps1 $ErrorActionPreference = "Continue" Set-Location -Path $PSScriptRoot $python = Join-Path $PSScriptRoot ".venv\Scripts\python.exe" $logDir = Join-Path $PSScriptRoot "logs" New-Item -ItemType Directory -Force -Path $logDir | Out-Null $maxRestartsPerWindow = 10 $restartWindowSeconds = 300 $restartTimestamps = @() Write-Host "OAK-1 Camera Playground - Supervisor" -ForegroundColor Cyan Write-Host "Close THIS window to stop the app. It will auto-restart on crash." -ForegroundColor Cyan Write-Host "" while ($true) { $timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss" $logFile = Join-Path $logDir "app_$timestamp.log" Write-Host "[$timestamp] Starting app.py (logging to $logFile)..." -ForegroundColor Green # Route through cmd.exe so stderr merges into plain text instead of # PowerShell wrapping each line as a NativeCommandError. & cmd.exe /c "`"$python`" app.py 2>&1" | Tee-Object -FilePath $logFile $exitCode = $LASTEXITCODE $now = Get-Date Write-Host "[$($now.ToString('yyyy-MM-dd_HH-mm-ss'))] app.py exited (code $exitCode)." -ForegroundColor Yellow $restartTimestamps = @($restartTimestamps | Where-Object { ($now - $_).TotalSeconds -lt $restartWindowSeconds }) $restartTimestamps += $now if ($restartTimestamps.Count -gt $maxRestartsPerWindow) { Write-Host "Too many restarts ($($restartTimestamps.Count)) in the last $restartWindowSeconds seconds." -ForegroundColor Red Write-Host "Something is persistently wrong (device disconnected? driver issue?). Pausing 60s before trying again." -ForegroundColor Red Start-Sleep -Seconds 60 $restartTimestamps = @() } else { Start-Sleep -Seconds 2 } }