#!/usr/bin/env bash # ============================================================================= # Velocity Backend — Recovery + Investor Demo Seed # ============================================================================= # Run this on the production server (via SSH) to: # 1. Diagnose why the FastAPI backend is down (502 gateway) # 2. Restart the uvicorn service on port 8001 # 3. Verify the /health endpoint responds through nginx # 4. Run the iPad investor-demo seed script to populate 50 realistic # clients, 11 properties with Unsplash media, calendar events, and tasks # # Usage: # ssh ubuntu@ # cd /path/to/Project_Velocity # bash backend/scripts/restore_and_seed_demo.sh # # Prerequisites on server: # - Python 3.11+ # - backend/.env with DATABASE_URL or VELOCITY_DB_* vars set # - asyncpg installed (pip install -r backend/requirements.txt) # ============================================================================= set -euo pipefail REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" BACKEND_DIR="$REPO_ROOT/backend" VENV_PATH="${VENV_PATH:-$REPO_ROOT/.venv}" UVICORN_PORT="${UVICORN_PORT:-8001}" UVICORN_WORKERS="${UVICORN_WORKERS:-2}" OPERATOR_EMAIL="${VELOCITY_DEMO_OPERATOR_EMAIL:-sayan@desineuron.in}" echo "" echo "============================================================" echo " Velocity Backend Recovery + Demo Seed" echo " Server: $(hostname)" echo " Repo: $REPO_ROOT" echo " Port: $UVICORN_PORT" echo "============================================================" echo "" # ── Step 1: Diagnose ───────────────────────────────────────────────────────── echo "▶ Step 1/4 — Diagnosing current process state" PIDS=$(lsof -ti tcp:"$UVICORN_PORT" 2>/dev/null || true) if [ -n "$PIDS" ]; then echo " ✓ Found process(es) on port $UVICORN_PORT: $PIDS" echo " Killing stale/hung processes..." kill -9 $PIDS 2>/dev/null || true sleep 1 else echo " ✗ No process on port $UVICORN_PORT — backend is down. Will restart." fi # Check systemd service if present if systemctl list-units --full --all | grep -q "velocity-backend"; then echo " Found systemd unit: velocity-backend" sudo systemctl stop velocity-backend 2>/dev/null || true fi # ── Step 2: Activate venv and verify deps ──────────────────────────────────── echo "" echo "▶ Step 2/4 — Activating virtualenv and verifying dependencies" if [ -f "$VENV_PATH/bin/activate" ]; then source "$VENV_PATH/bin/activate" echo " ✓ Activated venv: $VENV_PATH" else echo " ! No venv at $VENV_PATH — using system Python" fi PYTHON="${PYTHON:-python3}" # Quick dep check $PYTHON -c "import fastapi, uvicorn, asyncpg, dotenv" 2>/dev/null || { echo " Installing backend requirements..." $PYTHON -m pip install --quiet -r "$BACKEND_DIR/requirements.txt" } echo " ✓ Dependencies OK" # ── Step 3: Start uvicorn ───────────────────────────────────────────────────── echo "" echo "▶ Step 3/4 — Starting uvicorn on port $UVICORN_PORT" cd "$REPO_ROOT" LOG_FILE="/tmp/velocity_backend_$(date +%Y%m%d_%H%M%S).log" nohup $PYTHON -m uvicorn backend.main:app \ --host 127.0.0.1 \ --port "$UVICORN_PORT" \ --workers "$UVICORN_WORKERS" \ --log-level info \ --timeout-keep-alive 75 \ > "$LOG_FILE" 2>&1 & UVICORN_PID=$! echo " uvicorn PID: $UVICORN_PID Log: $LOG_FILE" # Wait for it to bind echo " Waiting for backend to accept connections..." MAX_WAIT=30 WAITED=0 until curl -sf "http://127.0.0.1:$UVICORN_PORT/health" > /dev/null 2>&1; do sleep 1 WAITED=$((WAITED + 1)) if [ $WAITED -ge $MAX_WAIT ]; then echo "" echo " ✗ Backend did not start within ${MAX_WAIT}s. Last log output:" tail -40 "$LOG_FILE" exit 1 fi echo -n "." done echo "" HEALTH=$(curl -sf "http://127.0.0.1:$UVICORN_PORT/health") echo " ✓ Backend healthy: $HEALTH" # Verify nginx can reach it echo "" echo " Checking nginx → backend proxy..." if curl -sf "https://api.desineuron.in/health" > /dev/null 2>&1; then echo " ✓ api.desineuron.in/health is UP — 502 is resolved." else echo " ! api.desineuron.in/health still failing." echo " Check: sudo nginx -t && sudo systemctl reload nginx" echo " Continuing to seed anyway (backend is healthy on localhost)." fi # ── Step 4: Seed investor demo data ────────────────────────────────────────── echo "" echo "▶ Step 4/4 — Running iPad investor demo seed" echo " Operator: $OPERATOR_EMAIL" echo " Target: 50 clients, 11 properties, media, tasks, calendar events" echo "" cd "$REPO_ROOT" $PYTHON backend/scripts/seed_ipad_investor_demo.py \ --operator-email "$OPERATOR_EMAIL" \ ${VELOCITY_DEMO_TENANT_ID:+--tenant-id "$VELOCITY_DEMO_TENANT_ID"} echo "" echo "============================================================" echo " ✅ Recovery complete." echo "" echo " Backend: http://127.0.0.1:$UVICORN_PORT (PID $UVICORN_PID)" echo " Public: https://api.desineuron.in" echo " Log: $LOG_FILE" echo "" echo " The iPad dashboard will show live data on next refresh." echo " Tap 'Updated now' in the header to trigger an immediate sync." echo "============================================================" echo ""