环境变量不适用于主管经营的芹菜工人

时间:2015-06-13 20:06:15

标签: python environment-variables celery supervisord

当芹菜通过主管运行时,我在一些芹菜任务中读取环境变量时遇到了麻烦

/etc/supervisord.conf ...

...
[program:celery]
...
command = /home/myuser/mydevelopment/git/ers_data_app/env/bin/celery worker -A webapp.celery --loglevel=info
stdout_logfile=/home/webdev/logs/celeryworker.log
stderr_logfile=/home/myuser/logs/celeryworkererr.log
environment=FLASK_CONFIG="testing"
myapp/myfile.py ...

...
print 'the system config is', os.getenv('FLASK_CONFIG')
dburi = app_config.config[os.getenv('FLASK_CONFIG')].DATABASE_URI
celeryworker.log ...

the system config is None
celeryworkererr.log ...

...
File "/home/webdev/mydevelopment/git/ers_data_app/webapp/dbmodels/database.py", line 16, in <module>
    dburi = app_config.config[os.getenv('FLASK_CONFIG')].DATABASE_URI
KeyError: None

由于supervisor没有启动shell,因此以下内容是不必要的,但是为了完整性,这里就是这样。

/etc/profile.d/my_app.sh

中的

export FLASK_CONFIG='testing'

1 个答案:

答案 0 :(得分:0)

在supervisord块下的.conf文件中,您可以添加所有环境键=值对

[supervisord]
environment=CELERY_BROKER_URL="amqp://guest:guest@127.0.0.1:5672//",FLASK_CONFIG="TESTING"

[program:celeryd]
command=celery worker -A celery --loglevel=info -P gevent -c 1000

如果您不想对变量进行硬编码,但想从os环境中将其拉入,请在bash上执行第1步

导出env var

>> sudo export CELERY_BROKER_URL="amqp://guest:guest@127.0.0.1:5672//"

重新加载Bash

>> . ~/.bashrc

检查环境是否设置正确

>> env

现在修改conf文件以阅读 - Note:使用ENV_

添加env变量
[supervisord]
environment=CELERY_BROKER_URL="%(ENV_CELERY_BROKER_URL)s",FLASK_CONFIG="%(ENV_FLASK_CONFIG)s"

[program:celeryd]
command=celery worker -A celery --loglevel=info -P gevent -c 1000