Celery 正在连接到 amqp://guest**@localhost 而不是 Redis。 Heroku部署

Celery is connecting to amqp://guest**@localhost instead of Redis. Heroku deploy

我已经阅读了类似的主题并完成了所有建议,但问题仍然存在。

我正在将我的应用程序部署到 Heroku。在本地一切正常,但在指定每个设置后的部署过程中,我可以考虑指定 celery worker 发送以下错误:

:22:50.722826+00:00 app[worker.1]: [2018-10-21 18:22:50,722: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 111] Connection refused.

我尝试从 CloudAMQP 切换到 Redis。问题依然存在。

这是我的配置文件:

Django 的 settings.py:

try: 
    CELERY_BROKER_URL = os.environ['REDIS_URL']
except KeyError: 
    CELERY_BROKER_URL = 'amqp://admin:admin@localhost:5672/admin_host'

try:
    CELERY_RESULT_BACKEND = os.environ['REDIS_URL']
except KeyError:
    CELERY_RESULT_BACKEND = 'amqp://admin:admin@localhost:5672/admin_host'


CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'

django_heroku.settings(locals())

celery.py

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from . import settings

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'file_upload.settings')

app = Celery('file_upload', broker_pool_limit=1)

app.config_from_object('django.conf:settings')

app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

__init__.py 来自包含 celery.py 和 settings.py 的包:

from __future__ import absolute_import, unicode_literals

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app

__all__ = ['celery_app']

并且 os.environ['REDIS_URL'] 做 return 一个 url,我检查过了。

有人帮忙吗?

与这个东西战斗了几天,在发布这个之后我回答了自己。有人可能会觉得这很有用。

在我的例子中,解决这个问题的关键是我在 celery.py 中对 redis URL 进行了硬编码,并在创建 celery 应用程序对象时将其作为参数传递:

app = Celery('file_upload', broker_pool_limit=1, broker=redis_url,
             result_backend=redis_url)

为了解决这个问题,我认为需要做的是将CELERY_BROKER_URL var 更改为BROKER_URL。如果在从对象中获取配置时给它命名空间 CELERY,它应该只为 CELERY_BROKER_URL。 更改自:

app.config_from_object('django.conf:settings')

收件人:

app.config_from_object('django.conf:settings', namespace='CELERY')

在 local/development 环境中,当我们使用 kombu 而不是 ampq 时,celery broker url 配置如下:

CELERY_BROKER_URL = 'django://'  # for example

在您的 celery.py 中,必须像这样设置对用于创建 celery 应用程序的构造函数的调用:

django_url = settings.CELERY_BROKER_URL
app = Celery('tone_insurance', broker=django_url, result_backend=django_url)

这是基于回答