Skip to content

Commit c959b03

Browse files
committed
in sync
1 parent acb5223 commit c959b03

File tree

1 file changed

+11
-5
lines changed
  • packages/service-library/src/servicelib/fastapi/long_running_tasks

1 file changed

+11
-5
lines changed

packages/service-library/src/servicelib/fastapi/long_running_tasks/client.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import asyncio
6+
import logging
67
from collections.abc import AsyncGenerator, Coroutine
78
from dataclasses import dataclass
89
from typing import Any, Final, TypeAlias
@@ -14,6 +15,7 @@
1415
from tenacity import (
1516
AsyncRetrying,
1617
TryAgain,
18+
before_sleep_log,
1719
retry,
1820
retry_if_exception_type,
1921
stop_after_delay,
@@ -33,20 +35,23 @@
3335
from ._client import DEFAULT_HTTP_REQUESTS_TIMEOUT, Client, setup
3436
from ._context_manager import periodic_task_result
3537

38+
_logger = logging.getLogger(__name__)
39+
3640
RequestBody: TypeAlias = Any
3741

3842
_MINUTE: Final[int] = 60 # in secs
3943
_HOUR: Final[int] = 60 * _MINUTE # in secs
4044
_DEFAULT_POLL_INTERVAL_S: Final[float] = 1
41-
_DEFAULT_AIOHTTP_RETRY_POLICY: dict[str, Any] = {
45+
_DEFAULT_FASTAPI_RETRY_POLICY: dict[str, Any] = {
4246
"retry": retry_if_exception_type(httpx.RequestError),
4347
"wait": wait_random_exponential(max=20),
4448
"stop": stop_after_delay(60),
4549
"reraise": True,
50+
"before_sleep": before_sleep_log(_logger, logging.INFO),
4651
}
4752

4853

49-
@retry(**_DEFAULT_AIOHTTP_RETRY_POLICY)
54+
@retry(**_DEFAULT_FASTAPI_RETRY_POLICY)
5055
async def _start(
5156
session: httpx.AsyncClient, url: URL, json: RequestBody | None
5257
) -> TaskGet:
@@ -56,7 +61,7 @@ async def _start(
5661
return TaskGet.model_validate(data)
5762

5863

59-
@retry(**_DEFAULT_AIOHTTP_RETRY_POLICY)
64+
@retry(**_DEFAULT_FASTAPI_RETRY_POLICY)
6065
async def _wait_for_completion(
6166
session: httpx.AsyncClient,
6267
task_id: TaskId,
@@ -68,6 +73,7 @@ async def _wait_for_completion(
6873
stop=stop_after_delay(client_timeout),
6974
reraise=True,
7075
retry=retry_if_exception_type(TryAgain),
76+
before_sleep=before_sleep_log(_logger, logging.DEBUG),
7177
):
7278
with attempt:
7379
response = await session.get(f"{status_url}")
@@ -93,7 +99,7 @@ async def _wait_for_completion(
9399
raise TimeoutError(msg) from exc
94100

95101

96-
@retry(**_DEFAULT_AIOHTTP_RETRY_POLICY)
102+
@retry(**_DEFAULT_FASTAPI_RETRY_POLICY)
97103
async def _task_result(session: httpx.AsyncClient, result_url: URL) -> Any:
98104
response = await session.get(f"{result_url}", params={"return_exception": True})
99105
response.raise_for_status()
@@ -102,7 +108,7 @@ async def _task_result(session: httpx.AsyncClient, result_url: URL) -> Any:
102108
return None
103109

104110

105-
@retry(**_DEFAULT_AIOHTTP_RETRY_POLICY)
111+
@retry(**_DEFAULT_FASTAPI_RETRY_POLICY)
106112
async def _abort_task(session: httpx.AsyncClient, abort_url: URL) -> None:
107113
response = await session.delete(f"{abort_url}")
108114
response.raise_for_status()

0 commit comments

Comments
 (0)