File size: 2,276 Bytes
c49b21b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/sh
set -e

echo "[entrypoint] v2025-08-16-permissions-fix"


echo "[entrypoint] ensuring data directories exist with proper permissions..."
# Create directories under /data and /tmp/nginx (for Nginx temp files)
mkdir -p /data/advisorai-data \
         /data/merged \
         /data/alpaca \
         /data/crypto-bubbles \
         /data/finnhub \
         /data/finviz \
         /data/marketaux \
         /data/logs \
         /tmp/nginx/body \
         /tmp/nginx/proxy \
         /tmp/nginx/fastcgi

# Fix permissions at runtime (in case Dockerfile is not enough)
# Best-effort ownership/permission fixes; ignore errors on Space mounts
chown -R $(id -u):$(id -g) /data /tmp/nginx 2>/dev/null || true
chmod -R 777 /data /tmp/nginx 2>/dev/null || true

echo "[entrypoint] restoring data from Filebase…"
# Run data restoration in background to avoid blocking startup. Let script auto-detect writable base.
python /app/deployment/fetch_filebase.py &
FETCH_PID=$!

# Wait a bit for critical data, but don't block indefinitely
sleep 10

# Check if fetch is still running
if kill -0 $FETCH_PID 2>/dev/null; then
    echo "[entrypoint] Data fetch still running in background (PID: $FETCH_PID)"
else
    echo "[entrypoint] Data fetch completed"
fi

echo "[entrypoint] launching services…"

# ROLE-based startup: 'web' (default) runs API+nginx under supervisord; 'worker' runs scheduler directly
ROLE_ENV=${ROLE:-web}
echo "[entrypoint] detected ROLE=$ROLE_ENV"

if [ "$ROLE_ENV" = "worker" ]; then
    echo "[entrypoint] starting worker: scheduler only"
    exec python /app/deployment/scheduler.py
else
    # Hugging Face Spaces friendly mode: run uvicorn directly on $PORT if HF_MODE=1
    if [ "${HF_MODE:-0}" = "1" ]; then
        export PORT=${PORT:-7860}
        echo "[entrypoint] HF_MODE=1 -> launching uvicorn directly on PORT=$PORT"
        exec uvicorn src.api.main:app --host 0.0.0.0 --port ${PORT} --workers 1 --timeout-keep-alive 30
    else
        # Default: nginx + uvicorn via supervisord
        if [ -n "$PORT" ]; then
            echo "[entrypoint] configuring nginx to listen on PORT=$PORT"
            sed -i "s/listen 80;/listen ${PORT};/" /etc/nginx/conf.d/app.conf || true
        fi
        exec supervisord -c /etc/supervisord.conf
    fi
fi