#!/bin/sh set -eu APP_MODULE="${APP_MODULE:-app.main:app}" APP_HOST="${APP_HOST:-0.0.0.0}" APP_PORT="${APP_PORT:-8000}" APP_ENV="${APP_ENV:-production}" WORKERS="${UVICORN_WORKERS:-1}" REDIS_WAIT_TIMEOUT="${REDIS_WAIT_TIMEOUT:-30}" wait_for_redis() { if [ -z "${REDIS_URL:-}" ]; then echo "REDIS_URL not set, skipping Redis wait" return 0 fi echo "Waiting for Redis: ${REDIS_URL}" python - <<'PY' import os import sys import time from urllib.parse import urlparse redis_url = os.environ.get("REDIS_URL", "") timeout = int(os.environ.get("REDIS_WAIT_TIMEOUT", "30")) parsed = urlparse(redis_url) host = parsed.hostname or "redis" port = parsed.port or 6379 deadline = time.time() + timeout while time.time() < deadline: try: import socket with socket.create_connection((host, port), timeout=2): print(f"Redis is reachable at {host}:{port}") raise SystemExit(0) except OSError: time.sleep(1) print(f"Timed out waiting for Redis at {host}:{port}", file=sys.stderr) raise SystemExit(1) PY } run_migrations() { if [ "${RUN_MIGRATIONS:-0}" != "1" ]; then echo "RUN_MIGRATIONS disabled, skipping" return 0 fi if [ -f alembic.ini ] && command -v alembic >/dev/null 2>&1; then echo "Running Alembic migrations" alembic upgrade head return 0 fi if [ -f manage.py ]; then echo "Running Django migrations" python manage.py migrate --noinput return 0 fi echo "No supported migration command found, skipping" } wait_for_redis run_migrations echo "Starting application on ${APP_HOST}:${APP_PORT}" exec python -m uvicorn "${APP_MODULE}" --host "${APP_HOST}" --port "${APP_PORT}" --workers "${WORKERS}"