Search code examples
pythondjangotaskceleryscheduling

How to check if celery task is already running before running it again with beat?


I have a periodic task scheduled to run every 10 minutes. Sometimes this task completes in 2-3 minutes, sometimes it takes 20 minutes.

Is there any way using celery beats to not open the task if the previous task hasn't completed yet? I don't see an option for it in the interval settings.


Solution

  • No, Celery Beat knows nothing about the running tasks.

    One way to achieve what you are trying to do is to link the task to itself. async_apply() for an example has optional parameter link and link_error which can be used to provide a signature (it can be a single task too) to run if the task finishes successfully (link) or unsuccessfully (link_error).

    What I use is the following - I schedule task to run frequently (say every 5 minutes), and I use a distributed lock to make sure I always have only one instance of the task running.

    Finally a reminder - you can always implement your own scheduler, and use it in your beat configuration. I was thinking about doing this in the past for exactly the same thing you want, but decided that the solution I already have is good enough for me.