mirror of
https://github.com/django-q2/django-q2.git
synced 2026-09-15 13:37:56 +08:00
Change task timeout logic to have now() as execution time (#58)
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user