Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Celery: access to time of last run of task?

Using celery, is it possible from within a task to check when the last time that task was run is?

I'd like to implement "grab everything since the last time you were run". I can either keep track of the last run timestamp myself or hopefully get access to it from celery.

This is from within a django project, if that makes a difference.

like image 701
Parand Avatar asked Jul 26 '11 22:07

Parand


3 Answers

By default celery doesn't have a permanent storage of the task results, if you are running in beat mode some clean process will run for clean task results and also execution infos. Want I suggest you is to use a NoSQL for store every last execution date, you can do this by override in your task the method after_return

def after_return(self, status, retval, task_id, args, kwargs, einfo):
    #exit point for context managers
    self.taskLogger.__exit__(status, retval, task_id, args, kwargs, einfo)

This method is called every time the task ends, with any result, by check the status you can store your date only when the task finish with SUCCESS or implement the behavior that best fits your needs.

like image 185
Mauro Rocco Avatar answered Nov 18 '22 13:11

Mauro Rocco


If you are using django-celery, last_run at is saved in the model. Use the normal django ORM to filter the PeriodicTask model using your task name.

from djcelery.models import PeriodicTask

last_run = PeriodicTask.objects.only('last_run_at').get(task='TASK_NAME_HERE').last_run_at
like image 38
phenicie Avatar answered Nov 18 '22 14:11

phenicie


In the latest version of celery(5.1.2), last run time of a task can be obtained by this code.

#tasks.py
from django_celery_beat.models import PeriodicTask

@shared_task(bind=True)
def backup_everyday(self)   
    try:
        last_run = PeriodicTask.objects.get(task=self.name).last_run_at
    except:
        last_run = None

backup everyday is just an example function. PeriodicTask model has an attribute named "last_run_at" which stored the previous execution time of a task.

like image 2
Vineet Widhani Avatar answered Nov 18 '22 14:11

Vineet Widhani