services:
  app:
    extends:
      file: docker-compose.yml
      service: app
    depends_on:
      argilla:
        condition: service_healthy
        required: false
    environment:
      - ARGILLA_API_URL=http://argilla:6900

  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:8.17.0
    environment:
      - ES_JAVA_OPTS=-Xms512m -Xmx512m
      - node.name=elasticsearch
      - cluster.name=es-argilla-local
      - discovery.type=single-node
      - cluster.routing.allocation.disk.threshold_enabled=false
      - xpack.security.enabled=false
    volumes:
      - es_data:/usr/share/elasticsearch/data
    networks:
      - app-network
    ports:
      - "9200:9200"
      - "9300:9300"
    ulimits:
      memlock:
        soft: -1
        hard: -1
      nofile:
        soft: 65536
        hard: 65536
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9200"]
      interval: 30s
      timeout: 10s
      retries: 3

  postgres:
    image: postgres:14
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: argilla
    networks:
      - app-network
    volumes:
      - postgres_data:/var/lib/postgresql/data

  redis:
    image: redis
    networks:
      - app-network

  argilla:
    image: argilla/argilla-server:latest
    ports:
      - "6900:6900"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:6900/api/ready"]
      interval: 30s
      timeout: 10s
      retries: 3
    env_file:
      - .env
    environment:
      - ARGILLA_HOME_PATH=/var/lib/argilla
      - ARGILLA_ELASTICSEARCH=http://elasticsearch:9200
      - ARGILLA_DATABASE_URL=postgresql+asyncpg://postgres:postgres@postgres:5432/argilla
      - ARGILLA_REDIS_URL=redis://redis:6379/0
      - USERNAME=${ARGILLA_USERNAME}
      - PASSWORD=${ARGILLA_PASSWORD}
      - API_KEY=${ARGILLA_API_KEY}
      - WORKSPACE=default
    volumes:
      - argilla_data:/argilla
    networks:
      - app-network
    depends_on:
      elasticsearch:
        condition: service_healthy
      postgres:
        condition: service_started
      redis:
        condition: service_started

  worker:
    image: argilla/argilla-server:latest
    env_file:
      - .env
    environment:
      - ARGILLA_HOME_PATH=/var/lib/argilla
      - ARGILLA_ELASTICSEARCH=http://elasticsearch:9200
      - ARGILLA_DATABASE_URL=postgresql+asyncpg://postgres:postgres@postgres:5432/argilla
      - ARGILLA_REDIS_URL=redis://redis:6379/0
      - BACKGROUND_NUM_WORKERS=2
      - USERNAME=${ARGILLA_USERNAME}
      - PASSWORD=${ARGILLA_PASSWORD}
      - API_KEY=${ARGILLA_API_KEY}
      - WORKSPACE=default
    networks:
      - app-network
    depends_on:
      - postgres
      - elasticsearch
      - redis
    command: sh -c 'python -m argilla_server worker --num-workers $${BACKGROUND_NUM_WORKERS}'

volumes:
  es_data:
    name: synthetic-data-es
  argilla_data:
    name: synthetic-data-argilla
  postgres_data:
    name: synthetic-data-postgres