Search code examples
celerypyramidcelerybeat

Celery Beat - Pyramid Mailer


So, I have some plain python code which works pefectly in a normal 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)

But, If I create a celery beat task such as this:

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)

This second example does not generate an email, it runs perfectly fine and throws no error at all, even with the log level set to DEBUG.

Running celery beat with:

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

Using the pyramid_celery plug-in as referenced in the official documentation on the celery website. My development.ini file can be seen below (relevant parts):

[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

EDIT:

If I launch celery (without beat) it works perfectly, e.g. if I launch with:

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

All tasks execute (over and over) but all emails send and nothing throws an error, it seems to be the introduction of beat which is causing issues.


Solution

  • So, after much googlig and frustrating debugging I found an old github issue. That claimed celery tasks were working only when launched with a worker, and not with beat. The user states

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

    So to launch the work and the beat instance with the same command, shown here:

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

    I will be sending a pull request today to fix the documentation with regards to the correct way to launch a worker and beat instance.