services:
  # 主应用（Web API 服务）- 使用 Nginx + PHP-FPM
  app:
    build: .
    container_name: math_cms_app
    # 使用 Dockerfile 中的默认 CMD（nginx + php-fpm）
    ports:
      - "5019:8000"
    env_file:
      - .env
    volumes:
      - ./storage:/app/storage  # 日志 + 临时文件 + OCR上传
      - ./.env:/app/.env        # 环境配置文件
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://localhost:8000/health || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  # 队列 Worker（通用任务队列）
  queue:
    build: .
    container_name: math_cms_queue
    command: php artisan queue:work --sleep=3 --tries=3 --max-time=3600
    env_file:
      - .env
    volumes:
      - ./storage:/app/storage
      - ./.env:/app/.env
    restart: unless-stopped
    stop_grace_period: 60s
    # 队列 worker 健康检查：检查进程是否存在
    healthcheck:
      test: ["CMD-SHELL", "pgrep -f 'queue:work' || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 10s
    deploy:
      resources:
        limits:
          cpus: '1'
          memory: 512M

  # PDF 生成专用 Worker（资源隔离，防止 Chrome 吃满 CPU）
  pdf-worker:
    build: .
    container_name: math_cms_pdf
    command: php artisan queue:work --queue=pdf --sleep=3 --tries=2 --max-time=300 --max-jobs=10
    env_file:
      - .env
    volumes:
      - ./storage:/app/storage
      - ./.env:/app/.env
    restart: unless-stopped
    stop_grace_period: 120s
    # PDF worker 健康检查：检查进程是否存在
    healthcheck:
      test: ["CMD-SHELL", "pgrep -f 'queue:work' || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 10s
    deploy:
      resources:
        limits:
          cpus: '2'
          memory: 2G

networks:
  default:
    driver: bridge
