version: "3.8"

services:
  anonymous_github:
    build: .
    restart: always
    image: tdurieux/anonymous_github:v2
    ports:
      - $EXPOSED_PORT:5000
    env_file:
      - ./.env
    volumes:
      - ./repositories:/app/repositories/
    environment:
      - PORT=5000
      - REDIS_HOSTNAME=redis
      - DB_HOSTNAME=mongodb
      - STREAMER_ENTRYPOINT=http://streamer:5000/
    healthcheck:
      test:
        - CMD
        - node
        - healthcheck.js
      interval: 10s
      timeout: 10s
      retries: 5
    depends_on:
      - mongodb
      - redis
      - streamer

  streamer:
    build: .
    restart: always
    image: tdurieux/anonymous_github:v2
    deploy:
      mode: replicated
      replicas: 4
      endpoint_mode: dnsrr
    entrypoint: ["node", "--require", "./opentelemetry.js", "./build/streamer/index.js"]
    env_file:
      - ./.env
    volumes:
      - ./repositories:/app/repositories/
    environment:
      - PORT=5000
      - SERVICE_NAME=Streamer
    healthcheck:
      test:
        - CMD
        - node
        - healthcheck.js
      interval: 10s
      timeout: 10s
      retries: 5

  redis:
    image: "redis:alpine"
    restart: always
    ports:
      - 127.0.0.1:6379:6379
    healthcheck:
      test:
        - CMD
        - redis-cli
        - ping
      interval: 10s
      timeout: 10s
      retries: 5

  mongodb:
    image: mongo:latest
    restart: on-failure
    environment:
      MONGO_INITDB_ROOT_USERNAME: $DB_USERNAME
      MONGO_INITDB_ROOT_PASSWORD: $DB_PASSWORD
    volumes:
      - mongodb_data_container:/data/db
    ports:
      - 127.0.0.1:27017:27017
    command: --quiet
    healthcheck:
      test:
        - CMD
        - mongosh
        - --eval
        - "db.adminCommand('ping')"
      interval: 10s
      timeout: 10s
      retries: 5

  opentelemetry:
    image: otel/opentelemetry-collector
    restart: always
    command: ["--config=/etc/otel-collector-config.yaml"]
    volumes:
      - ./opentelemetry-collector.yml:/etc/otel-collector-config.yaml
    depends_on:
      - jaeger
      - prometheus

  jaeger:
    image: jaegertracing/all-in-one:latest
    restart: always
    ports:
      - 127.0.0.1:16686:16686

  prometheus:
    image: prom/prometheus:latest
    restart: always
    volumes:
      - ./prometheus.yaml:/etc/prometheus/prometheus.yml
    ports:
      - 127.0.0.1:9090:9090

  mongodb-backup:
    image: tiredofit/db-backup
    links:
      - mongodb
    env_file:
      - ./.env
    volumes:
      - ./db_backups:/backup
    environment:
      - DB_TYPE=mongo
      - DB_HOST=mongodb
      - DB_DUMP_FREQ=120
      - DB_CLEANUP_TIME=500
      - COMPRESSION=XZ
      - DB_USER=$DB_USERNAME
      - DB_PASS=$DB_PASSWORD

    restart: always
volumes:
  mongodb_data_container:
