Maaroufabousaleh
f
c49b21b
#!/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