Files
AI-Trader/entrypoint.sh
Bill 3502a7ffa8 fix: respect dev mode in entrypoint database initialization
- Update entrypoint.sh to check DEPLOYMENT_MODE before initializing database
- DEV mode: calls initialize_dev_database() which resets the database
- PROD mode: calls initialize_database() which preserves existing data
- Adds clear logging to show which mode is being used

This ensures the dev database is properly reset on container startup,
matching the behavior of the lifespan function in api/main.py.
2025-11-02 15:30:11 -05:00

93 lines
2.7 KiB
Bash
Executable File

#!/bin/bash
set -e # Exit on any error
echo "🚀 Starting AI-Trader-Server API..."
# Validate required environment variables
echo "🔍 Validating environment variables..."
MISSING_VARS=()
if [ -z "$OPENAI_API_KEY" ]; then
MISSING_VARS+=("OPENAI_API_KEY")
fi
if [ -z "$ALPHAADVANTAGE_API_KEY" ]; then
MISSING_VARS+=("ALPHAADVANTAGE_API_KEY")
fi
if [ -z "$JINA_API_KEY" ]; then
MISSING_VARS+=("JINA_API_KEY")
fi
if [ ${#MISSING_VARS[@]} -gt 0 ]; then
echo ""
echo "❌ ERROR: Missing required environment variables:"
for var in "${MISSING_VARS[@]}"; do
echo " - $var"
done
echo ""
echo "Please set these variables in your .env file:"
echo " 1. Copy .env.example to .env"
echo " 2. Edit .env and add your API keys"
echo " 3. Restart the container"
echo ""
exit 1
fi
echo "✅ Environment variables validated"
# Step 1: Initialize database (respecting dev/prod mode)
echo "📊 Initializing database..."
python -c "
from tools.deployment_config import is_dev_mode, get_db_path
from api.database import initialize_dev_database, initialize_database
db_path = 'data/jobs.db'
if is_dev_mode():
print(' 🔧 DEV mode detected - initializing dev database')
dev_db_path = get_db_path(db_path)
initialize_dev_database(dev_db_path)
else:
print(' 🏭 PROD mode - initializing production database')
initialize_database(db_path)
"
echo "✅ Database initialized"
# Step 2: Merge and validate configuration
echo "🔧 Merging and validating configuration..."
python -c "from tools.config_merger import merge_and_validate; merge_and_validate()" || {
echo "❌ Configuration validation failed"
exit 1
}
export CONFIG_PATH=/tmp/runtime_config.json
echo "✅ Configuration validated and merged"
# Step 3: Start MCP services in background
echo "🔧 Starting MCP services..."
cd /app
python agent_tools/start_mcp_services.py &
MCP_PID=$!
# Setup cleanup trap before starting uvicorn
trap "echo '🛑 Stopping services...'; kill $MCP_PID 2>/dev/null; exit 0" EXIT SIGTERM SIGINT
# Step 4: Wait for services to initialize
echo "⏳ Waiting for MCP services to start..."
sleep 3
# Step 5: Start FastAPI server with uvicorn (this blocks)
# Note: Container always uses port 8080 internally
# The API_PORT env var only affects the host port mapping in docker-compose.yml
echo "🌐 Starting FastAPI server on port 8080..."
echo "🔍 Checking if FastAPI app can be imported..."
python -c "from api.main import app; print('✓ App imported successfully')" || {
echo "❌ Failed to import FastAPI app"
exit 1
}
exec uvicorn api.main:app \
--host 0.0.0.0 \
--port 8080 \
--log-level info \
--access-log