我的工人Celery在localhost工作,但不在heroku工作

时间:2019-08-21 19:25:46

标签: django heroku celery

我在本地主机(windows)上设置了工作进程,它可以正常运行,但在heroku上却不起作用。

我正在使用Django,Reddis和Celery。

celery.py

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

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'eco_gestao.settings')

app = Celery('eco_gestao')

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

app.autodiscover_tasks()

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

Procfile:

web: gunicorn eco_gestao.wsgi --log-file -
worker: celery -A eco_gestao worker -l info

设置:

CELERY_RESULT_BACKEND = 'django-db'
CELERY_BROKER_URL = 'redis://'
# BROKER_URL = os.getenv('REDISTOGO_URL', 'redis://127.0.0.1:6380')
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'

redis_host = os.environ.get('REDIS_HOST', 'localhost')
# Channel layer definitions
# http://channels.readthedocs.org/en/latest/deploying.html#setting-up-a-channel-backend
CHANNEL_LAYERS = {
    "default": {
        # This example app uses the Redis channel layer implementation asgi_redis
        "BACKEND": "asgi_redis.RedisChannelLayer",
        "CONFIG": {
            "hosts": [(redis_host, 6379)],
        },
        "ROUTING": "multichat.routing.channel_routing",
    },
}

错误:

[2019-08-21 17:33:22,075: ERROR/MainProcess] consumer: Cannot connect to redis://localhost:6379//: Error 111 connecting to 127.0.0.1:6379. Connection refused.

1 个答案:

答案 0 :(得分:0)

您必须通过导出正确的REDIS_HOST值来配置在Heroku上运行的Celery以使用您的Heroku Redis(我看到您正在从环境中选择Redis主机)。显然,该值不会导出(这就是为什么您获得Cannot connect to redis://localhost:6379//的原因。)