无法从 Celery 访问数据库 - 操作错误("没有这样的表:products_product")



我有Django项目,但我无法从Celery访问/查询PostgreSQL。它会影响任何表,即使它存在。查询在 Celery 之外运行良好,所以我可能在 Celery 配置中做错了什么。不接触数据库的芹菜任务效果很好。错误信息:

Traceback (most recent call last):
File "/usr/src/app/accounts/tests.py", line 28, in test_access_to_database
result_output = task.get()
File "/usr/local/lib/python3.8/site-packages/celery/result.py", line 217, in get
self.maybe_throw(callback=callback)
File "/usr/local/lib/python3.8/site-packages/celery/result.py", line 333, in maybe_throw
self.throw(value, self._to_remote_traceback(tb))
File "/usr/local/lib/python3.8/site-packages/celery/result.py", line 326, in throw
self.on_ready.throw(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/vine/promises.py", line 244, in throw
reraise(type(exc), exc, tb)
File "/usr/local/lib/python3.8/site-packages/vine/five.py", line 195, in reraise
raise value
django.db.utils.OperationalError: no such table: products_product

一切都基于 docker 的容器:

redis:
image: redis:latest
ports:
- 6379:6379
environment:
- TZ=Europe/Warsaw
db:
container_name: gallop_db
image: postgres
volumes:
- postgres_data:/var/lib/postgresql/data/
env_file: ./env/dev/.envdb
worker1:
build: ./app
command: celery -A gallop worker -l debug --pidfile=
links:
- redis
volumes:
- ./app/:/usr/src/app/
depends_on:
- db
- redis
- rabbitmq
celery-beat:
build: ./app
command: celery -A gallop beat -l debug --pidfile=
volumes:
- ./app/:/usr/src/app/
links:
- redis
environment:
- TZ="Europe/Warsaw"
depends_on:
- db
- redis
- rabbitmq
rabbitmq:
image: rabbitmq:latest
environment:
- TZ=Europe/Warsaw
volumes:
postgres_data:

我的环境:

celery                       4.4.5
Python                       3.8 (I checked also on 3.7)
Docker                       3.3
Django                       2.2.10
django-celery-beat           2.0.0
django-celery-results        1.2.1

已注册的应用:

DJANGO_APPS = (
#...
'celery',
'django_celery_results',
'django_celery_beat',
)

姜戈设置中的芹菜:

CELERY_BROKER_URL = 'amqp://rabbitmq' 
CELERY_RESULT_BACKEND = 'redis://redis'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_IGNORE_RESULT = False
CELERYD_TASK_SOFT_TIME_LIMIT = 60
CELERY_TIMEZONE = 'Europe/Warsaw'

芹菜任务:

@app.task(bind=True, ignore_result=False)
def get_first_product(self):
p = Product.objects.first()
return p

芹菜配置(项目目录中 celery.py(:

import os
from celery import Celery

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

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

调用芹菜任务:

get_first_product.delay()

问题解决了。

我将环境变量文件添加到工作线程并在 docker 撰写文件中击败容器:

env_file: ./env/dev/.env

顺便说一下,它也解决了我的第二个错误:

SMTPServerDisconnected('please run connect() first')

我无法从芹菜发送电子邮件,再说一次,在芹菜之外,我没有任何问题。在该文件中,我有一封电子邮件和一个数据库配置:

SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=your_database
SQL_USER=username
SQL_PASSWORD=your_password
SQL_HOST=db
SQL_PORT=5432
EMAIL_HOST=smtp.gmail.com
EMAIL_HOST_USER=your_email
EMAIL_HOST_PASSWORD=your_password
EMAIL_PORT=587

相关内容

  • 没有找到相关文章

最新更新