Celery:如何将不同的环境与不同的工人分开?

Celery: how to separate different environments with different workers?

我需要将某个 Django 站点实例的所有任务路由到某个队列。我的设置如下:

在 "celery server" 上,我 运行 多个 worker 实例通过 supervisor(简化配置):

[program:production_queue]
environment=PYTHONPATH=/pth/to/src/:/pth/to/site-packages/,DJANGO_SETTINGS_MODULE=website.settings.production
command=/pth/to/python celery -A website.celery worker --events --queues myserver --loglevel WARNING --concurrency 4 -n production@celery.myserver.nl

[program:staging_queue]
environment=PYTHONPATH=/pth/to/src/:/pth/to/site-packages/,DJANGO_SETTINGS_MODULE=website.settings.staging
command=/pth/to/python celery -A website.celery worker --events --queues myserver_staging --loglevel WARNING --concurrency 1 -n staging@celery.myserver.nl

[program:development_queue]
environment=PYTHONPATH=/pth/to/src/:/pth/to/site-packages/,DJANGO_SETTINGS_MODULE=website.settings.development
command=/pth/to/python celery -A website.celery worker --events --queues myserver_development --loglevel INFO --concurrency 1 -n development@celery.myserver.nl

这有效,经过检查:

$ celery -A website.celery inspect activeues
-> production@celery.myserver.nl: OK
    * {u'exclusive': False, u'name': u'myserver', u'exchange': {u'name': u'celery', u'durable': True, u'delivery_mode': 2, u'passive': False, u'arguments': None, u'type': u'direct', u'auto_delete': False}, u'durable': True, u'routing_key': u'celery', u'no_ack': False, u'alias': None, u'queue_arguments': None, u'binding_arguments': None, u'bindings': [], u'auto_delete': False}
-> staging@celery.myserver.nl: OK
    * {u'exclusive': False, u'name': u'myserver_staging', u'exchange': {u'name': u'celery', u'durable': True, u'delivery_mode': 2, u'passive': False, u'arguments': None, u'type': u'direct', u'auto_delete': False}, u'durable': True, u'routing_key': u'celery', u'no_ack': False, u'alias': None, u'queue_arguments': None, u'binding_arguments': None, u'bindings': [], u'auto_delete': False}
-> development@celery.myserver.nl: OK
    * {u'exclusive': False, u'name': u'myserver_development', u'exchange': {u'name': u'celery', u'durable': True, u'delivery_mode': 2, u'passive': False, u'arguments': None, u'type': u'direct', u'auto_delete': False}, u'durable': True, u'routing_key': u'celery', u'no_ack': False, u'alias': None, u'queue_arguments': None, u'binding_arguments': None, u'bindings': [], u'auto_delete': False}

(名称符合CELERY_DEFAULT_QUEUE设置)

website/celery.py 包含基础知识(跳过导入):

app = Celery('proj')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

因此,我希望由网络服务器 运行 开发设置生成的任务仅在 development_queue 中结束,依此类推。但是,我看到任务由不同的队列处理,或者由所有三个队列处理,这是有问题的。

我的期望是否错误,因为这将是分离这些任务的好方法?所有 documentation on routing 都是关于将不同的任务路由到不同的队列,我不知道不需要。我需要将某个站点(环境)的所有任务路由到某个队列。 我可以做些什么来分离这些环境?

我有一个与您类似的设置,但我的解决方案是使用不同的 RabbitMQ 代理进行开发、暂存和生产。

例如,在我的个人开发设置文件中我有:

CELERY_BROKER = "librabbitmq://user:user@my_machine.domain.com/"

在生产设置文件中我有:

CELERY_BROKER = "librabbitmq://prop:prod@main_cluster.domain.com/"

在我的芹菜应用程序模块中,我有:

app = Celery('App', broker=settings.CELERY_BROKER)

我不知道这是否有帮助,但我在 celery-users 板上看到了您的 post,并且由于您没有任何其他回复...

我在这里得到了 Celery 开发者的回答:https://github.com/celery/celery/issues/2508,即:

You have to set all of CELERY_DEFAULT_QUEUE, CELERY_DEFAULT_EXCHANGE and CELERY_DEFAULT_ROUTING_KEY. Otherwise you will end up with three queues all bound to the same exchange and routing key.

Or use the method here which will set it up explicitly: http://docs.celeryproject.org/en/latest/userguide/routing.html#changing-the-name-of-the-default-queue

有效!