django - 使用环境变量在 AWS Elastic Beanstalk 上通过 django 运行 celery

标签 django celery amazon-elastic-beanstalk

我想使用我的 Django 应用程序在 AWS Elastic Beanstalk 上运行 celery。我遵循了 @yellowcap ( How do you run a worker with AWS Elastic Beanstalk? ) 的这个很好的答案。所以我的 supervisord.conf 看起来像这样:

files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/run_supervised_celeryd.sh":
mode: "000755"
owner: root
group: root
content: |
  #!/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'`
  celeryenv=${celeryenv%?}

  # Create celery configuraiton script
  celeryconf="[program:celeryd]
  ; Set full path to celery program if using virtualenv
  command=/opt/python/run/venv/bin/celery worker -A myappname --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=998

  environment=$celeryenv"

  # Create the celery supervisord conf script
  echo "$celeryconf" | tee /opt/python/etc/celery.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: celery.conf" | tee -a /opt/python/etc/supervisord.conf
  fi

  # Reread the supervisord config
  supervisorctl -c /opt/python/etc/supervisord.conf reread

  # Update supervisord in cache without restarting all services
  supervisorctl -c /opt/python/etc/supervisord.conf update

  # Start/Restart celeryd through supervisord
  supervisorctl -c /opt/python/etc/supervisord.conf restart celeryd

他的代码运行良好,直到我决定将 settings.py 中的一些变量迁移到我的 Elastic Beanstalk 环境属性。

确实,调用脚本时出现以下错误:

for \'environment\' is badly formatted'>: file: /usr/lib64/python2.7/xmlrpclib.py line: 800
celeryd: ERROR (no such process)

感谢您的帮助。

最佳答案

这是由于 Supervisor 解析配置文件的方式所致 [1]。

您的环境设置包含未转义的 % 字符,可能来自 Django SECRET_KEY。

以下内容对我有用 - 尝试附加 | sed 's/%/%%/g' 到此处的管道链:

celeryenv=`cat/opt/python/current/env | tr '\n' ',' | sed 's/export//g' | sed 's/export//g' | sed 's/$PATH/%(ENV_PATH)s/g' | sed 's/$PATH/%(ENV_PATH)s/g' | sed 's/$PYTHONPATH//g' | sed 's/$LD_LIBRARY_PATH//g'`

结果行:

celeryenv=`cat/opt/python/current/env | tr '\n' ',' | sed 's/export//g' | sed 's/export//g' | sed 's/$PATH/%(ENV_PATH)s/g' | sed 's/$PATH/%(ENV_PATH)s/g' | sed 's/$PYTHONPATH//g' | sed 's/$LD_LIBRARY_PATH//g' | sed 's/%/%%/g'`

[1] https://github.com/Supervisor/supervisor/issues/291

关于django - 使用环境变量在 AWS Elastic Beanstalk 上通过 django 运行 celery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41231489/

相关文章:

python-3.x - Elastic Beanstalk CLI 部署 zip 错误

python - 扩建/先生。开发人员随机无法从 Git pull

python - Celery 与 Django 中的 Redis 代理 : tasks successfully execute, 但仍然存在太多持久的 Redis key 和连接

django - Celery 4.2-Django 递归错误 : maximum recursion depth exceeded

python - 用于 celery 的原始 PyMySQL 多线程

amazon-web-services - CloudFormation 中应用程序负载均衡器的 AWS::WAFv2::WebACLAssociation ResourceArn

node.js - 无法将我自己的 Node js部署到aws elastic beanstalk

ruby-on-rails - 哪个网络框架适合想要工作的人?

django - 如何使 IntegerField/CharField 成为 Django 中的自动递增字段,如 1000001、1000002

python - 如何访问在 Amazon ec2 上运行的 Django 应用程序?