Search code examples
pythondjangocelerydjango-celerycelerybeat

Celery scheduled tasks with expiration time for each task instance?


I have a django app with celery 4.1.0 and celery beat with database scheduler. What I want is to run periodic tasks from admin site and set expiration time for each of this tasks. expire property in PeriodicTask is a time scheduler stops creating new messages for that task but i want the expiration to revoke tasks which are scheduled but are older than some value e.g. one hour. how to do this?

I am really confused with celery documentation and differences between different versions of it.


Solution

  • I solved it by running an scheduled task which runs defined task with desired expiry time:

    @shared_task(bind=True, queue='q1', max_retries=3)
    def parent_task(self, arg1):
        child_task.apply_async(kwargs={'arg1': arg1}, expires=86400)
    
    
    @shared_task(bind=True, queue='q1', max_retries=3)
    def child_task(self, arg1):
        pass