Skip to content

Instantly share code, notes, and snippets.

@rg3915
Forked from bahiamartins/celery_daemon.config
Created August 27, 2019 15:04
Show Gist options
  • Select an option

  • Save rg3915/122983cb204a6164e7a6f63229e3361e to your computer and use it in GitHub Desktop.

Select an option

Save rg3915/122983cb204a6164e7a6f63229e3361e to your computer and use it in GitHub Desktop.
Daemonization process of Celery and Celery Beat using Supervisord in AWS Elastic Beanstalk
files:
"/tmp/100_celery_worker.sh":
mode: "000755"
owner: root
group: root
content: |
#!/bin/sh
# for_log_and_pid
mkdir -p /var/log/celery/ /var/run/celery/
chmod 777 -R /var/run/celery/
# Get django environment variables
celeryenv=`cat /opt/python/current/env | tr '\n' ',' | sed 's/export //g' | sed 's/$PATH/%(ENV_PATH)s/g' | sed 's/$PYTHONPATH//g' | sed 's/$LD_LIBRARY_PATH//g' | sed 's/%/%%/g'`
celeryenv=${celeryenv%?}
# Create celery configuraiton script
celerydconf="[program:celery-worker]
; Set full path to celery program if using virtualenv
#replace [PROJ] with your project name
command=/opt/python/run/venv/bin/celery worker -A [PROJ] -P solo --loglevel=INFO
directory=/opt/python/current/app
user=nobody
numprocs=1
stdout_logfile=/var/log/celery/worker.log
stderr_logfile=/var/log/celery/worker.log
autostart=true
autorestart=true
startsecs=10
; Need to wait for currently executing tasks to finish at shutdown.
; Increase this if you have very long running tasks.
stopwaitsecs = 600
; When resorting to send SIGKILL to the program to terminate it
; send SIGKILL to its whole process group instead,
; taking care of its children as well.
killasgroup=true
; Set Celery priority higher than default (999)
; so, if rabbitmq is supervised, it will start first.
priority=998
environment=$celeryenv
[program:celery-beat]
; Set full path to celery program if using virtualenv
command=/opt/python/run/venv/bin/celery beat -A [PROJ] --loglevel=INFO -s /var/run/celery/celerybeat-schedule --pidfile="/var/run/celery/celerybeat.pid"
directory=/opt/python/current/app
user=nobody
numprocs=1
stdout_logfile=/var/log/celery-beat.log
stderr_logfile=/var/log/celery-beat.log
autostart=true
autorestart=true
startsecs=10
; Need to wait for currently executing tasks to finish at shutdown.
; Increase this if you have very long running tasks.
stopwaitsecs = 600
; When resorting to send SIGKILL to the program to terminate it
; send SIGKILL to its whole process group instead,
; taking care of its children as well.
killasgroup=true
; if rabbitmq is supervised, set its priority higher
; so it starts first
priority=998
environment=$celeryenv"
# Create the celery supervisord conf script
echo "$celerydconf" | tee /opt/python/etc/celeryd.conf
# Add configuration script to supervisord conf (if not there already)
if ! grep -Fxq "[include]" /opt/python/etc/supervisord.conf
then
echo "[include]" | tee -a /opt/python/etc/supervisord.conf
echo "files: celeryd.conf" | tee -a /opt/python/etc/supervisord.conf
fi
# Reread the supervisord config
/usr/local/bin/supervisorctl -c /opt/python/etc/supervisord.conf reread
# Update supervisord in cache without restarting all services
/usr/local/bin/supervisorctl -c /opt/python/etc/supervisord.conf update
# Start/Restart celeryd through supervisord
/usr/local/bin/supervisorctl -c /opt/python/etc/supervisord.conf restart celery-worker
/usr/local/bin/supervisorctl -c /opt/python/etc/supervisord.conf restart celery-beat
@ScottHull
Copy link

FYI this will not easily work on Amazon Linux 2...supervisor is not installed by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment