Checking next time to run scheduled periodic jobs in Celery (with Django)
* Using celery 3.1.25 because django-celery-beat 1.0.1 has a problem scheduling periodic tasks.
I recently ran into a celerybeat issue where periodic jobs at intervals of one day or more seem to be "forgotten" by the scheduler. If I change the interval to every 5 seconds
, the task runs fine (every 5 seconds) and the attribute is last_run_at
updated. This means that celerybeat is responsive to the scheduler to a certain extent, but if I reset last_run_at
ie PeriodicTask.objects.update(last_run_at=None)
, none of the interval tasks every day
anymore run.
Celerybeat crashed at some point and it may have damaged something, so I created a new virtualenv and database to see if the problem persists. I would like to know if there is a way to get the next execution time, so that I don't have to wait one day to see if my periodic task has completed.
I also tried using inspect <active/scheduled/reserved>
but everything returned empty
. Is this ok for periodic tasks using the djcelery database scheduler?
Here's a function that schedules tasks:
def schedule_data_collection(request, project):
if (request.method == 'POST'):
interval = request.POST.get('interval')
target_project = Project.objects.get(url_path=project)
interval_schedule = dict(every=json.loads(interval), period='days')
schedule, created = IntervalSchedule.objects.get_or_create(
every=interval_schedule['every'],
period=interval_schedule['period'],
)
task_name = '{} data collection'.format(target_project.name)
try:
task = PeriodicTask.objects.get(name=task_name)
except PeriodicTask.DoesNotExist:
task = PeriodicTask.objects.create(
interval=schedule,
name=task_name,
task='myapp.tasks.collect_tool_data',
args=json.dumps([target_project.url_path])
)
else:
if task.interval != schedule:
task.interval = schedule
if task.enabled is False:
task.enabled = True
task.save()
return HttpResponse(task.interval)
else:
return HttpResponseForbidden()
source to share