Skip to content

Commit e16bd8f

Browse files
committed
WG-62: Set up celery + redis on staging/prod servers
1 parent ec58da8 commit e16bd8f

6 files changed

Lines changed: 70 additions & 3 deletions

File tree

ansible/roles/hypha/defaults/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@ hypha_docker_tag: "wtsh-main"
2525
# The postgres version used
2626
hypha_postgres_version: 16
2727

28+
# The redis version used (for celery)
29+
hypha_redis_version: "8.4"
30+
2831
# The name of the system user that can deploy the site (used in github action)
2932
hypha_deploy_user: deploy-{{ app_name }}

ansible/roles/hypha/tasks/main.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
loop:
3636
- web
3737
- db
38+
- redis
39+
- celery
3840

3941
- name: Enable systemd service {{ hypha_project_name }}-{{ item }}
4042
ansible.builtin.systemd:
@@ -43,11 +45,16 @@
4345
loop:
4446
- web
4547
- db
48+
- redis
49+
- celery
4650

47-
- name: Start service {{ hypha_project_name }}-db
51+
- name: Start service {{ hypha_project_name }}-{{ item }}
4852
ansible.builtin.systemd:
49-
name: "{{ hypha_project_name }}-db"
53+
name: "{{ hypha_project_name }}-{{ item }}"
5054
state: started
55+
loop:
56+
- db
57+
- redis
5158

5259
- name: Make sure .env file exists
5360
ansible.builtin.template:

ansible/roles/hypha/templates/deploy.sh.jinja2

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ set -euo pipefail
33

44
CONTAINER_WEB={{hypha_project_name}}-web
55
SERVICE_WEB={{hypha_project_name}}-web
6+
SERVICE_CELERY={{hypha_project_name}}-celery
67
IMAGE={{hypha_docker_image}}{% if hypha_docker_tag %}:{{ hypha_docker_tag }}{% endif +%}
78
LOGFILE="{{ hypha_root_dir }}/deploy_$(date --iso).log"
89

@@ -28,7 +29,11 @@ systemctl daemon-reload
2829

2930
echolog "Restarting service $SERVICE_WEB..."
3031
systemctl restart $SERVICE_WEB
31-
echolog "Service restarted. Sleeping for a bit..."
32+
33+
echolog "Restarting celery $SERVICE_CELERY..."
34+
systemctl restart $SERVICE_CELERY
35+
36+
echolog "Services restarted. Sleeping for a bit..."
3237
sleep 5
3338
echolog "Done."
3439

ansible/roles/hypha/templates/hypha.env.jinja2

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ SECRET_KEY="{{ lookup('community.general.random_string', length=50) }}"
44
ALLOWED_HOSTS={{ hypha_external_domain }}
55
PRIMARY_HOST={{ hypha_external_domain }}
66
DATABASE_URL="postgres://hypha@{{ hypha_project_name }}-db/hypha"
7+
REDIS_URL="redis://{{ hypha_project_name }}-redis:6379"
8+
9+
# Activate background tasks:
10+
CELERY_TASK_ALWAYS_EAGER=0
711

812

913
# Enable basic auth out of the box
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[Unit]
2+
Description=Hypha celery docker container ({{ app_name }})
3+
After=docker.service
4+
Wants=network-online.target docker.socket
5+
Requires=docker.socket
6+
7+
[Service]
8+
# Automatically restart this unit when web is restarted:
9+
PartOf={{ hypha_project_name }}-web
10+
Restart=on-failure
11+
ExecStart=/usr/bin/docker run \
12+
--name {{ hypha_project_name }}-celery \
13+
--pull never \
14+
--rm \
15+
--env PYTHONDONTWRITEBYTECODE=1 \
16+
--env PYTHONUNBUFFERED=1 \
17+
--network {{ hypha_docker_network }} \
18+
--volume {{ hypha_root_dir }}/media:/app/media \
19+
--mount type=bind,src={{ hypha_root_dir }}/hypha.env,dst=/app/.env,ro \
20+
--mount type=bind,src={{ hypha_root_dir }}/local_settings.py,dst=/app/hypha/settings/local.py,ro \
21+
{{ hypha_docker_image }}{% if hypha_docker_tag %}:{{ hypha_docker_tag }}{% endif +%}
22+
python -m celery --app={{ app_name }} worker -l INFO
23+
ExecStop=/usr/bin/docker container stop \
24+
--timeout 10 \
25+
{{ hypha_project_name }}-celery
26+
27+
[Install]
28+
WantedBy=multi-user.target
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[Unit]
2+
Description=Hypha redis docker container ({{ app_name }})
3+
After=docker.service
4+
Wants=network-online.target docker.socket
5+
Requires=docker.socket
6+
7+
[Service]
8+
Restart=on-failure
9+
ExecStart=/usr/bin/docker run \
10+
--name {{ hypha_project_name }}-redis \
11+
--pull missing \
12+
--rm \
13+
--network {{ hypha_docker_network }} \
14+
redis:{{ hypha_redis_version }}
15+
ExecStop=/usr/bin/docker container stop \
16+
--timeout 10 \
17+
{{ hypha_project_name }}-redis
18+
19+
[Install]
20+
WantedBy=multi-user.target

0 commit comments

Comments
 (0)