services:
  db:
    image: postgres:15
    container_name: portacode-db
    restart: unless-stopped
    env_file:
      - main.env
    volumes:
      - pgdata:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U portacode -d portacode"]
      interval: 5s
      timeout: 5s
      retries: 5
    logging:
        driver: json-file
        options:
          max-size: "50m"   # rotate when log hits 50 MB
          max-file: "3"     # keep 3 old files

  django:
    build:
      context: ./server/portacode_django
      dockerfile: Dockerfile
    container_name: portacode-django
    restart: unless-stopped
    command: ["bash", "-c", "rm -rf staticfiles && python manage.py migrate && python manage.py collectstatic --noinput && uvicorn portacode_django.asgi:application --host 0.0.0.0 --port 8001"]
    env_file:
      - main.env
    depends_on:
      db:
        condition: service_healthy
    ports:
      - "8001:8001"
    volumes:
      - ./server/portacode_django:/app
    logging:
        driver: json-file
        options:
          max-size: "50m"   # rotate when log hits 50 MB
          max-file: "3"     # keep 3 old files

  unibot-worker:
    build:
      context: ./server/portacode_django
      dockerfile: Dockerfile
    restart: unless-stopped
    command: ["python", "manage.py", "run_worker"]
    env_file:
      - main.env
    depends_on:
      db:
        condition: service_healthy
    volumes:
      - ./server/portacode_django:/app
    deploy:
      replicas: 3
    logging:
        driver: json-file
        options:
          max-size: "50m"   # rotate when log hits 50 MB
          max-file: "3"     # keep 3 old files

  imap-worker:
    build:
      context: ./server/portacode_django
      dockerfile: Dockerfile
    container_name: portacode-imap-worker
    restart: unless-stopped
    command: ["python", "manage.py", "start_imap_listeners"]
    env_file:
      - main.env
    depends_on:
      db:
        condition: service_healthy
    volumes:
      - ./server/portacode_django:/app
    logging:
        driver: json-file
        options:
          max-size: "50m"   # rotate when log hits 50 MB
          max-file: "3"     # keep 3 old files
  automation-worker:
    build:
      context: ./server/portacode_django
      dockerfile: Dockerfile
    restart: unless-stopped
    command: ["python", "manage.py", "run_automation_workers", "--workers", "3"]
    env_file:
      - main.env
      - automation-worker.env
    depends_on:
      db:
        condition: service_healthy
    volumes:
      - ./server/portacode_django:/app
    deploy:
      replicas: 3
    logging:
        driver: json-file
        options:
          max-size: "50m"
          max-file: "3"
  expiry-notifier:
    build:
      context: ./server/portacode_django
      dockerfile: Dockerfile
    restart: unless-stopped
    command:
      [
        "python",
        "manage.py",
        "notify_expiring_devices",
        "--window-hours",
        "24",
        "--run-forever",
        "--interval-seconds",
        "1800",
      ]
    env_file:
      - main.env
    depends_on:
      db:
        condition: service_healthy
    volumes:
      - ./server/portacode_django:/app
    logging:
        driver: json-file
        options:
          max-size: "50m"
          max-file: "3"
  expired-cleanup-worker:
    build:
      context: ./server/portacode_django
      dockerfile: Dockerfile
    restart: unless-stopped
    command:
      [
        "python",
        "manage.py",
        "cleanup_expired_devices",
        "--run-forever",
        "--batch-size",
        "20",
        "--poll-interval",
        "5",
      ]
    env_file:
      - main.env
    depends_on:
      db:
        condition: service_healthy
    volumes:
      - ./server/portacode_django:/app
    deploy:
      replicas: 3
    logging:
        driver: json-file
        options:
          max-size: "50m"
          max-file: "3"
  reacher:
    image: reacherhq/backend:latest
    env_file:
      - reacher.env
    ports:
      - "8080:8080"
    restart: always
    logging:
        driver: json-file
        options:
          max-size: "50m"   # rotate when log hits 50 MB
          max-file: "3"     # keep 3 old files
volumes:
  pgdata: 
