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
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__ )
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.
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!”
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With