Spaces:
Paused

File size: 3,397 Bytes
5eb4857
 
 
 
 
 
 
c054d5f
5df0bfe
1c4976c
5df0bfe
c054d5f
 
 
 
 
 
 
 
 
 
 
 
e6526ed
 
 
 
 
 
eab4a73
058e076
 
 
 
d4964f5
 
 
058e076
eab4a73
d4964f5
058e076
 
 
 
 
 
d4964f5
 
 
5eb4857
 
 
 
ff97dfb
 
5eb4857
 
d4964f5
 
 
 
 
 
 
5eb4857
46cac21
1c4976c
 
 
 
 
 
 
 
 
 
058e076
5eb4857
988a883
 
 
 
d4964f5
 
 
 
 
 
 
 
 
 
988a883
 
 
 
d4964f5
988a883
 
 
 
 
 
 
058e076
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash

# 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