34 - App Startup
Overview
Startup Flow Diagram
┌──────────────────────────────────────────────────────────────────────────────┐
│ OpenAlgo Startup Sequence │
└──────────────────────────────────────────────────────────────────────────────┘
uv run app.py
│
▼
┌─────────────────────────────────────────────────────────────────────────────┐
│ PHASE 1: Environment Validation │
│ │
│ utils/env_check.py::load_and_check_env_variables() │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ 1. Check ENV_CONFIG_VERSION compatibility │ │
│ │ - Compare .env version with .sample.env │ │
│ │ - Warn if outdated, prompt to continue or exit │ │
│ │ │ │
│ │ 2. Validate required environment variables (30+ vars) │ │
│ │ - APP_KEY, API_KEY_PEPPER (security) │ │
│ │ - BROKER_API_KEY, BROKER_API_SECRET (broker auth) │ │
│ │ - DATABASE_URL, WEBSOCKET_PORT (infrastructure) │ │
│ │ - Rate limits, logging config │ │
│ │ │ │
│ │ 3. Validate broker-specific API key formats │ │
│ │ - 5paisa: User_Key:::User_ID:::client_id │ │
│ │ - Flattrade: client_id:::api_key │ │
│ │ - Dhan: client_id:::api_key │ │
│ │ │ │
│ │ 4. Validate REDIRECT_URL matches valid broker │ │
│ │ │ │
│ │ 5. Exit with error if any validation fails │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────────┘
│
▼ (if all validations pass)
┌─────────────────────────────────────────────────────────────────────────────┐
│ PHASE 2: Flask App Creation (create_app()) │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ 1. Initialize Flask application │ │
│ │ │ │
│ │ 2. Initialize extensions: │ │
│ │ - SocketIO (real-time updates) │ │
│ │ - CSRF Protection │ │
│ │ - Flask-Limiter (rate limiting) │ │
│ │ - Flask-CORS (cross-origin) │ │
│ │ - CSP Middleware (content security) │ │
│ │ │ │
│ │ 3. Configure session cookies: │ │
│ │ - HTTPONLY, SAMESITE=Lax │ │
│ │ - SECURE if HTTPS detected │ │
│ │ - __Secure- prefix for HTTPS │ │
│ │ │ │
│ │ 4. Register 30+ blueprints: │ │
│ │ - React frontend (if /frontend/dist exists) │ │
│ │ - REST API (/api/v1/) │ │
│ │ - UI blueprints (dashboard, orders, etc.) │ │
│ │ - Webhook endpoints (chartink, strategy, flow) │ │
│ │ │ │
│ │ 5. Configure CSRF exemptions: │ │
│ │ - API endpoints (use API key auth) │ │
│ │ - Webhook endpoints (external callbacks) │ │
│ │ - OAuth broker callbacks │ │
│ │ │ │
│ │ 6. Initialize middleware: │ │
│ │ - Security middleware (IP banning, etc.) │ │
│ │ - Traffic logging │ │
│ │ - Latency monitoring │ │
│ │ │ │
│ │ 7. Setup error handlers (400, 404, 429, 500) │ │
│ │ │ │
│ │ 8. Auto-start Telegram bot (if previously active) │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────────────┐
│ PHASE 3: Setup Environment (setup_environment()) │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ 1. Load broker plugins │ │
│ │ utils/plugin_loader.py::load_broker_auth_functions() │ │
│ │ - Scan broker/*/plugin.json │ │
│ │ - Load auth functions dynamically │ │
│ │ │ │
│ │ 2. Initialize 17 databases in PARALLEL (ThreadPoolExecutor) │ │
│ │ - Auth DB, User DB, Master Contract DB │ │
│ │ - API Log DB, Analyzer DB, Settings DB │ │
│ │ - Chartink DB, Traffic Logs DB, Latency DB │ │
│ │ - Strategy DB, Sandbox DB, Action Center DB │ │
│ │ - Chart Prefs DB, Market Calendar DB │ │
│ │ - Qty Freeze DB, Historify DB, Flow DB │ │
│ │ │ │
│ │ 3. Initialize Flow scheduler │ │
│ │ services/flow_scheduler_service.py │ │
│ │ │ │
│ │ 4. Setup ngrok cleanup handlers │ │
│ │ (always registered, tunnel created later if enabled) │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────────────┐
│ PHASE 4: Cache Restoration │
│ │
│ database/cache_restoration.py::restore_all_caches() │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ Enables server restart without re-login: │ │
│ │ │ │
│ │ 1. Restore Symbol Cache │ │
│ │ - Load broker symbols from master contract DB │ │
│ │ - Rebuild BrokerSymbolCache in memory │ │
│ │ │ │
│ │ 2. Restore Auth Token Cache │ │
│ │ - Load encrypted tokens from auth DB │ │
│ │ - Decrypt and restore to TTLCache │ │
│ │ │ │
│ │ Result: Users remain logged in after server restart │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────────────┐
│ PHASE 5: Analyzer Mode Services (if enabled) │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ Check: database/settings_db.py::get_analyze_mode() │ │
│ │ │ │
│ │ If Analyzer Mode is ON, start in PARALLEL: │ │
│ │ │ │
│ │ 1. Execution Engine (sandbox/execution_thread.py) │ │
│ │ - Monitors pending orders │ │
│ │ - Executes based on live market prices │ │
│ │ │ │
│ │ 2. Square-off Scheduler (sandbox/squareoff_thread.py) │ │
│ │ - Auto-closes MIS positions at EOD │ │
│ │ │ │
│ │ 3. Catch-up Settlement Processor │ │
│ │ - Process any missed T+1 settlements │ │
│ │ - Handles weekend/holiday gaps │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────────────┐
│ PHASE 6: WebSocket Proxy Integration │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ Check environment mode: │ │
│ │ │ │
│ │ Docker/Standalone Mode: │ │
│ │ - WebSocket server started separately by start.sh │ │
│ │ - Skip proxy integration │ │
│ │ │ │
│ │ Local/Integrated Mode: │ │
│ │ - Start WebSocket proxy in Flask process │ │
│ │ - websocket_proxy/app_integration.py::start_websocket_proxy() │ │
│ │ - Runs on port 8765 (default) │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────────────┐
│ PHASE 7: Server Start (__main__ block) │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ 1. Read server configuration: │ │
│ │ - FLASK_HOST_IP (default: 127.0.0.1) │ │
│ │ - FLASK_PORT (default: 5000) │ │
│ │ - FLASK_DEBUG mode │ │
│ │ │ │
│ │ 2. Start ngrok tunnel (if NGROK_ALLOW=TRUE) │ │
│ │ utils/ngrok_manager.py::start_ngrok_tunnel() │ │
│ │ │ │
│ │ 3. Display startup banner with: │ │
│ │ - Version number │ │
│ │ - Web App URL │ │
│ │ - WebSocket URL │ │
│ │ - Ngrok URL (if enabled) │ │
│ │ - Docs URL │ │
│ │ - Ready status │ │
│ │ │ │
│ │ 4. Start SocketIO server: │ │
│ │ socketio.run(app, host, port, debug) │ │
│ │ - Excludes strategies/* and log/* from reloader │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────────┘
│
▼
Server Ready!Startup Checks Summary
Environment Validation Checks
Check
Description
Exit on Failure
Database Initialization
Before Request Hook
Error Handlers
Error
Handler
Behavior
Startup Banner Example
Key Files Reference
File
Purpose
Last updated