简体   繁体   English

如何确保Celery任务以正确的设置运行?

[英]How can I ensure a Celery task runs with the right settings?

I have two sites running essentially the same codebase, with only slight differences in settings. 我有两个站点运行基本相同的代码库,设置只有轻微的差异。 Each site is built in Django, with a WordPress blog integrated. 每个站点都在Django中构建,并集成了WordPress博客。

Each site needs to import blog posts from WordPress and store them in the Django database. 每个站点都需要从WordPress导入博客文章并将它们存储在Django数据库中。 When a user publishes a post, WordPress hits a webhook URL on the Django side, which kicks off a Celery task that grabs the JSON version of the post and imports it. 当用户发布帖子时,WordPress会在Django端点击一个webhook URL,从而启动一个Celery任务,该任务抓取帖子的JSON版本并导入它。

My initial thought was that each site could run its own instance of manage.py celeryd , each is in its own virtualenv , and the two sites would stay out of each other's way. 我最初的想法是每个站点都可以运行自己的manage.py celeryd实例,每个都在自己的virtualenv ,这两个站点将保持彼此不同的方式。 Each is daemonized with a separate upstart script. 每个都使用单独的upstart脚本进行守护。

But it looks like they're colliding somehow. 但它看起来好像在某种程度上相撞。 I can run one at a time successfully, but if both are running, one instance won't receive tasks, or tasks will run with the wrong settings (in this case, each has a WORDPRESS_BLOG_URL setting). 我可以一次成功运行一个,但如果两个都运行,一个实例将不会接收任务,或者任务将以错误的设置运行(在这种情况下,每个都有一个WORDPRESS_BLOG_URL设置)。

I'm using a Redis queue, if that makes a difference. 我正在使用Redis队列,如果这有所不同。 What am I doing wrong here? 我在这做错了什么?

Have you specified the name of the default queue that celery should use? 您是否指定了芹菜应该使用的默认队列的名称? If you haven't set CELERY_DEFAULT_QUEUE the both sites will be using the same queue and getting each other's messages. 如果您尚未设置CELERY_DEFAULT_QUEUE,则两个站点将使用相同的队列并获取彼此的消息。 You need to set this setting to a different value for each site to keep the message separate. 您需要为每个站点将此设置设置为不同的值,以使消息保持独立。

Edit 编辑

You're right, CELERY_DEFAULT_QUEUE is only for backends like RabbitMQ. 你没错,CELERY_DEFAULT_QUEUE仅适用于像RabbitMQ这样的后端。 I think you need to set a different database number for each site, using a different number at the end of your broker url . 我认为您需要为每个站点设置不同的数据库编号,在您的代理URL末尾使用不同的编号。

If you are using django-celery then make sure you don't have an instance of celery running outside of your virtualenvs. 如果您正在使用django-celery,那么请确保您没有在virtualenvs之外运行的芹菜实例。 Then start the celery instance within your virtualenvs using manage.py celeryd like you have done. 然后像使用manage.py celeryd一样在virtualenvs中启动celery实例。 I recommend setting up supervisord to keep track of your instances. 我建议设置supervisord以跟踪您的实例。

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

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