# Maya3 Distributed Pipeline - Docker Compose
#
# Usage:
#   docker-compose up -d                    # Start workers
#   docker-compose logs -f                  # View logs
#   docker-compose down                     # Stop workers
#
# Note: Each container uses ALL available GPUs by default.

version: '3.8'

services:
  # Main GPU worker - processes videos from Supabase queue
  worker:
    build:
      context: .
      dockerfile: Dockerfile
    image: maya3-pipeline:latest
    container_name: maya-worker
    restart: unless-stopped
    env_file:
      - .env
    environment:
      - NVIDIA_VISIBLE_DEVICES=all
      - CUDA_DEVICE_ORDER=PCI_BUS_ID
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: all
              capabilities: [gpu]
    volumes:
      # Persist output (optional if using R2)
      - ./output:/workspace/maya3_data/output
      # Model cache (speeds up restarts)
      - model-cache:/root/.cache
    shm_size: '16gb'
    logging:
      driver: "json-file"
      options:
        max-size: "100m"
        max-file: "3"
    command: >
      --supabase-queue
      --r2-upload
      --background-export
      --export-workers 4
      --output ./output

  # Lease recovery service - reclaims expired leases from crashed workers
  # CRITICAL: Run exactly ONE of these per cluster
  lease-recovery:
    build:
      context: .
      dockerfile: Dockerfile
    image: maya3-pipeline:latest
    container_name: maya-lease-recovery
    restart: unless-stopped
    env_file:
      - .env
    entrypoint: ["/workspace/maya3_data/.venv/bin/python"]
    command: ["scripts/monitor/lease_recovery.py", "--daemon", "--interval", "300"]
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 512M
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "1"

volumes:
  model-cache:
    driver: local
