Celery Beat - 金字塔邮递员

Celery Beat - Pyramid Mailer

所以,我有一些简单的 python 代码,可以在正常 python shell:

中正常工作
from pyramid_mailer.mailer import Mailer
from pyramid_mailer.message import Message
from pyramid_mailer.message import Attachment

mailer = Mailer(
    host="172.10.10.240",
    port="25")
message = Message(
subject="Orders with invalid status",
sender='r@example.com'],
recipients=['luke@example.com'],
html="<p>Test</p>")
mailer.send_immediately(message)

但是,如果我创建这样的 celery beat 任务:

from pyramid_celery import celery_app as app
from pyramid_mailer.mailer import Mailer
from pyramid_mailer.message import Message
from pyramid_mailer.message import Attachment

mailer = Mailer(
    host="172.10.10.240",
    port="25")

@app.task
def wronglines_celery():
    message = Message(
    subject="Orders with invalid status",
    sender='r@example.com'],
    recipients=['luke@example.com'],
    html="<p>Test</p>")
    mailer.send_immediately(message)

第二个示例不会生成电子邮件,即使日志级别设置为 DEBUG,它也能完美运行并且不会抛出任何错误。

运行芹菜节拍:

celery beat -A pyramid_celery.celery_app --ini development.ini

使用celery网站官方文档中引用的pyramid_celery插件。我的 development.ini 文件如下(相关部分):

[celery]
BROKER_URL = amqp://app_rmq:password@localhost:5672/myvhost
CELERY_IMPORTS = intranet.celery_tasks

# Check once a day for orders with wrong line status
[celerybeat:task1]
task = intranet.celery_tasks.wronglines_celery
type = crontab
schedule = {"hour": 16, "minute": 30}

[logger_celery]
level = DEBUG
handlers =
qualname = celery


# Begin logging configuration
[loggers]
keys = root, intranet, sqlalchemy, celery

编辑:

如果我启动 celery(没有节拍),它会完美运行,例如如果我启动:

celery worker -A pyramid_celery.celery_app --ini development.ini

所有任务都执行(一遍又一遍)但是所有电子邮件都已发送并且没有任何错误,似乎是引入了 beat 导致了问题。

默认情况下,Celery tasks silently fail on error output。它很可能会引发您从未见过的异常​​。

为了确定什么会失败,在任务代码中放置 pdb (ipdb) 断点,在前台启动 celery worker 并逐行执行代码。

你确定它不起作用吗?我们配置您的 crontab 的方式显示 "Only run once a day at 4:30"。所以如果你 运行 直到它达到 4:30 我希望它能正确执行。

你能否将你的 schedule 改为 {} 而不是每分钟 运行 作为基本测试?

我在此处的示例中添加了一个 crontab 示例:

https://github.com/sontek/pyramid_celery/blob/master/examples/scheduler_example/development.ini#L33-L36

如果您可以提供更多代码(可能是示例存储库或对存储库中已有示例的修改)表明它不起作用,我可以看一下并希望修复该错误。

因此,经过多次谷歌搜索和令人沮丧的调试后,我找到了 an old github issue。这声称 celery 任务只有在与 worker 一起启动时才有效,而不是与 beat 一起启动。用户状态

Beat does not execute tasks, it just sends the messages. You need both a beat instance and a worker instance!

所以要使用相同的命令启动工作和节拍实例,如下所示:

celery worker --beat -A pyramid_celery.celery_app --ini development.ini

我今天将发送一个 pull request 来修复有关启动 worker 和 beat 实例的正确方法的文档。