0

I want to use django-celery-beat library to make some changes in my database periodically. I set task to run each 10 minutes. Everything working fine till my task takes less than 10 minutes, if it lasts longer next tasks starts while first one is doing calculations and it couses an error.

my tasks loks like that:

from celery import shared_task
from .utils.database_blockchain import BlockchainVerify


@shared_task()
def run_function():
    build_block = BlockchainVerify()
    return "Database updated"

is there a way to avoid starting the same task if previous wasn't done ?

harveeey
  • 41
  • 7
  • Does this answer your question? [Make celery wait for task to finish](https://stackoverflow.com/questions/60912356/make-celery-wait-for-task-to-finish) – gold_cy Jul 25 '21 at 17:28

1 Answers1

2

There is definitely a way. It's locking.

There is whole page in the celery documentation - Ensuring a task is only executed one at a time.

Shortly explained - you can use some cache or even database to put lock in and then every time some task starts just check if this lock is still in use or has been already released.

Be aware of that the task may fail or run longer than expected. Task failure may be handled by adding some expiration to the lock. And set the lock expiration to be long enough just in case the task is still running.

There already is a good thread on SO - link.

Kryštof Řeháček
  • 1,965
  • 1
  • 16
  • 28