简体   繁体   English

解耦和Dockerize Django和Celery

[英]Decouple and Dockerize Django and Celery

I am wondering what is the best way to decouple Celery from Django in order to dockerize the two parts and use docker swarm service ? 我想知道什么是最好的方法将Celery与Django分离,以便将这两个部分停靠并使用docker swarm服务 Typically one starts their celery workers and celery beat using a command that references there Django application: 通常一个人开始他们的芹菜工人和芹菜击败使用引用Django应用程序的命令:

celery worker -A my_app
celery beat -A my_app

From this I believe celery picks up config info from settings file and a celery.py file which is easy to move to a microservice. 从此我相信芹菜从设置文件和celery.py文件中获取配置信息,这很容易移动到微服务。 What I don't totally understand is how the tasks would leverage the Django ORM? 我不完全理解的是这些任务将如何利用Django ORM? Or is that not really the microservices mantra and Celery should be designed to make GET/POST calls to Django REST Framework API for the data it needs to complete the task? 或者是不是真的微服务口头禅和Celery应该被设计为对Django REST Framework API进行GET / POST调用以获得完成任务所需的数据?

I use a setup where the code for both the django app and its celery workers is the same (as in a single repository). 我使用一个设置,其中django应用程序和它的芹菜工作者的代码是相同的(如在单个存储库中)。

When deploying I make sure to have the same code release everywhere, to avoid any surprises with the ORM, etc... 在部署时我确保在任何地方都有相同的代码版本,以避免任何意外的ORM等...

Celery starts with a reference to the django app, so that it has access to the models, etc... Celery首先引用django应用程序,以便它可以访问模型等...

Communication between the workers and the main app happens either through the messaging queue ( rabbitmq or redis ...) or via the database (as in, the celery worker works directly in the db, since it knows the models, etc...). 工作人员和主应用程序之间的通信通过消息传递队列( rabbitmqredis ...)或通过数据库进行(因为,芹菜工作者直接在数据库中工作,因为它知道模型等...) 。

I'm not sure if that follows the microservices mantra, but it does work :) 我不确定这是否遵循微服务口头禅,但它确实有效:)

Celery的.send_task.signature可能会有所帮助: https.signature

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM