Spaces:
Paused
Paused
Download scripts/run_prod.sh from 3SCError404/miracle: direct link, hf CLI and curl.
- Browser
- Download file 3.4 kB
-
https://huggingface.co/spaces/3SCError404/miracle/resolve/main/scripts/run_prod.sh
- Command line
-
hf download hf://spaces/3SCError404/miracle/scripts/run_prod.sh
-
curl -L -o run_prod.sh https://huggingface.co/spaces/3SCError404/miracle/resolve/main/scripts/run_prod.sh
3.4 kB
| # Production Entrypoint for Hugging Face Spaces | |
| # This script runs both the FastAPI API and the Arq Worker in the same container. | |
| echo "--- NEXUS PRODUCTION BOOTSTRAP ---" | |
| # Start Local Redis for Caching (Ephemeral Sidecar) | |
| echo "[0/3] Starting Local Redis..." | |
| redis-server --daemonize yes --port 6379 --maxmemory 256mb --maxmemory-policy allkeys-lru | |
| # wait for redis to be ready | |
| RETRIES=5 | |
| until redis-cli -p 6379 ping > /dev/null 2>&1 || [ $RETRIES -eq 0 ]; do | |
| echo "Waiting for Redis to start... ($RETRIES retries left)" | |
| sleep 1 | |
| RETRIES=$((RETRIES-1)) | |
| done | |
| if ! redis-cli -p 6379 ping > /dev/null 2>&1; then | |
| echo "[⚠️ WARNING] Local Redis failed to start. Backend may use fallback cache." | |
| fi | |
| # --- STEP 1: DATABASE MIGRATIONS --- | |
| # We run this ONCE in the parent process to prevent race conditions | |
| # when starting multiple uvicorn workers. | |
| echo "[1/4] Running Database Migrations (alembic)..." | |
| python -m alembic upgrade head | |
| # --- CLOUD ENTRPOINT HARDENING --- | |
| # We use a trap to ensure SIGTERM (from Hugging Face/Docker) is propagated | |
| # to both uvicorn and arq, allowing for graceful job completion. | |
| _term() { | |
| echo "🛑 Caught termination signal! Propagating to Worker(s), API, and Ollama..." | |
| kill -TERM "$REGULAR_WORKER_PID" 2>/dev/null | |
| kill -TERM "$HEAVY_WORKER_PID" 2>/dev/null | |
| kill -TERM "$API_PID" 2>/dev/null | |
| kill -TERM "$OLLAMA_PID" 2>/dev/null | |
| wait "$REGULAR_WORKER_PID" "$HEAVY_WORKER_PID" "$API_PID" | |
| echo "--- NEXUS SHUTDOWN COMPLETE ---" | |
| exit 0 | |
| } | |
| trap _term SIGTERM SIGINT | |
| # Start Arq Workers in the background | |
| echo "[2/4] Starting Nexus AI Workers (Regular + Heavy)..." | |
| # Persistence Mapping: Use the /data mount if available | |
| if [ -d "/data" ]; then | |
| echo "--- PERSISTENT STORAGE DETECTED (/data) ---" | |
| mkdir -p /data/uploads /data/logs | |
| export UPLOAD_DIR="uploads" | |
| export LOG_DIR="logs" | |
| fi | |
| # Worker 1: Regular Lane (High concurrency, high frequency) | |
| arq nexus_worker.RegularWorkerSettings & | |
| REGULAR_WORKER_PID=$! | |
| # Worker 2: Heavy Lane (AI tasks, low concurrency, long timeouts) | |
| arq nexus_worker.HeavyWorkerSettings & | |
| HEAVY_WORKER_PID=$! | |
| APP_PORT=${PORT:-7860} | |
| echo "[3/4] Starting Nexus Master API (Gunicorn) on port ${APP_PORT}..." | |
| # Migrated to Gunicorn for robust process management and worker recycling. | |
| # Using 4 workers with a 120s timeout to handle long-running AI streams. | |
| gunicorn main:app \ | |
| --worker-class uvicorn.workers.UvicornWorker \ | |
| --workers 4 \ | |
| --bind 0.0.0.0:${APP_PORT} \ | |
| --timeout 120 \ | |
| --keep-alive 5 \ | |
| --log-level info & | |
| API_PID=$! | |
| # --- RESILIENT SERVICE MONITOR --- | |
| echo "🟢 All services started. Initiating infrastructure monitor..." | |
| while true; do | |
| if ! kill -0 "$REGULAR_WORKER_PID" 2>/dev/null; then | |
| echo "⚠️ ALERT: Regular Worker (PID: $REGULAR_WORKER_PID) has stopped. Attempting restart..." | |
| arq nexus_worker.RegularWorkerSettings & | |
| REGULAR_WORKER_PID=$! | |
| fi | |
| if ! kill -0 "$HEAVY_WORKER_PID" 2>/dev/null; then | |
| echo "⚠️ ALERT: Heavy Worker (PID: $HEAVY_WORKER_PID) has stopped. Attempting restart..." | |
| arq nexus_worker.HeavyWorkerSettings & | |
| HEAVY_WORKER_PID=$! | |
| fi | |
| if ! kill -0 "$API_PID" 2>/dev/null; then | |
| echo "⚠️ ALERT: Nexus Master API (PID: $API_PID) has stopped. Fail-fast triggered." | |
| break | |
| fi | |
| sleep 60 | |
| done | |
| echo "🛑 CRITICAL: core services have exited. Shutting down node..." | |
| _term | |
| exit 1 | |