Skip to content

Commit

Permalink
Merge pull request #199 from autn/feature/add-celery-beat
Browse files Browse the repository at this point in the history
Add celery beat
  • Loading branch information
YPCrumble authored Nov 2, 2023
2 parents ef5980e + 60762fe commit 11941a5
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions group_vars/development/vars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ rabbitmq_application_password: password

# Celery settings.
celery_num_workers: 2
use_celery_beat: false


# Application settings.
Expand Down
1 change: 1 addition & 0 deletions group_vars/vagrant/vars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ rabbitmq_application_password: password

# Celery settings.
celery_num_workers: 2
use_celery_beat: false


# Application settings.
Expand Down
4 changes: 4 additions & 0 deletions roles/celery/handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions roles/celery/tasks/copy_scripts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 27 additions & 0 deletions roles/celery/tasks/setup_supervisor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions roles/celery/templates/celery_beat_start.j2
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions roles/celery/templates/supervisor_celery_beat.conf.j2
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions roles/celery/vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 11941a5

Please sign in to comment.