From 60762fec183007733a681f64c500f20acda0bb7e Mon Sep 17 00:00:00 2001 From: AuTN Date: Thu, 26 Oct 2023 21:43:48 +0700 Subject: [PATCH] Add celery beat --- group_vars/development/vars.yml | 1 + group_vars/vagrant/vars.yml | 1 + roles/celery/handlers/main.yml | 4 +++ roles/celery/tasks/copy_scripts.yml | 7 +++++ roles/celery/tasks/setup_supervisor.yml | 27 +++++++++++++++++++ roles/celery/templates/celery_beat_start.j2 | 11 ++++++++ .../templates/supervisor_celery_beat.conf.j2 | 10 +++++++ roles/celery/vars/main.yml | 4 +++ 8 files changed, 65 insertions(+) create mode 100644 roles/celery/templates/celery_beat_start.j2 create mode 100644 roles/celery/templates/supervisor_celery_beat.conf.j2 diff --git a/group_vars/development/vars.yml b/group_vars/development/vars.yml index fa1e756c..b84eb531 100644 --- a/group_vars/development/vars.yml +++ b/group_vars/development/vars.yml @@ -53,6 +53,7 @@ rabbitmq_application_password: password # Celery settings. celery_num_workers: 2 +use_celery_beat: false # Application settings. diff --git a/group_vars/vagrant/vars.yml b/group_vars/vagrant/vars.yml index cb55de4a..b1e435d7 100644 --- a/group_vars/vagrant/vars.yml +++ b/group_vars/vagrant/vars.yml @@ -53,6 +53,7 @@ rabbitmq_application_password: password # Celery settings. celery_num_workers: 2 +use_celery_beat: false # Application settings. diff --git a/roles/celery/handlers/main.yml b/roles/celery/handlers/main.yml index 0d5956d0..6ab19451 100644 --- a/roles/celery/handlers/main.yml +++ b/roles/celery/handlers/main.yml @@ -2,3 +2,7 @@ - name: restart {{ celery_application_name }} community.general.supervisorctl: name={{ celery_application_name }} state=restarted + +- name: Restart {{ celery_beat_application_name }} + community.general.supervisorctl: name={{ celery_beat_application_name }} state=restarted + when: use_celery_beat is defined diff --git a/roles/celery/tasks/copy_scripts.yml b/roles/celery/tasks/copy_scripts.yml index b0f0e311..0a707494 100644 --- a/roles/celery/tasks/copy_scripts.yml +++ b/roles/celery/tasks/copy_scripts.yml @@ -13,3 +13,10 @@ owner={{ celery_user }} group={{ celery_group }} mode=0755 + +- name: Create the script file for {{ celery_beat_application_name }} + ansible.builtin.template: src={{ celery_beat_template_file }} + dest={{ celery_scripts_dir }}/{{ celery_beat_application_name }}_start + owner={{ celery_user }} + group={{ celery_group }} + mode=0755 diff --git a/roles/celery/tasks/setup_supervisor.yml b/roles/celery/tasks/setup_supervisor.yml index 91624c30..1767a631 100644 --- a/roles/celery/tasks/setup_supervisor.yml +++ b/roles/celery/tasks/setup_supervisor.yml @@ -45,3 +45,30 @@ - name: Re-read the Supervisor config files community.general.supervisorctl: name={{ celery_application_name }} state=present + +- name: Create the Supervisor config file for {{ celery_beat_application_name }} + ansible.builtin.template: src=supervisor_{{ celery_beat_application_name }}.conf.j2 + dest=/etc/supervisor/conf.d/{{ celery_beat_application_name }}.conf + mode=0644 + +- name: Check for an existing celery_beat logfile + ansible.builtin.stat: + path: "{{ celery_beat_log_file }}" + register: b + +- name: Create (or retain) the log file for {{ celery_beat_application_name }} + # Removing until https://github.com/ansible/ansible/issues/45530 gets resolved. + # ansible.builtin.copy: content="" + # dest={{ celery_beat_log_file }} + # owner={{ celery_user }} + # group={{ celery_group }} + # force=no + ansible.builtin.file: + path: "{{ celery_beat_log_file }}" + owner: "{{ celery_user }}" + group: "{{ celery_group }}" + state: '{{ "file" if b.stat.exists else "touch" }}' + mode: "0644" + +- name: Re-read the Supervisor celery_beat config files + community.general.supervisorctl: name={{ celery_beat_application_name }} state=present diff --git a/roles/celery/templates/celery_beat_start.j2 b/roles/celery/templates/celery_beat_start.j2 new file mode 100644 index 00000000..caa4e9e0 --- /dev/null +++ b/roles/celery/templates/celery_beat_start.j2 @@ -0,0 +1,11 @@ +#!/bin/sh + +DJANGODIR={{ project_path }} + +# Activate the virtual environment. +cd $DJANGODIR +. {{ virtualenv_path }}/bin/activate +. {{ virtualenv_path }}/bin/postactivate + +# require to install djang_celery_beat package: https://github.com/celery/django-celery-beat +exec celery -A {{ application_name }} beat -l info -S django diff --git a/roles/celery/templates/supervisor_celery_beat.conf.j2 b/roles/celery/templates/supervisor_celery_beat.conf.j2 new file mode 100644 index 00000000..2d433efb --- /dev/null +++ b/roles/celery/templates/supervisor_celery_beat.conf.j2 @@ -0,0 +1,10 @@ +[program:{{ celery_beat_application_name }}] +command={{ celery_scripts_dir }}/{{ celery_beat_application_name }}_start + +autostart=true +autorestart=true + +user={{ celery_user }} + +stdout_logfile={{ celery_beat_log_file }} +redirect_stderr = true diff --git a/roles/celery/vars/main.yml b/roles/celery/vars/main.yml index c8baed1d..d6fb29ab 100644 --- a/roles/celery/vars/main.yml +++ b/roles/celery/vars/main.yml @@ -12,5 +12,9 @@ celery_application_name: celery celery_scripts_dir: "{{ virtualenv_path }}/scripts/celery" celery_template_file: "{{ celery_application_name }}_start.j2" +celery_beat_application_name: celery_beat +celery_beat_template_file: "{{ celery_beat_application_name }}_start.j2" + celery_log_dir: "{{ virtualenv_path }}/logs" celery_log_file: "{{ celery_log_dir }}/{{ celery_application_name }}.log" +celery_beat_log_file: "{{ celery_log_dir }}/{{ celery_beat_application_name }}.log"