Skip to content

Commit 4bf353b

Browse files
author
Andrei Neagu
committed
added total attempts count as well
1 parent 9b717ce commit 4bf353b

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

packages/service-library/src/servicelib/deferred_tasks/_base_deferred_handler.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,17 @@ async def get_retries(cls, context: DeferredContext) -> NonNegativeInt:
4747

4848
@classmethod
4949
async def get_retry_delay(
50-
cls, context: DeferredContext, remaining_attempts: NonNegativeInt
50+
cls,
51+
context: DeferredContext,
52+
remaining_attempts: NonNegativeInt,
53+
total_attempts: NonNegativeInt,
5154
) -> timedelta:
5255
"""
5356
returns: the delay between eatch retry attempt (default: 0s)
5457
"""
5558
assert context # nosec
5659
assert remaining_attempts # nosec
60+
assert total_attempts # nosec
5761
return timedelta(seconds=0)
5862

5963
@classmethod

packages/service-library/src/servicelib/deferred_tasks/_deferred_manager.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,11 @@ async def __start(
297297
subclass = self.__get_subclass(class_unique_reference)
298298
deferred_context = self.__get_deferred_context(start_context)
299299

300+
retry_count = await subclass.get_retries(deferred_context)
300301
task_schedule = TaskScheduleModel(
301302
timeout=await subclass.get_timeout(deferred_context),
302-
execution_attempts=await subclass.get_retries(deferred_context) + 1,
303+
total_attempts=retry_count,
304+
execution_attempts=retry_count + 1,
303305
class_unique_reference=class_unique_reference,
304306
start_context=start_context,
305307
state=TaskState.SCHEDULED,
@@ -467,6 +469,7 @@ async def _fs_handle_error_result( # pylint:disable=method-hidden
467469
sleep_interval = await subclass.get_retry_delay(
468470
context=deferred_context,
469471
remaining_attempts=task_schedule.execution_attempts,
472+
total_attempts=task_schedule.total_attempts,
470473
)
471474
await asyncio.sleep(sleep_interval.total_seconds())
472475

packages/service-library/src/servicelib/deferred_tasks/_task_schedule.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class TaskScheduleModel(BaseModel):
3939
..., description="represents the execution step of the task"
4040
)
4141

42+
total_attempts: NonNegativeInt = Field(
43+
...,
44+
description="maximum number of attempts before giving up (0 means no retries)",
45+
)
46+
4247
execution_attempts: NonNegativeInt = Field(
4348
...,
4449
description="remaining attempts to run the code, only retries if this is > 0",

0 commit comments

Comments
 (0)