name: agentforms

services:
  relay:
    build: .
    container_name: agentforms-app
    user: "1000:1000"
    ports:
      - "5060:5060"
    volumes:
      - ./data:/app/data
      - ./app:/app/app
      - ./tests:/app/tests
      - ./alembic.ini:/app/alembic.ini:ro
      - ./alembic:/app/alembic:rw
    tmpfs:
      - /app/app/__pycache__:nosuid,noexec,nodev
    env_file:
      - ./data/.env
    environment:
      - REDIS_URL=redis://redis:6379/0
    depends_on:
      redis:
        condition: service_healthy
    restart: unless-stopped
    deploy:
      resources:
        limits:
          cpus: "2.0"
          memory: "1G"
        reservations:
          cpus: "0.5"
          memory: "256M"
    healthcheck:
      test: ["CMD", "python3", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:5060/health/ready', timeout=5)"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 15s
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

  worker:
    build: .
    container_name: agentforms-worker
    user: "1000:1000"
    command: python3 -m app.worker
    volumes:
      - ./data:/app/data
      - ./app:/app/app
      - ./tests:/app/tests
    tmpfs:
      - /app/app/__pycache__:nosuid,noexec,nodev
    env_file:
      - ./data/.env
    environment:
      - REDIS_URL=redis://redis:6379/0
    depends_on:
      redis:
        condition: service_healthy
    restart: unless-stopped
    deploy:
      resources:
        limits:
          cpus: "1.0"
          memory: "512M"
        reservations:
          cpus: "0.25"
          memory: "128M"
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"
    healthcheck:
      test: ["CMD", "python3", "-c", "import redis, os, time; r=redis.from_url(os.environ.get('REDIS_URL','redis://localhost:6379/0')); hb=r.get('agentforms:worker:heartbeat'); assert hb is not None and (time.time()-float(hb))<180, 'worker heartbeat stale'"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 20s

  redis:
    image: redis:7-alpine
    container_name: agentforms-redis
    volumes:
      - redis-data:/data
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    logging:
      driver: json-file
      options:
        max-size: "1m"
        max-file: "2"

volumes:
  redis-data:
