I am able to create the celery_beat_schedule and it works. YAY!
But I was wondering if there any way to create the cronjob for different objects of same Django model.
Settings.py
CELERY_BEAT_SCHEDULE = {
'ok': {
'task' : 'bill.tasks.ok',
'schedule' : crontab(minute=27, hour=0),
# 'args' : (*args)
}
}
bill/tasks.py
from celery import task
@task
def ok():
bills = Bill.objects.all()
for bill in bills:
perform_something(bill)
I wanted to change the crontab time for each object. How can I do it?
Assuming I have an hour and minute value in model object
Thanks for your time :)
Well I wouldn't be able to find how to run different crontab for each task instances. But there is another way to run. Just run your crontab on each hour and every time check if your query matches with the present time in tasks.py.
You can specify the values in the arguments and then use them in filtering the QuerySet.
Settings.py
CELERY_BEAT_SCHEDULE = {
'ok_27_0': {
'task' : 'bill.tasks.ok',
'schedule' : crontab(minute=27, hour=0),
'args' : (27, 0)
},
'ok_5_any': {
'task' : 'bill.tasks.ok',
'schedule' : crontab(minute=5),
'args' : (5, None)
}
}
bill/tasks.py
from celery import task
@task
def ok(minute=None, hour=None):
bills = Bill.objects.all()
if minute is not None:
bills = bills.filter(minute=minute)
if hour is not None:
bills = bills.filter(hour=hour)
for bill in bills:
perform_something(bill)
Edit:
You may also want to try binding the task and seeing if you can find the schedule for the task in the instance of the task or its request. That way you wouldn't have to repeat yourself in the settings. However, I don't know if this is possible.
@task(bind=True)
def ok(self):
self.request