【问题标题】:How to route tasks to queues while using Docker如何在使用 Docker 时将任务路由到队列
【发布时间】:2019-12-30 23:10:01
【问题描述】:

我正在设置多个工作人员,这些工作人员在 docker 环境中根据给定队列执行任务。我无法看到我在 settings.py 中声明的队列

我已经看到顶部的工作命令(在容器命令中)只是被执行,其余的都没有被执行。

这就是我尝试在 settings.py 中路由 celery 任务的方式

# Celery settings


CELERY_BROKER_URL = 'redis://:secret@redis:6379/0'
CELERY_RESULT_BACKEND = 'redis://:secret@redis:6379/0'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_TIMEZONE = TIME_ZONE
CELERY_TASK_ROUTES = {

    'app1.tasks.*': {'queue': 'emails'},
    'app2.tasks.*': {'queue': 'parser'},
    'app3.tasks.task1': {'queue': 'sms'}
}

这就是我编写 tasks.py 文件的方式。

@app.task(bind=True,  queue='parser')
def parser(self, extension,  data, file):
    return True

这就是我在容器中的 docker-compose.yml 文件中给出命令的方式

version: '3.3'
services:
  api:
    restart: always
    build: ./app
    volumes:
      - ./app:/app/
    depends_on:
      - redis
    command:  bash -c 'python3 manage.py makemigrations --noinput  &&
                       python3 manage.py migrate --noinput &&
                       celery -A app worker -l info -Q email-n worker1 &&
                       celery -A app worker -l info -Q parser-n worker2 &&
                       celery -A app worker -l info -Q sms-n worker3 &&
                       celery -A app  worker -l info  &&
                       celery -A app beat -l info  &&
                       python3 manage.py runserver 0.0.0.0:1337'
​
​
    ports:
      - 1337:1337
    expose:
      - 1337
    environment:
      - SECRET_KEY = qbsdk08)5&n*x4xdya7fbm0&lb)x!6!f_#ta(y-)*!w_wibc4c
      - SQL_ENGINE = django.db.backends.postgresql_psycopg2
      - SQL_DATABASE = postgres
      - SQL_USER = admin
      - SQL_PASSWORD = password
      - SQL_HOST = postgres_container
      - SQL_PORT = 5432
      - DATABASE = postgres
      - DJANGO_SETTINGS_MODULE = api.settings
    depends_on:
      - postgres_container

预期结果: 我希望队列列表显示在控制台中并运行良好

实际结果: 它仅显示 docker-compose 文件中给出的第一个任务,在我的情况下,根据 docker-compose 文件执行以下命令,其余的将不起作用。

"celery -A app worker -l info -Q email-n worker1 &&"

技术: Django v2.2 芹菜 v4 Redis v5

【问题讨论】:

    标签: python django docker redis celery


    【解决方案1】:

    问题在于启动命令。您基本上已经连续启动了每个工作程序,因此 celery -A app worker -l info -Q parser-n worker2 直到 worker1 退出后才会执行。解决这个问题的最简单方法就是为每个工作人员设置单独的 docker 实例。

      email-n:
        restart: always
        build: ./app
        volumes:
          - ./app:/app/
        depends_on:
          - redis
        command:  bash -c 'python3 manage.py makemigrations --noinput  &&
                           python3 manage.py migrate --noinput &&
                           celery -A app worker -l info -Q email-n worker1'
    ...
      parser-n:
        restart: always
        build: ./app
        volumes:
          - ./app:/app/
        depends_on:
          - redis
        command: celery -A app worker -l info -Q parser-n worker2 
      sms-n:
        restart: always
        build: ./app
        volumes:
          - ./app:/app/
        depends_on:
          - redis
        command: celery -A app worker -l info -Q sms-n worker3 
      celery:
        restart: always
        build: ./app
        volumes:
          - ./app:/app/
        depends_on:
          - redis
        command: celery -A app  worker -l info
      beat:
        restart: always
        build: ./app
        volumes:
          - ./app:/app/
        depends_on:
          - redis
        command: celery -A app beat -l info
      api:
        restart: always
        build: ./app
        volumes:
          - ./app:/app/
        depends_on:
          - redis
        command: python3 manage.py runserver 0.0.0.0:1337
    

    或者,您可以启动一个具有多个队列的工作人员,例如,-Q parser-n,email-n,sms-n。最后,您还可以在容器内对 docker 进行守护进程,但是当您准备好停止容器时,您必须有一种优雅的方式来停止守护进程,但这超出了本问题的范围。

    【讨论】:

      猜你喜欢
      • 2012-04-22
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多