【问题标题】:How to configure Celery Worker and Beat for Email Reporting in Apache Superset running on Docker?如何在 Docker 上运行的 Apache Superset 中为电子邮件报告配置 Celery Worker 和 Beat?
【发布时间】:2020-08-03 11:08:31
【问题描述】:

我正在通过 Docker 运行 Superset。我启用了电子邮件报告功能并尝试了它:

但是,我只收到测试电子邮件报告。之后我没有收到任何电子邮件。

这是我在 superset_config.py 中的 CeleryConfig:

class CeleryConfig(object):
    BROKER_URL = 'sqla+postgresql://superset:superset@db:5432/superset'
    CELERY_IMPORTS = (
        'superset.sql_lab',
        'superset.tasks',
    )
    CELERY_RESULT_BACKEND = 'db+postgresql://superset:superset@db:5432/superset'
    CELERYD_LOG_LEVEL = 'DEBUG'
    CELERYD_PREFETCH_MULTIPLIER = 10
    CELERY_ACKS_LATE = True
    CELERY_ANNOTATIONS = {
        'sql_lab.get_sql_results': {
            'rate_limit': '100/s',
        },
        'email_reports.send': {
            'rate_limit': '1/s',
            'time_limit': 120,
            'soft_time_limit': 150,
            'ignore_result': True,
        },
    }
    CELERYBEAT_SCHEDULE = {
        'email_reports.schedule_hourly': {
            'task': 'email_reports.schedule_hourly',
            'schedule': crontab(minute=1, hour='*'),
        },
    }

documentation 说我需要运行芹菜工人并击败。

celery worker --app=superset.tasks.celery_app:app --pool=prefork -O fair -c 4
celery beat --app=superset.tasks.celery_app:app

我将它们添加到“docker-compose.yml”:

superset-worker:
    build: *superset-build
    command: >
      sh -c "celery worker --app=superset.tasks.celery_app:app -Ofair -f /app/celery_worker.log &&
             celery beat --app=superset.tasks.celery_app:app -f /app/celery_beat.log"
    env_file: docker/.env
    restart: unless-stopped
    depends_on: *superset-depends-on
    volumes: *superset-volumes

发送第一封电子邮件时,Celery Worker 确实在工作。日志文件也是可见的。然而,芹菜节拍似乎不起作用。也没有创建“celery_beat.log”。

如果您想深入了解,请here's the commit 了解功能的完整实现。

如何正确配置 celery beat?我该如何调试?

【问题讨论】:

    标签: python python-3.x docker celery apache-superset


    【解决方案1】:

    我设法通过更改 CeleryConfig 实现来解决它,并向“docker-compose.yml”添加节拍服务

    'superset_config.py' 中的新 CeleryConfig 类:

    REDIS_HOST = get_env_variable("REDIS_HOST")
    REDIS_PORT = get_env_variable("REDIS_PORT")
    
    class CeleryConfig(object):
        BROKER_URL = "redis://%s:%s/0" % (REDIS_HOST, REDIS_PORT)
        CELERY_IMPORTS = (
            'superset.sql_lab',
            'superset.tasks',
        )
        CELERY_RESULT_BACKEND = "redis://%s:%s/1" % (REDIS_HOST, REDIS_PORT)
        CELERY_ANNOTATIONS = {
            'sql_lab.get_sql_results': {
                'rate_limit': '100/s',
            },
            'email_reports.send': {
                'rate_limit': '1/s',
                'time_limit': 120,
                'soft_time_limit': 150,
                'ignore_result': True,
            },
        }
        CELERY_TASK_PROTOCOL = 1
        CELERYBEAT_SCHEDULE = {
            'email_reports.schedule_hourly': {
                'task': 'email_reports.schedule_hourly',
                'schedule': crontab(minute='1', hour='*'),
            },
        }
    

    “docker-compose.yml”的变化:

      superset-worker:
        build: *superset-build
        command: ["celery", "worker", "--app=superset.tasks.celery_app:app", "-Ofair"]
        env_file: docker/.env
        restart: unless-stopped
        depends_on: *superset-depends-on
        volumes: *superset-volumes
    
      superset-beat:
        build: *superset-build
        command: ["celery", "beat", "--app=superset.tasks.celery_app:app", "--pidfile=", "-f", "/app/celery_beat.log"]
        env_file: docker/.env
        restart: unless-stopped
        depends_on: *superset-depends-on
        volumes: *superset-volumes
    

    【讨论】:

      【解决方案2】:

      我相信 Celery 需要在您的超集容器内运行 - 所以您需要修改您的 dockerfile 和入口点。
      但是你真的应该首先守护 celery,这样你就不必监视和重新启动 celery [参见 how to detect failure and auto restart celery workerhttp://docs.celeryproject.org/en/latest/userguide/daemonizing.html]
      请参阅此处的示例,了解如何在 docker 中运行守护进程 celery:Docker - Celery as a daemon - no pidfiles found

      【讨论】:

        【解决方案3】:

        你也可以在 celery worker 命令中添加-B 标志来运行beat

        celery worker --app=superset.tasks.celery_app:app --pool=prefork -O fair -c 4 -B
        

        【讨论】:

        • 不建议在生产中使用,当您有多个工人时
        • 最好在 docker 中使用两个单独的命令/进程。我有 1.5 年的 -B 生产经验,一切都很好。
        • 你有多少工人在运行? docs 说“您还可以通过启用workers -B 选项将节拍嵌入worker,如果您永远不会运行多个worker节点,这很方便,但它不常用,因此不推荐用于生产”
        • 如果你只有一个工人,那很好,但你不应该对多个工人使用-B
        • 有 6 个容器具有不同的工人参数,其中一个带有 -B 选项,一切正常
        猜你喜欢
        • 2016-09-20
        • 2019-05-02
        • 1970-01-01
        • 2021-12-01
        • 1970-01-01
        • 2018-09-16
        • 2015-01-15
        • 2016-02-29
        • 2019-09-30
        相关资源
        最近更新 更多