简体   繁体   English

如何使用 docker-compose 构建不同配置的 celery 工作人员?

[英]How to build differently configured celery workers using docker-compose?

I am using docker-compose to build and run celery workers.我正在使用 docker-compose 来构建和运行 celery 工作人员。

services:
    ...

    worker-a:
        command: celery -A tasks worker -l "DEBUG" --concurrency=1 -Q qname_a
        restart: always
        environment:
            WORKER_TYPE: a_setting
            ...
        depends_on:
            - ...

    worker-b:
        command: celery -A tasks worker -l "DEBUG" --concurrency=1 -Q qname_b
        restart: always
        environment:
            WORKER_TYPE: b_setting
            ...
        depends_on:
            - ...

How can I build 2 different worker images (worker-a and worker-b), which perform the same task (same code), but use different configurations (based on WORKER_TYPE) and listen to different queues?如何构建 2 个不同的工作映像(worker-a 和 worker-b),它们执行相同的任务(相同的代码),但使用不同的配置(基于 WORKER_TYPE)并监听不同的队列?

If you are aiming to avoid having to repeat an almost identical piece of code, YAML anchors are great for defining reusable blocks.如果您的目标是避免重复几乎相同的代码,YAML 锚非常适合定义可重用的块。

For example, in the below configuration all the configuration for worker-a can be used by any other service by calling <<: *default , and then overriding any value as needed:例如,在下面的配置中, worker-a所有配置都可以通过调用<<: *default被任何其他服务使用,然后根据需要覆盖任何值:

version: '3'

services:
  worker-a: &default
    image: alpine
    command: env
    environment:
      WORKER_TYPE: a_setting
    # ... more definitions

  worker-b:
    <<: *default
    environment:
      WORKER_TYPE: b_setting

Notice that if you want nested elements - such as environment - to also have common values, you may need to do something like this:请注意,如果您希望嵌套元素(例如environment )也具有公共值,则可能需要执行以下操作:

version: '3'

services:
  worker-a: &default
    image: alpine
    command: env
    environment: &environment
      COMMON_VAR_FOR_ALL: value
      WORKER_TYPE: a_setting
    # ... more definitions

  worker-b:
    <<: *default
    environment:
      <<: *environment
      WORKER_TYPE: b_setting

There is a good writeup on the subject here and here .关于这个主题有一篇很好的文章herehere

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

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