From ad77ad75127fa0914bbc2f64515b7fc01ea35350 Mon Sep 17 00:00:00 2001 From: Kenny Heinonen Date: Fri, 7 May 2021 18:26:41 +0300 Subject: [PATCH] Fixes #225 (#552) --- django_q/cluster.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/django_q/cluster.py b/django_q/cluster.py index 503ef71..9c9e5c8 100644 --- a/django_q/cluster.py +++ b/django_q/cluster.py @@ -476,8 +476,10 @@ def save_task(task, broker: Broker): # SAVE LIMIT > 0: Prune database, SAVE_LIMIT 0: No pruning close_old_django_connections() try: - if task["success"] and 0 < Conf.SAVE_LIMIT <= Success.objects.count(): - Success.objects.last().delete() + with db.transaction.atomic(): + last = Success.objects.select_for_update().last() + if task["success"] and 0 < Conf.SAVE_LIMIT <= Success.objects.count(): + last.delete() # check if this task has previous results if Task.objects.filter(id=task["id"], name=task["name"]).exists(): existing_task = Task.objects.get(id=task["id"], name=task["name"])