如何在已经守护的Celery进程中更改CELERY_BROKER_URL?

问题描述 投票:1回答:2

我正在使用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))
django ubuntu celery supervisord
2个回答
0
投票

所以您的问题在这里:

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

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

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

0
投票

事实证明,我为经纪人使用的密码出于某种原因包含无效字符。

密码为#qahrKscbW#3!HkMJg#jFcyaOR7HtK%j08Jt$yY2

正在发生的事情是我的broker_url无效,因此默认情况下返回到ampq://guest:password而不是ampq://myuser:#qahrKscbW#3!HkMJg#jFcyaOR7HtK%j08Jt$yY2@localhost/mysite

我将密码更改为仅使用字母数字字符,并且可以使用。

© www.soinside.com 2019 - 2024. All rights reserved.