From 865b1a5ba8ba41b60885c6d5276263e55b00fdd3 Mon Sep 17 00:00:00 2001 From: msabatier <37879561+msabatier@users.noreply.github.com> Date: Wed, 11 Jan 2023 01:48:57 +0100 Subject: [PATCH] Change task timeout logic to have now() as execution time (#58) --- django_q/brokers/orm.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/django_q/brokers/orm.py b/django_q/brokers/orm.py index de209b4..3c11261 100644 --- a/django_q/brokers/orm.py +++ b/django_q/brokers/orm.py @@ -11,7 +11,7 @@ from django_q.models import OrmQ def _timeout(): - return timezone.now() - timedelta(seconds=Conf.RETRY) + return timezone.now() + timedelta(seconds=Conf.RETRY) class ORM(Broker): @@ -31,13 +31,13 @@ class ORM(Broker): def queue_size(self) -> int: return ( self.get_connection() - .filter(key=self.list_key, lock__lte=_timeout()) + .filter(key=self.list_key, lock__lte=timezone.now()) .count() ) def lock_size(self) -> int: return ( - self.get_connection().filter(key=self.list_key, lock__gt=_timeout()).count() + self.get_connection().filter(key=self.list_key, lock__gt=timezone.now()).count() ) def purge_queue(self): @@ -56,12 +56,12 @@ class ORM(Broker): def enqueue(self, task): package = self.get_connection().create( - key=self.list_key, payload=task, lock=_timeout() + key=self.list_key, payload=task, lock=timezone.now() ) return package.pk def dequeue(self): - tasks = self.get_connection().filter(key=self.list_key, lock__lt=_timeout())[ + tasks = self.get_connection().filter(key=self.list_key, lock__lt=timezone.now())[ 0 : Conf.BULK # noqa: E203 ] if tasks: @@ -70,7 +70,7 @@ class ORM(Broker): if ( self.get_connection() .filter(id=task.id, lock=task.lock) - .update(lock=timezone.now()) + .update(lock=_timeout()) ): task_list.append((task.pk, task.payload)) # else don't process, as another cluster has been faster than us on