我创建了一个项目 github 存储库,其中包含 Docker、Django、React、Redis、Celery: https://github.com/axilaris/docker-django-react-celery-redis。我的目标是让芹菜发挥作用,但事实并非如此。
这基于 Docker - React 和 Django 示例教程代码: https://github.com/dotja/authentication_app_react_django_rest
并尝试使用本教程代码中的 Docker - Celery 和 Redis: https://github.com/veryacademy/docker-mastery-with-django/tree/main/Part-4%20Django%20Postgres%20Redis%20and%20Celery <-- Part 4 tutorial for Celery & Redis
我无法正确启动 Celery,因为我看到后台任务未执行。 (你可以查看我分享的要点)
它没有打印这个。我想这就是问题所在。
celery | -------------- celery@b755a7cdba8d v5.3.6 (emerald-rush)
celery | --- ***** -----
celery | -- ******* ---- Linux-6.6.12-linuxkit-aarch64-with 2024-03-02 20:48:06
celery | - *** --- * ---
celery | - ** ---------- [config]
celery | - ** ---------- .> app: core:0xffff9bbd7550
celery | - ** ---------- .> transport: redis://redis:6379//
celery | - ** ---------- .> results: redis://redis:6379/
celery | - *** --- * --- .> concurrency: 10 (prefork)
celery | -- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
celery | --- ***** -----
celery | -------------- [queues]
celery | .> celery exchange=celery(direct) key=celery
这是我分享的要点,其中包含有关 docker-compose 构建/启动以及失败的后台任务的控制台日志的更多详细信息 https://gist.github.com/axilaris/095b4fd926df6a27b5e635fc1b51c854
2024-03-02 21:31:33,403 DEBUG
def add(x, y):
return 1
..
File "/usr/local/lib/python3.9/contextlib.py", line 137, in __exit__
self.gen.throw(typ, value, traceback)
File "/usr/local/lib/python3.9/site-packages/kombu/connection.py", line 476, in _reraise_as_library_errors
raise ConnectionError(str(exc)) from exc
kombu.exceptions.OperationalError: [Errno 111] Connection refused
我觉得这与我的后端 docker-compose.yaml 或 Dockerfile 和导致此问题的 celery 有关。
我在github上看到了你的文件,你可以这样做:
首先,在“backend/backend/init.py”中的init文件中,设置:
from .celery import app as celery_app
__all__ = ("celery_app",)
在 docker compose 文件中设置 redis 端口并更改 celery 命令:
redis:
image: redis:alpine
ports:
- "6379:6379"
celery:
restart: always
build:
context: ./backend
command: celery -A backend worker -l info
volumes:
- .:/django
container_name: celery
depends_on:
- redis
最后你可以为你的docker compose设置网络,或者检查你的docker日志和网络。
您还可以使用此参考:https://saasitive.com/tutorial/django-celery-redis-postgres-docker-compose/
web:
build: .
container_name: del_web
ports:
- 8000:8000
volumes:
- ./del_src:/del_src
command: bash Deployment/bashes/RunBash.sh
links:
- db
- redis
depends_on:
- db
- redis
redis:
image: redis
restart: always
volumes:
- ./db/redis_data:/data
我的 RunBash.sh 文件是:
python3 -m celery -A del worker &
python3 manage.py runserver 0.0.0.0:8000