{% include 'roles/docker-compose/templates/base.yml.j2' %}

{% set service_name = ESPOCRM_SERVICE %}
  {{ service_name }}:
    {{ lookup('template', 'roles/docker-container/templates/build.yml.j2') | indent(4) }}
    container_name: {{ ESPOCRM_CONTAINER }}
    image: "{{ ESPOCRM_CUSTOM_IMAGE }}"
    init: true
    stop_signal: SIGTERM
    stop_grace_period: 30s
{% include 'roles/docker-container/templates/base.yml.j2' %}
{% include 'roles/docker-container/templates/healthcheck/curl.yml.j2' %}
    ports:
      - "127.0.0.1:{{ ports.localhost.http[application_id] }}:80"
{% include 'roles/docker-container/templates/depends_on/dmbs_excl.yml.j2' %}
{% include 'roles/docker-container/templates/networks.yml.j2' %}
    volumes:
      - data:/var/www/html/

{% set service_name = 'daemon' %}
  {{ service_name }}:
    image: "{{ ESPOCRM_CUSTOM_IMAGE }}"
    container_name: {{ ESPOCRM_CONTAINER }}_{{ service_name }}
    pull_policy: never
    init: true
    stop_signal: SIGTERM
    stop_grace_period: 30s
{% include 'roles/docker-container/templates/base.yml.j2' %}
    entrypoint: docker-daemon.sh
{% include 'roles/docker-container/templates/networks.yml.j2' %}
    volumes:
      - data:/var/www/html/
    depends_on:
      {{ ESPOCRM_SERVICE }}:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "php", "-r", "require '/var/www/html/bootstrap.php'; echo 'OK';"]
      interval: 30s
      timeout: 5s
      retries: 5
      start_period: 40s

{% set service_name = 'websocket' %}
  {{ service_name }}:
    image: "{{ ESPOCRM_CUSTOM_IMAGE }}"
    container_name: {{ ESPOCRM_CONTAINER }}_{{ service_name }}
    pull_policy: never
    init: true
    stop_signal: SIGTERM
    stop_grace_period: 30s
{% include 'roles/docker-container/templates/base.yml.j2' %}
    environment:
      - ESPOCRM_CONFIG_USE_WEB_SOCKET=true
      - ESPOCRM_CONFIG_WEB_SOCKET_URL={{ WEBSOCKET_PROTOCOL }}://{{ domains | get_domain(application_id) }}/ws
      - ESPOCRM_CONFIG_WEB_SOCKET_ZERO_M_Q_SUBSCRIBER_DSN=tcp://*:7777
      - ESPOCRM_CONFIG_WEB_SOCKET_ZERO_M_Q_SUBMISSION_DSN=tcp://websocket:7777
    entrypoint: docker-websocket.sh
{% include 'roles/docker-container/templates/depends_on/dmbs_excl.yml.j2' %}
{% include 'roles/docker-container/templates/networks.yml.j2' %}
    volumes:
      - data:/var/www/html/
    ports:
      - "127.0.0.1:{{ ports.localhost.websocket[application_id] }}:8080"
    depends_on:
      {{ ESPOCRM_SERVICE }}:
        condition: service_healthy

{% include 'roles/docker-compose/templates/volumes.yml.j2' %}
  data:
    name: {{ ESPOCRM_DATA_VOLUME }}

{% include 'roles/docker-compose/templates/networks.yml.j2' %}