From 0d8a917760b641ea81104f0a42773f8f18767329 Mon Sep 17 00:00:00 2001 From: tcely Date: Mon, 14 Jul 2025 05:31:51 -0400 Subject: [PATCH] Add the `running` function to `TaskHistory.objects` --- tubesync/common/models/tasks.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tubesync/common/models/tasks.py b/tubesync/common/models/tasks.py index b02b0892..335fda14 100644 --- a/tubesync/common/models/tasks.py +++ b/tubesync/common/models/tasks.py @@ -30,6 +30,20 @@ def th_schedule(cls, task_wrapper, /, *args, remove_duplicates=False, vn_args=() class TaskHistoryQuerySet(models.QuerySet): + def running(self, now=None, within=None): + if now is None: + now = timezone.now() + qs = self.filter( + start_at=models.F('end_at'), + scheduled_at__lte=models.F('end_at'), + ).order_by('end_at') + if within: + if not isinstance(within, timedelta): + within = timedelta(seconds=within) + time_limit = now - within + qs = qs.filter(end_at__gt=time_limit) + return qs + def failed(self, within=None): """ :param within: A timedelta object