name: e-ticket

services:
  php:
    build:
      context: ./docker/php/prod
      dockerfile: Dockerfile
    deploy:
      replicas: 2
      resources:
        limits:
          cpus: "1.5"
          memory: 1G
        reservations:
          cpus: "0.5"
          memory: 256M
    restart: unless-stopped
    group_add:
      - "{{ docker_gid }}"
    volumes:
      - .:/app
      - /var/run/docker.sock:/var/run/docker.sock:ro
    ports:
      - "4578-4579:9000"
    networks:
      - e-ticket
    depends_on:
      pgbouncer:
        condition: service_healthy
      redis:
        condition: service_healthy

  db-master:
    image: postgres:16-alpine
    restart: unless-stopped
    deploy:
      resources:
        limits:
          cpus: "2.0"
          memory: 4G
        reservations:
          cpus: "0.5"
          memory: 1G
    environment:
      POSTGRES_USER: e-ticket
      POSTGRES_PASSWORD: {{ db_password }}
      POSTGRES_DB: e-ticket
    command:
      - postgres
      - -c
      - wal_level=replica
      - -c
      - max_wal_senders=3
      - -c
      - wal_keep_size=64MB
      - -c
      - hot_standby=on
      - -c
      - shared_buffers=1GB
      - -c
      - effective_cache_size=3GB
      - -c
      - work_mem=16MB
    volumes:
      - db-master-data:/var/lib/postgresql/data
      - ./docker/pgsql/init-master.sh:/docker-entrypoint-initdb.d/init-master.sh
    networks:
      - e-ticket
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U e-ticket -d e-ticket"]
      interval: 5s
      timeout: 5s
      retries: 5

  db-slave:
    image: postgres:16-alpine
    restart: unless-stopped
    deploy:
      resources:
        limits:
          cpus: "1.5"
          memory: 2G
        reservations:
          cpus: "0.25"
          memory: 512M
    environment:
      POSTGRES_USER: e-ticket
      POSTGRES_PASSWORD: {{ db_password }}
      POSTGRES_DB: e-ticket
      PGDATA: /var/lib/postgresql/data
    volumes:
      - db-slave-data:/var/lib/postgresql/data
      - ./docker/pgsql/init-slave.sh:/init-slave.sh
    entrypoint: ["/bin/bash", "/init-slave.sh"]
    command: ["postgres"]
    networks:
      - e-ticket
    depends_on:
      db-master:
        condition: service_healthy
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U e-ticket -d e-ticket"]
      interval: 5s
      timeout: 5s
      retries: 5

  pgbouncer:
    image: edoburu/pgbouncer
    restart: unless-stopped
    deploy:
      resources:
        limits:
          cpus: "0.5"
          memory: 128M
        reservations:
          cpus: "0.1"
          memory: 32M
    volumes:
      - ./docker/pgsql/pgbouncer.ini:/etc/pgbouncer/pgbouncer.ini:ro
      - ./docker/pgsql/userlist.txt:/etc/pgbouncer/userlist.txt:ro
    networks:
      - e-ticket
    depends_on:
      db-master:
        condition: service_healthy
      db-slave:
        condition: service_healthy
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -h 127.0.0.1 -p 6432 -U e-ticket"]
      interval: 5s
      timeout: 5s
      retries: 5

  messenger:
    build:
      context: ./docker/php/prod
      dockerfile: Dockerfile
    command: php bin/console messenger:consume async --time-limit=3600 --memory-limit=256M --limit=500 -vv
    deploy:
      replicas: 2
      resources:
        limits:
          cpus: "1.0"
          memory: 512M
        reservations:
          cpus: "0.25"
          memory: 128M
    restart: unless-stopped
    volumes:
      - .:/app
    networks:
      - e-ticket
    depends_on:
      pgbouncer:
        condition: service_healthy
      redis:
        condition: service_healthy
      meilisearch:
        condition: service_started

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    deploy:
      resources:
        limits:
          cpus: "1.0"
          memory: 1G
        reservations:
          cpus: "0.25"
          memory: 128M
    command: redis-server --requirepass {{ redis_password }} --maxmemory 768mb --maxmemory-policy allkeys-lru
    volumes:
      - redis-data:/data
    networks:
      - e-ticket
    healthcheck:
      test: ["CMD", "redis-cli", "-a", "{{ redis_password }}", "ping"]
      interval: 5s
      timeout: 5s
      retries: 5

  meilisearch:
    image: getmeili/meilisearch:latest
    restart: unless-stopped
    deploy:
      resources:
        limits:
          cpus: "1.0"
          memory: 1G
        reservations:
          cpus: "0.25"
          memory: 256M
    environment:
      MEILI_MASTER_KEY: {{ meilisearch_master_key }}
      MEILI_ENV: production
      MEILI_NO_ANALYTICS: true
    volumes:
      - meilisearch-data:/meili_data
    networks:
      - e-ticket

networks:
  e-ticket:
    driver: bridge

volumes:
  db-master-data:
  db-slave-data:
  redis-data:
  meilisearch-data:
