#!/bin/bash # Foxhunt HFT System - Automated Startup Sequence # Starts all services in correct dependency order with health verification set -e # Exit on error # Color codes RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Configuration INFRA_WAIT=60 CORE_WAIT=120 GATEWAY_WAIT=30 echo "================================================================================" echo " Foxhunt HFT System - Startup Sequence" echo "================================================================================" echo "Timestamp: $(date '+%Y-%m-%d %H:%M:%S')" echo "" # Function to verify service health verify_service() { local service_name=$1 local health_check=$2 local max_retries=10 local retry=0 echo -n " Verifying $service_name..." while [ $retry -lt $max_retries ]; do if eval "$health_check" > /dev/null 2>&1; then echo -e " ${GREEN}OK${NC}" return 0 fi retry=$((retry + 1)) sleep 2 echo -n "." done echo -e " ${RED}FAILED${NC}" return 1 } # Layer 1: Infrastructure echo -e "${BLUE}[1/4] Starting Infrastructure Layer...${NC}" docker-compose up -d postgres redis vault influxdb echo "" echo "Waiting for infrastructure to be ready (${INFRA_WAIT}s)..." sleep $INFRA_WAIT echo "" echo "Verifying infrastructure health..." verify_service "PostgreSQL" "docker exec foxhunt-postgres pg_isready -U foxhunt" || { echo -e "${RED}PostgreSQL not ready${NC}"; exit 1; } verify_service "Redis" "docker exec foxhunt-redis redis-cli ping | grep -q PONG" || { echo -e "${RED}Redis not ready${NC}"; exit 1; } verify_service "Vault" "curl -sf http://localhost:8200/v1/sys/health" || { echo -e "${RED}Vault not ready${NC}"; exit 1; } verify_service "InfluxDB" "curl -sf http://localhost:8086/health" || echo -e "${YELLOW}InfluxDB check skipped${NC}" # Run database migrations echo "" echo -e "${BLUE}[2/4] Running Database Migrations...${NC}" if cargo sqlx migrate run 2>&1 | tee /tmp/foxhunt_migrations.log; then echo -e "${GREEN}✓ Migrations completed successfully${NC}" else echo -e "${YELLOW}⚠ Migration warnings/errors detected - review /tmp/foxhunt_migrations.log${NC}" fi # Layer 2: Core Services echo "" echo -e "${BLUE}[3/4] Starting Core Services...${NC}" docker-compose up -d trading_service backtesting_service ml_training_service echo "" echo "Waiting for core services to initialize (${CORE_WAIT}s)..." echo " (ML model loading and GPU initialization in progress)" sleep $CORE_WAIT echo "" echo "Verifying core services health..." verify_service "Trading Service" "curl -sf http://localhost:8081/health" || { echo -e "${RED}Trading Service not ready${NC}"; exit 1; } verify_service "Backtesting Service" "curl -sf http://localhost:8083/health" || echo -e "${YELLOW}Backtesting Service check skipped${NC}" verify_service "ML Training Service" "curl -sf http://localhost:8095/health" || echo -e "${YELLOW}ML Training Service check skipped${NC}" # Layer 3: API Gateway echo "" echo -e "${BLUE}[4/4] Starting API Gateway...${NC}" docker-compose up -d api_gateway echo "" echo "Waiting for API Gateway to be ready (${GATEWAY_WAIT}s)..." sleep $GATEWAY_WAIT echo "" echo "Verifying API Gateway health..." if command -v grpc_health_probe &> /dev/null; then verify_service "API Gateway (gRPC)" "grpc_health_probe -addr=localhost:50051" || { echo -e "${RED}API Gateway not ready${NC}"; exit 1; } else verify_service "API Gateway (HTTP)" "curl -sf http://localhost:8080/health" || { echo -e "${RED}API Gateway not ready${NC}"; exit 1; } fi # Start monitoring (optional) echo "" echo -e "${BLUE}Starting Monitoring Stack (optional)...${NC}" docker-compose up -d prometheus grafana alertmanager 2>/dev/null || echo -e "${YELLOW}Monitoring stack start skipped${NC}" # Final status echo "" echo "================================================================================" echo -e "${GREEN}✓ Foxhunt HFT System Started Successfully${NC}" echo "================================================================================" echo "" echo "Service Status:" docker-compose ps echo "" echo "Access Points:" echo " - API Gateway (gRPC): localhost:50051" echo " - Trading Service: localhost:50052" echo " - Backtesting Service: localhost:50053" echo " - ML Training Service: localhost:50054" echo " - Prometheus: http://localhost:9090" echo " - Grafana: http://localhost:3000 (admin/foxhunt123)" echo "" echo "Next Steps:" echo " 1. Verify health: scripts/comprehensive_health_check.sh" echo " 2. View logs: docker-compose logs -f [service-name]" echo " 3. Run tests: cargo test --workspace" echo "" echo "================================================================================"