Celery: launch task at startup

I have a task to run when Celery starts. These tasks run every 5 minutes via callback / eta.

I find some threads about this, but nothing that seems to work on Celery 3.

Thanks for your help, Arnaud.

+3


source to share


2 answers


Someone from the IRC Celery channel give me the correct way to do this using the "worker_ready.connect" signal: http://docs.celeryproject.org/en/latest/userguide/signals.html#worker-ready

@worker_ready.connect
    def at_start(sender, **k):
        with sender.app.connection() as conn:
             sender.app.send_task('app.modules.task', args,connection=conn, ...)

      



Now it works like a charm!

+6


source


You need to define in the settings:

import djcelery
djcelery.setup_loader()
CELERY_IMPORTS = ("apps.app_name.module.tasks",)

      

Also, if you don't have a celery broker installed, you must install one. I am using RabbitMQ, there is a very good tutorial on how to use it in the celery docs:



http://docs.celeryproject.org/en/latest/getting-started/brokers/rabbitmq.html

And then start with the command line celery daemon:

django-admin.py celeryd -v 2 -B -s celery -El INFO

0


source







All Articles