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

My worker Celery works in localhost but not in heroku

我在我的本地主机 (windows) 上设置了 workers,它工作得很好,但在 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))

过程文件:

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.

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