在过去的几天里,我一直在尝试为我的网站进行设置,但一直未能理解这个特定的错误。
- 我为我的项目使用了 Django Cookiecutter 锅炉模板。
- 我已经在 Redis 本地添加了 Celery 来执行任务,效果
很好。 - 我已将我的项目部署到 Heroku。
我现在正尝试在我的生产环境中实现相同的任务,但我现在使用的是名为 Redis To Go 的 heroku 插件而不是 Redis。我这样做了;
在我的设置中:我已经从 redis 复制了这个去教程。除了更改 urlparse 导入以满足 Python 3 标准外,没有进行任何调整。
production.py
import os
from urllib.parse
import urlparse
redis_url = urlparse(os.environ.get('REDISTOGO_URL', 'redis://localhost:6959'))
CACHES = { 'default': {
'BACKEND': 'redis_cache.RedisCache',
'LOCATION': '%s:%s' % (redis_url.hostname, redis_url.port),
'OPTIONS': {
'DB': 0,
'PASSWORD': redis_url.password, }
}
}
档案
我在我的 Procfile 中添加了以下行,并为我的工人添加了第二个测功机。
worker: celery -A appname worker --beat
工人似乎在工作。状态改变,然后关闭。它显然无法连接,请参阅错误。
Heroku 日志
2020-05-16T02:07:58.647226+00:00 app[worker.1]: [2020-05-16 04:07:58,647: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 111] Connection refused.
我究竟做错了什么?
我的芹菜配置:
芹菜.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', 'config.settings.local')
app = Celery('rebanq_pro')
# Using a string here means the worker don'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))
设置.local.py
CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = TIME_ZONE
设置.生产.py
CELERY_BROKER_URL = os.environ.get("REDISTOGO_URL")
CELERY_RESULT_BACKEND = os.environ.get("REDISTOGO_URL")
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = TIME_ZONE