File size: 1,636 Bytes
edc9558
 
 
 
 
 
 
 
 
 
 
 
0438355
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edc9558
 
 
 
 
 
 
 
 
 
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
#!/bin/bash
# AutoMLOps startup β€” launches Airflow scheduler then the Flask app
set -e

echo "===== AutoMLOps Startup at $(date -u '+%Y-%m-%d %H:%M:%S') ====="

# ── Airflow scheduler ─────────────────────────────────────────────────────────
echo "[startup] Starting Apache Airflow scheduler..."
airflow scheduler &
SCHEDULER_PID=$!
echo "[startup] Scheduler PID: ${SCHEDULER_PID}"

# Wait until the Airflow DB is reachable and scheduler has parsed DAGs.
# Polls every 2 s, gives up after 90 s (HuggingFace Space health-check window).
echo "[startup] Waiting for Airflow scheduler to become ready..."
for i in $(seq 1 45); do
    if python - <<'EOF' 2>/dev/null
import sys
try:
    from airflow.utils.session import create_session
    from airflow.models import DagBag
    with create_session() as s:
        pass
    db = DagBag(dag_folder="/app/dags", include_examples=False, read_dags_from_db=False)
    if db.import_errors:
        sys.exit(1)
    sys.exit(0)
except Exception:
    sys.exit(1)
EOF
    then
        echo "[startup] Airflow ready after $((i*2))s"
        break
    fi
    sleep 2
done

# ── Flask application ─────────────────────────────────────────────────────────
echo "[startup] Starting Flask application on :7860..."
exec gunicorn app:app \
    --bind 0.0.0.0:7860 \
    --workers 1 \
    --threads 4 \
    --worker-class gthread \
    --timeout 300 \
    --log-level info