django-celery-beat不会在主管内手动启动

时间:2019-01-15 14:26:58

标签: django django-celery amazon-elastic-beanstalk celerybeat ebextensions

我在Django 1.10项目中安装了Celery [sqs]和django-celery-beat。 我试图同时使用Supervisor和Elastic Beanstalk实例运行它们(工作程序和节拍)。

正在使用以下脚本动态创建Supervisor配置:

#!/usr/bin/env bash

# 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 beat config script
celerybeatconf="[program:celery-beat]
; Set full path to celery program if using virtualenv
command=/opt/python/run/venv/bin/celery beat -A phsite --loglevel=DEBUG --workdir=/tmp -S django --pidfile /tmp/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=false
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 = 10

; 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 celery worker config script
celeryworkerconf="[program:celery-worker]
; Set full path to celery program if using virtualenv
command=/opt/python/run/venv/bin/celery worker -A phsite --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

; if rabbitmq is supervised, set its priority higher
; so it starts first
priority=999

environment=$celeryenv"

# create files for the scripts
echo "$celerybeatconf" | tee /opt/python/etc/celerybeat.conf
echo "$celeryworkerconf" | tee /opt/python/etc/celeryworker.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: celerybeat.conf celeryworker.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

之后将运行以下ebextension:

container_commands:
  01_create_celery_beat_configuration_file:
    command: "cat .ebextensions/files/celery_configuration.sh > /opt/elasticbeanstalk/hooks/appdeploy/post/run_supervised_celeryd.sh && chmod 744 /opt/elasticbeanstalk/hooks/appdeploy/post/run_supervised_celeryd.sh && sed -i 's/\r$//' /opt/elasticbeanstalk/hooks/appdeploy/post/run_supervised_celeryd.sh"
  02_chmod_supervisor_sock:
    command: "chmod 777 /opt/python/run/supervisor.sock"
  03_create_logs:
    command: "touch /var/log/celery-beat.log /var/log/celery-worker.log"
  04_chmod_logs:
    command: "chmod 777 /var/log/celery-beat.log /var/log/celery-worker.log"
  05_start_celery_worker:
    command: "/usr/local/bin/supervisorctl -c /opt/python/etc/supervisord.conf restart celery-worker"
  06_start_celery_beat:
    command: "/usr/local/bin/supervisorctl -c /opt/python/etc/supervisord.conf start celery-beat"

登录到实例并运行时

/usr/local/bin/supervisorctl -c /opt/python/etc/supervisord.conf status
celery-worker运行时,

celery-beat已经“未启动”(日志文件为空)。 最奇怪的部分是如果我手动运行它(例如

/usr/local/bin/supervisorctl -c /opt/python/etc/supervisord.conf start celery-beat

它正在正常运行。

任何人都知道如何调试它吗? 为什么以后再加载时不加载到eb_extension中? 也许与Django尚未启动并且正在使用django_celery_beat.schedulers:DatabaseScheduler配置有关吗?

1 个答案:

答案 0 :(得分:1)

简单的原因是,在eb_extension中创建的shell脚本:

container_commands:
  01_create_celery_beat_configuration_file:
    command: "cat .ebextensions/files/celery_configuration.sh > /opt/elasticbeanstalk/hooks/appdeploy/post/run_supervised_celeryd.sh && chmod 744 /opt/elasticbeanstalk/hooks/appdeploy/post/run_supervised_celeryd.sh && sed -i 's/\r$//' /opt/elasticbeanstalk/hooks/appdeploy/post/run_supervised_celeryd.sh"

是在appdeploy / post目录中创建的,因此在执行以下命令后运行(基本上是部署后)。 start / restart命令没有任何作用,因为shell脚本尚未注册这些服务。 ‍♂️