diegobeyl/backtesting
2
1#!/bin/bash2set -e3 4# Entrypoint script for Docker container5# Handles different service types (api, streamlit, worker)6 7echo "๐ Starting Backtesting App V2..."8 9# Detect which service to run based on environment variable or command10SERVICE_TYPE="${SERVICE_TYPE:-api}"11 12# Create directories if they don't exist13mkdir -p logs cache config14 15# Check if .env file exists, if not use .env.example as fallback16if [ ! -f .env ]; then17 echo "โ ๏ธ No .env file found, using .env.example as reference"18 # Don't copy, just use env vars from docker-compose19fi20 21# Function to wait for a service22wait_for_service() {23 local host="$1"24 local port="$2"25 local service_name="$3"26 local max_attempts=6027 local attempt=028 29 echo "โณ Waiting for $service_name at $host:$port..."30 while [ $attempt -lt $max_attempts ]; do31 # Try curl first (more reliable in containers)32 if curl -f -s http://"$host":"$port"/health > /dev/null 2>&1; then33 echo "โ
$service_name is ready!"34 return 035 fi36 37 attempt=$((attempt + 1))38 if [ $((attempt % 10)) -eq 0 ]; then39 echo " Still waiting... ($attempt/$max_attempts)"40 fi41 sleep 142 done43 44 echo "โ ๏ธ $service_name didn't respond to health check, continuing anyway..."45 return 046}47 48# Handle different service types49case "$SERVICE_TYPE" in50 api)51 echo "๐ง Starting FastAPI server..."52 LOG_LEVEL_LOWER=$(echo "${LOG_LEVEL:-info}" | tr '[:upper:]' '[:lower:]')53 exec uvicorn api.main:app \54 --host "${API_HOST:-0.0.0.0}" \55 --port "${API_PORT:-8000}" \56 --log-level "$LOG_LEVEL_LOWER"57 ;;58 59 streamlit)60 echo "๐จ Starting Streamlit UI..."61 # Wait for API to be ready62 wait_for_service "api" "${API_PORT:-8000}" "API"63 exec streamlit run backtesting_app/app.py \64 --server.port="${STREAMLIT_PORT:-8501}" \65 --server.address="0.0.0.0" \66 --server.headless=true \67 --browser.gatherUsageStats=false \68 --logger.level="${LOG_LEVEL:-info}"69 ;;70 71 worker)72 echo "โ๏ธ Starting Celery worker..."73 # Wait for Redis to be ready74 if [ -n "$REDIS_HOST" ]; then75 wait_for_service "$REDIS_HOST" "${REDIS_PORT:-6379}" "Redis"76 fi77 exec celery -A api.tasks worker \78 --loglevel="${LOG_LEVEL:-info}" \79 --concurrency="${WORKER_CONCURRENCY:-4}"80 ;;81 82 beat)83 echo "โฐ Starting Celery beat..."84 # Wait for Redis to be ready85 if [ -n "$REDIS_HOST" ]; then86 wait_for_service "$REDIS_HOST" "${REDIS_PORT:-6379}" "Redis"87 fi88 exec celery -A api.tasks beat \89 --loglevel="${LOG_LEVEL:-info}"90 ;;91 92 *)93 echo "โ Unknown SERVICE_TYPE: $SERVICE_TYPE"94 echo "Valid options: api, streamlit, worker, beat"95 exit 196 ;;97esac98 