x-defaults: &defaults
  restart: unless-stopped
  logging:
    driver: json-file
    options:
      max-size: 100m
      max-file: '3'

services:
  ray-head:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}rayproject/ray:${RAY_VERSION:-2.42.1-py312}
    command: ray start --head --dashboard-host=0.0.0.0 --port=6379 --block
    ports:
      - '${RAY_DASHBOARD_PORT_OVERRIDE:-8265}:8265'
      - '${RAY_CLIENT_PORT_OVERRIDE:-10001}:10001'
      - '${RAY_GCS_PORT_OVERRIDE:-6379}:6379'
    environment:
      TZ: ${TZ:-UTC}
      RAY_NUM_CPUS: ${RAY_HEAD_NUM_CPUS:-4}
      RAY_MEMORY: ${RAY_HEAD_MEMORY:-8589934592}
    volumes:
      - ray_storage:/tmp/ray
    deploy:
      resources:
        limits:
          cpus: ${RAY_HEAD_CPU_LIMIT:-4.0}
          memory: ${RAY_HEAD_MEMORY_LIMIT:-8G}
        reservations:
          cpus: ${RAY_HEAD_CPU_RESERVATION:-2.0}
          memory: ${RAY_HEAD_MEMORY_RESERVATION:-4G}
    healthcheck:
      test: [CMD, wget, --no-verbose, --tries=1, --spider, 'http://localhost:8265/']
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s

  ray-worker-1:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}rayproject/ray:${RAY_VERSION:-2.42.1-py312}
    command: ray start --address=ray-head:6379 --block
    depends_on:
      ray-head:
        condition: service_healthy
    environment:
      TZ: ${TZ:-UTC}
      RAY_NUM_CPUS: ${RAY_WORKER_NUM_CPUS:-2}
      RAY_MEMORY: ${RAY_WORKER_MEMORY:-4294967296}
    deploy:
      resources:
        limits:
          cpus: ${RAY_WORKER_1_CPU_LIMIT:-2.0}
          memory: ${RAY_WORKER_1_MEMORY_LIMIT:-4G}
        reservations:
          cpus: ${RAY_WORKER_1_CPU_RESERVATION:-1.0}
          memory: ${RAY_WORKER_1_MEMORY_RESERVATION:-2G}

  ray-worker-2:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}rayproject/ray:${RAY_VERSION:-2.42.1-py312}
    command: ray start --address=ray-head:6379 --block
    depends_on:
      ray-head:
        condition: service_healthy
    environment:
      TZ: ${TZ:-UTC}
      RAY_NUM_CPUS: ${RAY_WORKER_NUM_CPUS:-2}
      RAY_MEMORY: ${RAY_WORKER_MEMORY:-4294967296}
    deploy:
      resources:
        limits:
          cpus: ${RAY_WORKER_2_CPU_LIMIT:-2.0}
          memory: ${RAY_WORKER_2_MEMORY_LIMIT:-4G}
        reservations:
          cpus: ${RAY_WORKER_2_CPU_RESERVATION:-1.0}
          memory: ${RAY_WORKER_2_MEMORY_RESERVATION:-2G}

volumes:
  ray_storage:
