Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add, modify, remove celery.schedules at run time

Tags:

python

celery

is there a way to Add, modify, remove celery.schedules at run time. I need something that reads a db table periodically to know list of schedules.

Document says one can use djcelery.schedulers.DatabaseScheduler to achieve what I want, but not sure how to do it.

I read How to dynamically add / remove periodic tasks to Celery (celerybeat), still not clear

Thanks for help

like image 880
com.iavian Avatar asked May 19 '14 14:05

com.iavian


People also ask

What is Apply_async in Celery?

apply_async(args[, kwargs[, …]]) Sends a task message. delay(*args, **kwargs) Shortcut to send a task message, but doesn't support execution options. calling ( __call__ )

How do I stop Celery from running?

revoke cancels the task execution. If a task is revoked, the workers ignore the task and do not execute it. If you don't use persistent revokes your task can be executed after worker's restart. revoke has an terminate option which is False by default.

How do you run a Celery scheduler?

To begin, let's first set up the Flask framework with Celery. Save this python script as app.py in the main project directory, and in your terminal, run: python ~/celery-scheduler/app.py . Now go to http://localhost:5000/. If everything is running fine, you should see “Hello, Flask is up and running!”

What is Celery scheduler?

celery beat is a scheduler. It kicks off tasks at regular intervals, which are then executed by the worker nodes available in the cluster. By default the entries are taken from the CELERYBEAT_SCHEDULE setting, but custom stores can also be used, like storing the entries in an SQL database.


1 Answers

When you set in your app settings:

CELERYBEAT_SCHEDULER='djcelery.schedulers.DatabaseScheduler'

celery beat proces checks django PeriodicTask model to see what task should be executed.

You can add / modify / remove those tasks by modifying it using django model:

from djcelery.models import PeriodicTask, CrontabSchedule

every_hours_crontab = CrontabSchedule(minute=0)
every_hours_crontab.save()

periodic_task = PeriodicTask(
    name='Call my task every hour',
    task='myproject.tasks.mytask',
    crontab=every_hours_crontab,
    args=json.dump([arg1, arg2]),
    kwargs=json.dump({'foo': 'bar'})
)
periodic_task.save()

You can also test various configuration of PeriodicTask using django admin panel:
http://localhost:8000/admin/djcelery/crontabschedule/add/
http://localhost:8000/admin/djcelery/periodictask/

like image 120
daniula Avatar answered Oct 10 '22 22:10

daniula