version: "3.8"

# Worker services for {{ project_name }}
# Run background jobs locally with: docker compose -f docker-compose.workers.yml up
#
# In production, each worker runs on a dedicated Hetzner VM.

services:
{% for job in jobs %}
  {{ job.name }}:
    {% if job.docker_image %}
    image: {{ job.docker_image }}
    {% else %}
    image: {{ docker_registry }}/{{ project_name }}-backend:latest
    {% endif %}
    container_name: {{ project_name_snake }}_worker_{{ job.name | replace("-", "_") }}
    command: {{ job.command }}
    {% if job.restart_policy == "always" %}
    restart: always
    {% elif job.restart_policy == "on-failure" %}
    restart: "on-failure:{{ job.max_restarts }}"
    {% else %}
    restart: "no"
    {% endif %}
    {% if job.environment %}
    environment:
      {% for key, value in job.environment.items() %}
      {{ key }}: "{{ value }}"
      {% endfor %}
    {% endif %}
    deploy:
      resources:
        limits:
          cpus: "{{ job.cpu }}"
          memory: {{ job.memory_mb }}M
    {% if job.timeout_seconds %}
    stop_grace_period: {{ job.timeout_seconds }}s
    {% endif %}
    {% if job.schedule %}
    # Scheduled job: {{ job.schedule }}
    # In production, this runs via cron on the Hetzner VM.
    # Locally, the container runs once and exits.
    {% endif %}
    labels:
      prisme.managed: "true"
      prisme.job.name: "{{ job.name }}"
      {% if job.schedule %}
      prisme.job.schedule: "{{ job.schedule }}"
      {% endif %}
      prisme.job.restart_policy: "{{ job.restart_policy }}"

{% endfor %}
