version: '3.8'

# Genesis Browserless — Persistent Session Infrastructure
# Deployed on Elestio alongside existing PostgreSQL/Qdrant/Redis infra
# Accounts managed: kinan@agileadapt.com (Gemini Ultra, GHL, Telnyx)
#                   sunvision07@gmail.com (Jules Pro, YouTube)

services:
  browserless:
    image: ghcr.io/browserless/chromium:latest
    container_name: genesis-browserless
    ports:
      - "3000:3000"
    volumes:
      # Persistent Chrome profiles — survive container restarts
      - ./profiles:/tmp/browserless-profiles
      # Browserless assets/storage (screenshots, PDFs, etc.)
      - ./storage:/usr/local/lib/node_modules/browserless/assets
    environment:
      # Auth token — set in .env file, NEVER hardcode
      - TOKEN=${BROWSERLESS_TOKEN}

      # Concurrency: 10 parallel sessions, queue up to 20
      - MAX_CONCURRENT_SESSIONS=10
      - MAX_QUEUE_LENGTH=20

      # 60s connection timeout — enough for page loads, not runaway sessions
      - CONNECTION_TIMEOUT=60000

      # Pre-warm Chrome on startup for zero-latency first request
      - PREBOOT_CHROME=true

      # Disable debugger UI (not needed in prod)
      - ENABLE_DEBUGGER=false

      # Live deployment — no demo watermarks
      - DEMO_MODE=false

      # Allow file:// protocol for local profile loading
      - ALLOW_FILE_PROTOCOL=true

      # Keep sessions alive between requests (critical for login persistence)
      - KEEP_ALIVE=true

      # Health check endpoint
      - HEALTH=true

      # Ignore HTTPS cert errors (needed for some Elestio internal services)
      - DEFAULT_LAUNCH_ARGS=["--no-sandbox","--disable-dev-shm-usage","--disable-gpu","--ignore-certificate-errors"]

    restart: unless-stopped

    # 2GB RAM cap — prevents OOM on shared Elestio node
    mem_limit: 2g
    memswap_limit: 2g

    # Health check — Docker will restart if browserless goes unresponsive
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 20s

    # Logging — keep last 50MB of logs across 5 rotated files
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "5"

# Named volumes for data that must survive docker-compose down/up
volumes:
  browserless_profiles:
    driver: local
  browserless_storage:
    driver: local
