Skip to content

Commit

Permalink
Make periodic task retry time configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
MWedl committed Jan 14, 2025
1 parent 6969a0a commit cd04770
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 4 additions & 1 deletion api/src/reportcreator_api/tasks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class PeriodicTaskSpec:
id: str
schedule: timedelta
func: callable
retry: timedelta = timedelta(minutes=10)
max_runtime: timedelta = timedelta(minutes=10)


@dataclasses.dataclass()
Expand Down Expand Up @@ -59,11 +61,12 @@ def unregister(self, task: PeriodicTaskSpec):
periodic_task_registry = PeriodicTaskRegistry()


def periodic_task(schedule: timedelta, id: str|None = None):
def periodic_task(schedule: timedelta, id: str|None = None, retry: timedelta|None = None):
def inner(func):
periodic_task_registry.register(PeriodicTaskSpec(
id=id or f'{func.__module__}.{func.__name__}',
schedule=schedule,
retry=retry or (schedule / 10),
func=func,
))
return func
Expand Down
5 changes: 2 additions & 3 deletions api/src/reportcreator_api/tasks/querysets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
from datetime import timedelta

import elasticapm
from asgiref.sync import iscoroutinefunction, sync_to_async
Expand All @@ -19,8 +18,8 @@ async def get_pending_tasks(self):
model = task_models.get(t_id)
# Remove non-pending tasks
if model and (
(model.status == TaskStatus.RUNNING and model.started > timezone.now() - timedelta(minutes=10)) or \
(model.status == TaskStatus.FAILED and model.started > timezone.now() - timedelta(minutes=10)) or \
(model.status == TaskStatus.RUNNING and model.started > timezone.now() - spec.max_runtime) or \
(model.status == TaskStatus.FAILED and model.started > timezone.now() - spec.retry) or \
(model.status == TaskStatus.SUCCESS and model.started > timezone.now() - spec.schedule)
):
continue
Expand Down

0 comments on commit cd04770

Please sign in to comment.