---
- name: Créer le répertoire de logs
  file:
    path: "/var/log/laravel-worker"
    state: directory
    owner: www-data
    group: www-data
    mode: '0755'

- name: Déployer le fichier de configuration
  copy:
    dest: /etc/supervisor/conf.d/{{ worker.name }}.conf
    content: |
      [program:{{ worker.name }}]
      command=/usr/bin/php{{ worker.php_version }} /var/www/{{ worker.package_name }}/artisan queue:work --queue={{ worker.queue }}{% if worker.options is defined %} {{ worker.options }}{% endif %}
      autostart=true
      autorestart=true
      stderr_logfile=/var/log/laravel-worker/{{ worker.name }}.err.log
      stdout_logfile=/var/log/laravel-worker/{{ worker.name }}.out.log
      stopasgroup=true
      killasgroup=true
  loop: "{{ laravel_workers }}"
  loop_control:
    loop_var: worker
    label: "{{ worker.name }}"

- name: Créer le fichier de log du worker
  ansible.builtin.file:
    path: "/var/log/laravel-worker/{{ worker.name }}.log"
    state: touch
    owner: www-data
    group: www-data
    mode: '0664'
  loop: "{{ laravel_workers }}"
  loop_control:
    loop_var: worker
    label: "{{ worker.name }}"

- name: Recharger la configuration de supervisord
  command: supervisorctl reread

- name: Mettre à jour les processus supervisord
  command: supervisorctl update

- name: Redémarrer les workers
  command: supervisorctl restart all
