我正在使用Supervisord守护我的芹菜工人。问题是我的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没有选择新的环境变量。我怎样才能做到这一点?

[编辑]
我在settings.py中的Celery设置如下:

# Celery settings.
CELERY_BROKER_URL = os.environ.get(
    'BROKER_URL', 'amqp://guest:[email protected]//')
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))

最佳答案

因此,您的问题在这里:

CELERY_BROKER_URL = os.environ.get(
    'BROKER_URL', 'amqp://guest:[email protected]//')


设置文件正在寻找环境变量BROKER_URL,但是您要设定的环境变量是CELERY_BROKER_URL。如果我们将settings.py更新为如下所示,则它应该可以工作:

CELERY_BROKER_URL = os.environ.get(
    'CELERY_BROKER_URL', 'amqp://guest:[email protected]//')

10-04 10:25