我有一个在celery和redis的Elastic beantalk上运行的django应用程序。 失败的celery任务是处理照片并将一些结果保存到数据库(这是一个postgres RDS)中,并且失败了,因为它试图连接到localhost:5432上的数据库。
我有一个调试任务,该任务不会与数据库交互,并且可以看到它已被接收并执行。
我的设置文件具有以下
ALLOWED_HOSTS = [
"localhost",
"127.0.0.1",
".elasticbeanstalk.com",
]
if 'RDS_DB_NAME' in os.environ:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ['RDS_DB_NAME'],
'USER': os.environ['RDS_USERNAME'],
'PASSWORD': os.environ['RDS_PASSWORD'],
'HOST': os.environ['RDS_HOSTNAME'],
'PORT': os.environ['RDS_PORT'],
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'woodpecker',
'USER': os.environ.get("DB_USER"),
'PASSWORD': os.environ.get("DB_PASS"),
'HOST': 'localhost',
'PORT': '',
}
}
CELERY_BROKER_URL = 'redis://localhost'
芹菜配置文件
from __future__ import absolute_import
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings') # DON'T FORGET TO CHANGE THIS ACCORDINGLY
# os.environ.setdefault('FORKED_BY_MULTIPROCESSING', '1')
app = Celery('myapp')
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))
我仍然无法在具有主管的情况下运行celery,因此现在我登录到ec2实例后执行以下操作来启动celery
source /opt/python/run/venv/bin/activate
cd /opt/python/current/app/
celery worker -A myapp --loglevel=DEBUG
我得到的错误摘要表明celery试图连接到localhost上的数据库,而它应该从设置文件连接到postgres RDS
[2019-07-28 17:58:27,486: ERROR/ForkPoolWorker-1] Task photos.views.generate_features_async[6808ab5d-1160-456a-ae06-fbc2ec37e150] raised unexpected: OperationalError('could not connect to server: Connection refused\n\tIs the server running on host "localhost" (127.0.0.1) and accepting\n\tTCP/IP connections on port 5432?\n',)
Traceback (most recent call last):
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection
self.connect()
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 195, in connect
self.connection = self.get_new_connection(conn_params)
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 178, in get_new_connection
connection = Database.connect(**conn_params)
File "/opt/python/run/venv/local/lib64/python3.6/site-packages/psycopg2/__init__.py", line 126, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?