version: "3"

services:
  db:
    image: postgres:13-alpine
    container_name: social_db
    restart: always
    environment:
      - POSTGRES_HOST_AUTH_METHOD=trust
    volumes:
      - ./docker/data/db/data:/var/lib/postgresql/data
      - ./docker/data/db/backup:/backup
      - ./etc/init-user.sql:/docker-entrypoint-initdb.d/init-user.sql
    ports:
      - "8432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5


  app:
    build:
      context: .
      args:
        DEV: "true"
    ports:
      - "8080:8080"
    restart: always
    container_name: social_app
    env_file:
      - .env
    links:
      - db
    depends_on:
      db:
        condition: service_healthy
    command: sh -c "aerich upgrade && uvicorn main:app --reload --host 0.0.0.0 --port 8080"
    volumes:
      - ./src:/src/

  rabbitmq:
    container_name: social_rabbitmq
    image: rabbitmq:3-management-alpine
    ports:
      - "3672:5672"
      - "14672:15672"
    healthcheck:
      test: ["CMD", "rabbitmqctl", "status"]
      interval: 10s
      timeout: 5s
      retries: 5

  taskiq-worker:
    build:
      context: .
      dockerfile: Dockerfile
    image: social_taskiq
    container_name: social_taskiq
    env_file: .env
    restart: always
    volumes:
      - ./src:/src
    links:
      - db
      - rabbitmq
    depends_on:
      db:
        condition: service_healthy
      rabbitmq:
        condition: service_healthy
    command: [ taskiq, worker, core.tkq:broker, -fsd]
    ports: []

  taskiq-faststream-scheduler:
    build:
      context: .
      dockerfile: Dockerfile
    image: social_taskiq_faststream_scheduler
    container_name: social_taskiq_faststream_scheduler
    env_file: .env
    restart: always
    volumes:
      - ./src:/src
    links:
      - db
      - rabbitmq
    depends_on:
      db:
        condition: service_healthy
      rabbitmq:
        condition: service_healthy
    command: [ taskiq, scheduler, core.tkq_faststream:scheduler, -fsd]
    ports: []

  taskiq-scheduler:
    build:
      context: .
      dockerfile: Dockerfile
    image: social_taskiq_scheduler
    container_name: social_taskiq_scheduler
    env_file: .env
    restart: always
    volumes:
      - ./src:/src
    links:
      - db
      - rabbitmq
    depends_on:
      db:
        condition: service_healthy
      rabbitmq:
        condition: service_healthy
    command: [ taskiq, scheduler, core.tkq:scheduler, -fsd]
    ports: []

  redis:
    image: redis:6.0-alpine
    container_name: social_redis
    restart: always
    volumes:
      - ./docker/data/redis:/data
    ports:
      - 6377:6379
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  faststream_worker:
    build:
      context: .
      dockerfile: Dockerfile
    env_file: .env
    links:
      - db
      - redis
    depends_on:
      app:
        condition: service_started
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    command: python core/faststream_worker.py
