Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
| # Entrypoint script for Auto-Analyst backend | |
| # This script safely initializes the database and starts the application | |
| # SAFE for PostgreSQL/RDS - only modifies SQLite databases | |
| set -e # Exit on any error | |
| echo "π Starting Auto-Analyst Backend..." | |
| # Function to run safe database initialization | |
| init_production_database() { | |
| echo "π§ Running SAFE database initialization..." | |
| # Run the safe initialization script | |
| python scripts/init_production_db.py | |
| # Don't fail if database initialization has issues - let app try to start | |
| if [ $? -eq 0 ]; then | |
| echo "β Database initialization completed successfully" | |
| else | |
| echo "β οΈ Database initialization had issues, but continuing..." | |
| echo "π App will start but some features may not work properly" | |
| fi | |
| } | |
| # Function to verify basic app imports work | |
| verify_app_imports() { | |
| echo "π Verifying application imports..." | |
| python -c " | |
| try: | |
| from app import app | |
| print('β Main application imports successful') | |
| except Exception as e: | |
| print(f'β Application import failed: {e}') | |
| exit(1) | |
| " || { | |
| echo "β Critical application import failure - cannot start" | |
| exit 1 | |
| } | |
| } | |
| # Function to verify database connectivity (non-failing) | |
| verify_database_connectivity() { | |
| echo "π Testing database connectivity..." | |
| python -c " | |
| try: | |
| from src.db.init_db import get_session, is_postgres_db | |
| from src.db.schemas.models import AgentTemplate | |
| db_type = 'PostgreSQL/RDS' if is_postgres_db() else 'SQLite' | |
| print(f'ποΈ Database type: {db_type}') | |
| session = get_session() | |
| # Try to query templates if table exists | |
| try: | |
| template_count = session.query(AgentTemplate).count() | |
| print(f'β Database connected. Found {template_count} templates.') | |
| except Exception as table_error: | |
| print(f'β οΈ Database connected but template table issue: {table_error}') | |
| print('π Template functionality may not work') | |
| finally: | |
| session.close() | |
| except Exception as e: | |
| print(f'β οΈ Database connectivity issue: {e}') | |
| print('π App will start but database features may not work') | |
| " | |
| # Don't exit on database connectivity issues - let app try to start | |
| } | |
| # Function to populate agents and templates for development (SQLite only) | |
| # Uses agents_config.json if available, falls back to legacy method | |
| populate_agents_templates() { | |
| echo "π§ Checking if agents/templates need to be populated..." | |
| python -c " | |
| try: | |
| from src.db.init_db import DATABASE_URL | |
| from src.db.schemas.models import AgentTemplate | |
| from src.db.init_db import session_factory | |
| # Check database type | |
| if DATABASE_URL.startswith('sqlite'): | |
| print('π SQLite database detected - checking template population') | |
| session = session_factory() | |
| try: | |
| template_count = session.query(AgentTemplate).count() | |
| if template_count == 0: | |
| print('π No templates found - populating agents and templates...') | |
| session.close() | |
| exit(1) # Signal that population is needed | |
| else: | |
| print(f'β Found {template_count} templates - population not needed') | |
| session.close() | |
| exit(0) # Signal that population is not needed | |
| except Exception as e: | |
| print(f'β οΈ Error checking templates: {e}') | |
| print('π Will attempt to populate anyway') | |
| session.close() | |
| exit(1) # Signal that population is needed | |
| else: | |
| print('π PostgreSQL/RDS detected - skipping auto-population') | |
| exit(0) # Signal that population is not needed | |
| except Exception as e: | |
| print(f'β Error during template check: {e}') | |
| exit(0) # Don't fail startup, just skip population | |
| " | |
| # Check if population is needed (exit code 1 means yes) | |
| if [ $? -eq 1 ]; then | |
| echo "π Running agent/template population for SQLite..." | |
| # Check if agents_config.json exists (try multiple locations) | |
| if [ -f "agents_config.json" ] || [ -f "/app/agents_config.json" ] || [ -f "../agents_config.json" ]; then | |
| echo "π Found agents_config.json - validating configuration..." | |
| # Validate configuration first | |
| python scripts/populate_agent_templates.py validate | |
| validation_result=$? | |
| if [ $validation_result -eq 0 ]; then | |
| echo "β Configuration valid - proceeding with sync" | |
| python scripts/populate_agent_templates.py sync | |
| else | |
| echo "β οΈ Configuration validation failed - attempting sync anyway" | |
| python scripts/populate_agent_templates.py sync | |
| fi | |
| else | |
| echo "β οΈ agents_config.json not found - trying legacy method" | |
| python scripts/populate_agent_templates.py | |
| fi | |
| if [ $? -eq 0 ]; then | |
| echo "β Agent/template population completed successfully" | |
| else | |
| echo "β οΈ Agent/template population had issues, but continuing..." | |
| echo "π You may need to populate templates manually" | |
| echo "π‘ Tip: Ensure agents_config.json exists in the backend directory" | |
| fi | |
| fi | |
| } | |
| # Check if we need to find agents_config.json from space root | |
| if [ ! -f "/app/agents_config.json" ]; then | |
| echo "β οΈ agents_config.json not found in container - checking build issues" | |
| echo "π Files in /app directory:" | |
| ls -la /app/ | head -10 | |
| else | |
| echo "β agents_config.json found in container" | |
| fi | |
| # Main startup sequence | |
| echo "π§ Initializing production environment..." | |
| # Verify critical imports first | |
| verify_app_imports | |
| # Initialize database safely (won't modify RDS) | |
| init_production_database | |
| # Test database connectivity (non-failing) | |
| verify_database_connectivity | |
| # Populate agents and templates for development (SQLite only) | |
| populate_agents_templates | |
| echo "π― Starting FastAPI application..." | |
| echo "π Application will be available on port 7860" | |
| # Start the FastAPI application | |
| exec uvicorn app:app --host 0.0.0.0 --port 7860 |