fix: exit container when backend or nginx dies so Docker can restart it

Previously uvicorn was launched with `&` while nginx ran with `exec`, so if
the backend crashed (e.g. the `AssertionError` in websockets' keepalive_ping
that took prod down for ~31h), nginx kept the container "up" and every
/api/* request returned 502. Now both processes are monitored with
`wait -n`; whichever dies first triggers the entrypoint to exit, letting
`restart: unless-stopped` recover the container cleanly.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
David Montero 2026-04-16 05:52:52 +02:00
parent 09d048e20f
commit 8e3c00ee7e
1 changed files with 13 additions and 2 deletions

View File

@ -39,10 +39,21 @@ fi
# Start FastAPI backend in the background on port 8001
echo "🚀 Starting Velxio Backend..."
uvicorn app.main:app --host 127.0.0.1 --port 8001 &
UVICORN_PID=$!
# Wait for backend to be healthy (optional but good practice)
sleep 2
# Start Nginx in the foreground to keep the container running
# Start Nginx in the background so we can monitor both processes
echo "🌐 Starting Nginx Web Server on port 80..."
exec nginx -g "daemon off;"
nginx -g "daemon off;" &
NGINX_PID=$!
# If either process dies, exit so Docker's restart policy can recover the
# container. Previously uvicorn could crash silently while nginx kept the
# container "up", leaving every /api/* request returning 502 Bad Gateway.
wait -n "$UVICORN_PID" "$NGINX_PID"
EXIT_CODE=$?
echo "⚠️ A core process exited (code=$EXIT_CODE). Shutting down container so Docker can restart it."
kill "$UVICORN_PID" "$NGINX_PID" 2>/dev/null || true
exit "$EXIT_CODE"