From bbd2e82fd4188e6e40df43a46d086d43e845157f Mon Sep 17 00:00:00 2001 From: Yann Pomarede Date: Mon, 5 Dec 2016 18:40:04 +0100 Subject: [PATCH] fix race condition in orm broker --- django_q/brokers/orm.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/django_q/brokers/orm.py b/django_q/brokers/orm.py index a0db3a9..ef96912 100644 --- a/django_q/brokers/orm.py +++ b/django_q/brokers/orm.py @@ -54,11 +54,10 @@ class ORM(Broker): tasks = self.get_connection().filter(key=self.list_key, lock__lt=_timeout())[0:Conf.BULK] if tasks: task_list = [] - lock = timezone.now() for task in tasks: - task.lock = lock - task.save(update_fields=['lock']) - task_list.append((task.pk, task.payload)) + if self.get_connection().filter(id=task.id, lock=task.lock).update(lock=timezone.now()): + task_list.append((task.pk, task.payload)) + # else don't process, as another cluster has been faster than us on that task return task_list # empty queue, spare the cpu sleep(Conf.POLL)