# Production compose — deployed to 192.168.1.13 /home/app/GovCrawler/docker.
#
# Differences from the repo-root docker-compose.yml (dev):
#   • No embedded postgres — the app talks to the external MySQL at
#     192.168.1.222:3306/GovCrawler (credentials in ../.env).
#   • Build context is the repo root (one level up), so `docker compose
#     -f docker/docker-compose.prod.yml up -d --build` reuses the existing
#     BuildKit layer cache on the server.
#   • Data volumes live under ../data relative to this file so the host
#     layout stays `/home/app/GovCrawler/data/{govcrawler,valkey}`.
#
# Bring-up on the server (cwd = /home/app/GovCrawler/docker):
#   docker compose -f docker-compose.prod.yml pull valkey   # optional
#   docker compose -f docker-compose.prod.yml up -d --build
#   docker compose -f docker-compose.prod.yml logs -f api

services:
  valkey:
    # BSD-3 Redis fork — cookie pool for the fetcher
    image: valkey/valkey:8-alpine
    command: ["valkey-server", "--appendonly", "no", "--save", ""]
    ports: ["6380:6379"]
    volumes: ["../data/valkey:/data"]
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "valkey-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 10

  scheduler:
    build:
      context: ..
      dockerfile: Dockerfile
      args:
        # Browser binaries are bind-mounted from the host
        # (../data/ms-playwright). Skip the build-time install — npmmirror
        # is missing CFT 145.x and the official mirror times out from this
        # network, both of which used to make rebuilds hang ~15 min.
        SKIP_CHROMIUM: "1"
        PLAYWRIGHT_DOWNLOAD_HOST: ""
    depends_on:
      valkey: { condition: service_healthy }
      api: { condition: service_started }
    env_file:
      - ../.env
    environment:
      DATA_DIR: /data/govcrawler
      VALKEY_URL: redis://valkey:6379/0
      TZ: Asia/Shanghai
      # Dispatch cron jobs through the api's per-site queue so manual +
      # scheduled crawls stay serialized per site.
      SCHEDULER_API_URL: http://api:8787
    volumes:
      - ../data/govcrawler:/data/govcrawler
      - ../config:/app/config:ro
      # Persist patchright/Chromium binaries across rebuilds. The Dockerfile
      # tries to install during build but silently falls through on
      # download failures (npmmirror missing CFT 145.x → 404). Bind-mounted
      # host directory keeps a known-good copy survives every up --build.
      - ../data/ms-playwright:/root/.cache/ms-playwright
    restart: unless-stopped
    command: ["python", "-m", "govcrawler", "schedule"]
    healthcheck:
      test: ["CMD", "python", "-c", "import sys; sys.exit(0)"]
      interval: 60s
      timeout: 10s
      retries: 3

  api:
    build:
      context: ..
      dockerfile: Dockerfile
      args:
        # Browser binaries are bind-mounted from the host
        # (../data/ms-playwright). Skip the build-time install — npmmirror
        # is missing CFT 145.x and the official mirror times out from this
        # network, both of which used to make rebuilds hang ~15 min.
        SKIP_CHROMIUM: "1"
        PLAYWRIGHT_DOWNLOAD_HOST: ""
    env_file:
      - ../.env
    environment:
      DATA_DIR: /data/govcrawler
      TZ: Asia/Shanghai
    volumes:
      # api needs write access — manual /run from dashboard hits the same
      # crawl pipeline that scheduler does (saves raw_html / articles_text /
      # attachments under /data/govcrawler).
      - ../data/govcrawler:/data/govcrawler
      - ../config:/app/config:ro
      # Same persistence as scheduler — share the chromium binary so both
      # services see it without each running its own download.
      - ../data/ms-playwright:/root/.cache/ms-playwright
    ports: ["8787:8787"]
    restart: unless-stopped
    command: ["python", "-m", "govcrawler", "serve", "--host", "0.0.0.0", "--port", "8787"]
