# 查价中台 — 生产环境 Docker Compose
# 用法（在项目根目录）：
#   cp deploy/.env.production.example .env   # 编辑后
#   bash deploy/install.sh

name: chajia-prod

services:
  mysql:
    image: mysql:8.0
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: chajia
    volumes:
      - mysql_data:/var/lib/mysql
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      timeout: 5s
      retries: 12
      start_period: 30s
    networks:
      - internal

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    command: ["redis-server", "--appendonly", "yes"]
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 10
    networks:
      - internal

  next-app:
    build:
      context: ..
      dockerfile: Dockerfile
    restart: unless-stopped
    env_file:
      - ../.env
    environment:
      NODE_ENV: production
      DATABASE_URL: mysql://root:${MYSQL_ROOT_PASSWORD}@mysql:3306/chajia
      REDIS_URL: redis://redis:6379
    ports:
      - "127.0.0.1:${APP_PORT:-3000}:3000"
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "node -e \"require('http').get('http://127.0.0.1:3000',(r)=>process.exit(r.statusCode<500?0:1)).on('error',()=>process.exit(1))\"",
        ]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 90s
    networks:
      - internal

  rpa-worker:
    build:
      context: ..
      dockerfile: Dockerfile.worker
    restart: unless-stopped
    env_file:
      - ../.env
    environment:
      NODE_ENV: production
      DATABASE_URL: mysql://root:${MYSQL_ROOT_PASSWORD}@mysql:3306/chajia
      REDIS_URL: redis://redis:6379
      PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: "1"
    volumes:
      - rpa_state:/app/.rpa
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
      next-app:
        condition: service_started
    shm_size: "2gb"
    networks:
      - internal

  scheduler:
    build:
      context: ..
      dockerfile: Dockerfile.worker
    restart: unless-stopped
    command: ["npm", "run", "worker:scheduler"]
    env_file:
      - ../.env
    environment:
      NODE_ENV: production
      DATABASE_URL: mysql://root:${MYSQL_ROOT_PASSWORD}@mysql:3306/chajia
      REDIS_URL: redis://redis:6379
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - internal

volumes:
  mysql_data:
  redis_data:
  rpa_state:

networks:
  internal:
    driver: bridge
