version: "3.6"

volumes:
  logs:
  media:
  postgres-data:
  static-files:

x-db-env: &db-env
  POSTGRES_HOST:
  POSTGRES_PORT:
  POSTGRES_DB:
  POSTGRES_USER:
  POSTGRES_PASSWORD:

x-django-build-env: &django-build-env
  <<: *db-env
  DJANGO_SECRET_KEY:
  DJANGO_SETTINGS_MODULE:

x-django-env: &django-env
  <<: *django-build-env

  # Email
  EMAIL_HOST:
  EMAIL_PORT:
  EMAIL_HOST_USER:
  EMAIL_HOST_PASSWORD:
  EMAIL_USE_TLS:
  EMAIL_USE_SSL:
  EMAIL_DEFAULT_FROM:

  # Reddit
  REDDIT_CLIENT_ID:
  REDDIT_CLIENT_SECRET:
  REDDIT_CALLBACK_URL:

  # Twitter
  TWITTER_CONSUMER_ID:
  TWITTER_CONSUMER_SECRET:
  TWITTER_REDIRECT_URL:

  # Sentry
  SENTRY_DSN:

services:
  db:
    environment:
      <<: *db-env
    image: postgres:15
    healthcheck:
      # Note that --env-file should be used to set these correctly
      test: /usr/bin/pg_isready --username="${POSTGRES_USER}" --dbname="${POSTGRES_DB}"
      interval: 5s
      timeout: 10s
      retries: 10
    volumes:
      - postgres-data:/var/lib/postgresql/data

  rabbitmq:
    image: rabbitmq:3.12

  memcached:
    image: memcached:1.6
    entrypoint:
      - memcached
      - -m 64

  celery:
    build:
      context: .
      dockerfile: ./docker/django
      target: production
      args:
        <<: *django-build-env
    environment:
      <<: *django-env
    command: |
      celery --app newsreader
      --workdir /app/src/
      worker --loglevel INFO
      --concurrency 2
      --beat
      --scheduler django
      -n worker1@%h
      -n worker2@%h
    depends_on:
      rabbitmq:
        condition: service_started
      memcached:
        condition: service_started
      db:
        condition: service_healthy
      django:
        condition: service_healthy
    volumes:
      - logs:/app/logs

  django:
    build:
      context: .
      dockerfile: ./docker/django
      target: production
      args:
        <<: *django-build-env
    environment:
        <<: *django-env
    entrypoint: /app/bin/docker-entrypoint.sh
    command: |
      gunicorn --bind 0.0.0.0:8000
      --workers 3
      --chdir /app/src/
      newsreader.wsgi:application
    healthcheck:
      test: /usr/bin/curl --fail http://django:8000 || exit 1
      interval: 30s
      timeout: 10s
      retries: 10
    depends_on:
      memcached:
        condition: service_started
      db:
        condition: service_healthy
    volumes:
      - logs:/app/logs
      - media:/app/media
      - static-files:/app/static
