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.
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.
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
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.
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