如何在已经守护进程的 Celery 进程中更改我的 CELERY_BROKER_URL?
How do I change my CELERY_BROKER_URL in an already daemonized Celery process?
我正在使用 Supervisord 守护我的 Celery worker。问题是我的 CELERY_BROKER_URL
中有一个拼写错误,工作人员没有正确连接到 RabbitMQ。
当我 运行 celery -A mysite report
它显示旧的环境变量。
我的/etc/supervisor/conf.d/celery.conf
文件不包含环境变量:
[program:celery]
command=/webapps/mysite/scripts/celery/celery_start
autostart=true
autorestart=true
user=myuser
stdout_logfile=/webapps/mysite/logs/celery.log
redirect_stderr = true
环境变量是通过我在 celery_start
脚本中的虚拟环境获取的:
#!/bin/sh
DJANGODIR=/webapps/mysite/mysite
# Activate the virtual environment.
cd $DJANGODIR
. /webapps/mysite/bin/activate
. /webapps/mysite/bin/postactivate
# Programs meant to be run under supervisor should not daemonize themselves
# (do not use --daemon).
exec celery -A mysite worker -E -l info --concurrency=2
当我在激活环境后检查 CELERY_BROKER_URL
环境变量时,它是正确的。我试过 supervisorctl restart celery
,它没有选择新的环境变量(celery -A mysite report
显示旧的 CELERY_BROKER_URL
)。我试过 supervisorctl shutdown
然后 supervisord
也不会选择新的环境变量。
当我 运行 ps aux | grep 'celery worker'
什么都没看到,大概是因为 Celery 被 Supervisor 守护,所以我不确定有什么方法可以完全破坏当前的 Celery 进程。
不管怎么说,感觉Celery 是没有pick 新的环境变量。我怎样才能做到这一点?
[编辑]
我在settings.py
中的Celery设置如下:
# Celery settings.
CELERY_BROKER_URL = os.environ.get(
'BROKER_URL', 'amqp://guest:guest@127.0.0.1//')
CELERY_TASK_SOFT_TIME_LIMIT = 60
CELERY_RESULT_BACKEND = 'django-db'
我的 mysite/celery.py
文件是:
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings.local')
APP = Celery('mysite')
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
APP.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
APP.autodiscover_tasks()
@APP.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
原来我为我的经纪人使用的密码由于某种原因包含无效字符。
密码是#qahrKscbW#3!HkMJg#jFcyaOR7HtK%j08Jt$yY2
。
发生的事情是我的 broker_url 无效,因此它默认返回 ampq://guest:password
而不是 ampq://myuser:#qahrKscbW#3!HkMJg#jFcyaOR7HtK%j08Jt$yY2@localhost/mysite
。
我将密码更改为仅使用字母数字字符并且有效。
我正在使用 Supervisord 守护我的 Celery worker。问题是我的 CELERY_BROKER_URL
中有一个拼写错误,工作人员没有正确连接到 RabbitMQ。
当我 运行 celery -A mysite report
它显示旧的环境变量。
我的/etc/supervisor/conf.d/celery.conf
文件不包含环境变量:
[program:celery]
command=/webapps/mysite/scripts/celery/celery_start
autostart=true
autorestart=true
user=myuser
stdout_logfile=/webapps/mysite/logs/celery.log
redirect_stderr = true
环境变量是通过我在 celery_start
脚本中的虚拟环境获取的:
#!/bin/sh
DJANGODIR=/webapps/mysite/mysite
# Activate the virtual environment.
cd $DJANGODIR
. /webapps/mysite/bin/activate
. /webapps/mysite/bin/postactivate
# Programs meant to be run under supervisor should not daemonize themselves
# (do not use --daemon).
exec celery -A mysite worker -E -l info --concurrency=2
当我在激活环境后检查 CELERY_BROKER_URL
环境变量时,它是正确的。我试过 supervisorctl restart celery
,它没有选择新的环境变量(celery -A mysite report
显示旧的 CELERY_BROKER_URL
)。我试过 supervisorctl shutdown
然后 supervisord
也不会选择新的环境变量。
当我 运行 ps aux | grep 'celery worker'
什么都没看到,大概是因为 Celery 被 Supervisor 守护,所以我不确定有什么方法可以完全破坏当前的 Celery 进程。
不管怎么说,感觉Celery 是没有pick 新的环境变量。我怎样才能做到这一点?
[编辑]
我在settings.py
中的Celery设置如下:
# Celery settings.
CELERY_BROKER_URL = os.environ.get(
'BROKER_URL', 'amqp://guest:guest@127.0.0.1//')
CELERY_TASK_SOFT_TIME_LIMIT = 60
CELERY_RESULT_BACKEND = 'django-db'
我的 mysite/celery.py
文件是:
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings.local')
APP = Celery('mysite')
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
APP.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
APP.autodiscover_tasks()
@APP.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
原来我为我的经纪人使用的密码由于某种原因包含无效字符。
密码是#qahrKscbW#3!HkMJg#jFcyaOR7HtK%j08Jt$yY2
。
发生的事情是我的 broker_url 无效,因此它默认返回 ampq://guest:password
而不是 ampq://myuser:#qahrKscbW#3!HkMJg#jFcyaOR7HtK%j08Jt$yY2@localhost/mysite
。
我将密码更改为仅使用字母数字字符并且有效。