Skip to content

Commit cea3e84

Browse files
committed
rename
1 parent 154917a commit cea3e84

File tree

22 files changed

+55
-69
lines changed

22 files changed

+55
-69
lines changed

packages/service-library/src/servicelib/async_utils.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
from contextlib import suppress
88
from dataclasses import dataclass
99
from functools import wraps
10-
from typing import TYPE_CHECKING, Any, Final, ParamSpec, TypeVar
11-
12-
from tenacity import before_sleep_log, retry, stop_after_attempt
10+
from typing import TYPE_CHECKING, Any, ParamSpec, TypeVar
1311

1412
from . import tracing
1513
from .utils_profiling_middleware import dont_profile, is_profiling, profile_context
@@ -234,30 +232,20 @@ async def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
234232
return decorator
235233

236234

237-
_MAX_TASK_CANCELLATION_ATTEMPTS: Final[int] = 3
238-
239-
240-
@retry(
241-
reraise=True,
242-
stop=stop_after_attempt(_MAX_TASK_CANCELLATION_ATTEMPTS),
243-
before_sleep=before_sleep_log(_logger, logging.WARNING, exc_info=True),
244-
)
245-
async def retried_cancel_task(
235+
async def cancel_wait_task(
246236
task: asyncio.Task,
247237
*,
248-
timeout: float | None = None, # noqa: ASYNC109
238+
max_delay: float | None = None,
249239
) -> None:
250-
"""Reliable task cancellation. Some libraries will just hang without
251-
cancelling the task. The operation is retried _MAX_TASK_CANCELLATION_ATTEMPTS times
252-
in case of timeout.
240+
"""Cancel a asyncio.Task and waits for it to finish.
253241
254242
:param task: task to be canceled
255-
:param timeout_delay: duration (in seconds) to wait before giving
256-
up the cancellation. If None it waits forever. Will be repeated _MAX_TASK_CANCELLATION_ATTEMPTS times.
243+
:param max_delay: duration (in seconds) to wait before giving
244+
up the cancellation. If None it waits forever.
257245
:raises TimeoutError: raised if cannot cancel the task.
258246
"""
259247

260248
task.cancel()
261-
async with asyncio.timeout(timeout):
249+
async with asyncio.timeout(max_delay):
262250
with contextlib.suppress(asyncio.CancelledError):
263251
await task

packages/service-library/src/servicelib/background_task.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from tenacity import TryAgain, before_sleep_log, retry, retry_if_exception_type
1010
from tenacity.wait import wait_fixed
1111

12-
from .async_utils import retried_cancel_task, with_delay
12+
from .async_utils import cancel_wait_task, with_delay
1313
from .logging_utils import log_context
1414

1515
_logger = logging.getLogger(__name__)
@@ -134,6 +134,4 @@ async def periodic_task(
134134
if asyncio_task is not None:
135135
# NOTE: this stopping is shielded to prevent the cancellation to propagate
136136
# into the stopping procedure
137-
await asyncio.shield(
138-
retried_cancel_task(asyncio_task, timeout=stop_timeout)
139-
)
137+
await asyncio.shield(cancel_wait_task(asyncio_task, max_delay=stop_timeout))

packages/service-library/tests/redis/test_decorators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import arrow
1212
import pytest
1313
from faker import Faker
14-
from servicelib.async_utils import retried_cancel_task
14+
from servicelib.async_utils import cancel_wait_task
1515
from servicelib.redis import (
1616
CouldNotAcquireLockError,
1717
RedisClientSDK,
@@ -217,7 +217,7 @@ async def _assert_task_completes_once(
217217

218218
await _assert_on_sleep_done(sleep_events, stop_after=stop_after)
219219

220-
await retried_cancel_task(started_task, timeout=5)
220+
await cancel_wait_task(started_task, max_delay=5)
221221

222222
events_timestamps: tuple[float, ...] = tuple(
223223
x.args[0].timestamp() for x in sleep_events.call_args_list

packages/service-library/tests/test_background_task.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import pytest
1515
from faker import Faker
1616
from pytest_mock.plugin import MockerFixture
17-
from servicelib.async_utils import retried_cancel_task
17+
from servicelib.async_utils import cancel_wait_task
1818
from servicelib.background_task import ( # Assuming the module is imported correctly
1919
create_periodic_task,
2020
periodic,
@@ -73,7 +73,7 @@ async def _creator(
7373
yield _creator
7474
# cleanup
7575
await asyncio.gather(
76-
*(retried_cancel_task(t, timeout=stop_task_timeout) for t in created_tasks)
76+
*(cancel_wait_task(t, max_delay=stop_task_timeout) for t in created_tasks)
7777
)
7878

7979

services/agent/src/simcore_service_agent/services/volumes_manager.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from fastapi import FastAPI
1010
from models_library.projects_nodes_io import NodeID
1111
from pydantic import NonNegativeFloat
12-
from servicelib.async_utils import retried_cancel_task
12+
from servicelib.async_utils import cancel_wait_task
1313
from servicelib.background_task import create_periodic_task
1414
from servicelib.fastapi.app_state import SingletonInAppStateMixin
1515
from servicelib.logging_utils import log_context
@@ -61,10 +61,10 @@ async def shutdown(self) -> None:
6161
await self.docker.close()
6262

6363
if self._task_bookkeeping:
64-
await retried_cancel_task(self._task_bookkeeping)
64+
await cancel_wait_task(self._task_bookkeeping)
6565

6666
if self._task_periodic_volume_cleanup:
67-
await retried_cancel_task(self._task_periodic_volume_cleanup)
67+
await cancel_wait_task(self._task_periodic_volume_cleanup)
6868

6969
async def _bookkeeping_task(self) -> None:
7070
with log_context(_logger, logging.DEBUG, "volume bookkeeping"):

services/api-server/src/simcore_service_api_server/core/_prometheus_instrumentation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from fastapi import FastAPI
77
from prometheus_client import CollectorRegistry, Gauge
88
from pydantic import PositiveInt
9-
from servicelib.async_utils import retried_cancel_task
9+
from servicelib.async_utils import cancel_wait_task
1010
from servicelib.background_task import create_periodic_task
1111
from servicelib.fastapi.prometheus_instrumentation import (
1212
setup_prometheus_instrumentation as setup_rest_instrumentation,
@@ -80,7 +80,7 @@ async def on_startup() -> None:
8080
async def on_shutdown() -> None:
8181
assert app.state.instrumentation_task # nosec
8282
with log_catch(_logger, reraise=False):
83-
await retried_cancel_task(app.state.instrumentation_task)
83+
await cancel_wait_task(app.state.instrumentation_task)
8484

8585
app.add_event_handler("startup", on_startup)
8686
app.add_event_handler("shutdown", on_shutdown)

services/api-server/src/simcore_service_api_server/core/health_checker.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from models_library.rabbitmq_messages import LoggerRabbitMessage
1010
from models_library.users import UserID
1111
from pydantic import NonNegativeInt, PositiveFloat, PositiveInt
12-
from servicelib.async_utils import retried_cancel_task
12+
from servicelib.async_utils import cancel_wait_task
1313
from servicelib.background_task import create_periodic_task
1414
from servicelib.fastapi.dependencies import get_app
1515
from servicelib.logging_utils import log_catch
@@ -63,8 +63,8 @@ async def setup(self, health_check_task_period_seconds: PositiveFloat):
6363
async def teardown(self):
6464
if self._background_task:
6565
with log_catch(_logger, reraise=False):
66-
await retried_cancel_task(
67-
self._background_task, timeout=self._timeout_seconds
66+
await cancel_wait_task(
67+
self._background_task, max_delay=self._timeout_seconds
6868
)
6969
await self._log_distributor.deregister(job_id=self._dummy_job_id)
7070

services/autoscaling/src/simcore_service_autoscaling/modules/auto_scaling_task.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import Final
44

55
from fastapi import FastAPI
6-
from servicelib.async_utils import retried_cancel_task
6+
from servicelib.async_utils import cancel_wait_task
77
from servicelib.background_task import create_periodic_task
88
from servicelib.redis import exclusive
99

@@ -44,7 +44,7 @@ async def _startup() -> None:
4444

4545
def on_app_shutdown(app: FastAPI) -> Callable[[], Awaitable[None]]:
4646
async def _stop() -> None:
47-
await retried_cancel_task(app.state.autoscaler_task)
47+
await cancel_wait_task(app.state.autoscaler_task)
4848

4949
return _stop
5050

services/autoscaling/src/simcore_service_autoscaling/modules/buffer_machines_pool_task.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import Final
44

55
from fastapi import FastAPI
6-
from servicelib.async_utils import retried_cancel_task
6+
from servicelib.async_utils import cancel_wait_task
77
from servicelib.background_task import create_periodic_task
88
from servicelib.redis import exclusive
99

@@ -44,7 +44,7 @@ async def _startup() -> None:
4444
def on_app_shutdown(app: FastAPI) -> Callable[[], Awaitable[None]]:
4545
async def _stop() -> None:
4646
if hasattr(app.state, "buffers_pool_task"):
47-
await retried_cancel_task(app.state.buffers_pool_task)
47+
await cancel_wait_task(app.state.buffers_pool_task)
4848

4949
return _stop
5050

services/clusters-keeper/src/simcore_service_clusters_keeper/modules/clusters_management_task.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from collections.abc import Awaitable, Callable
44

55
from fastapi import FastAPI
6-
from servicelib.async_utils import retried_cancel_task
6+
from servicelib.async_utils import cancel_wait_task
77
from servicelib.background_task import create_periodic_task
88
from servicelib.redis import exclusive
99

@@ -37,7 +37,7 @@ async def _startup() -> None:
3737

3838
def on_app_shutdown(app: FastAPI) -> Callable[[], Awaitable[None]]:
3939
async def _stop() -> None:
40-
await retried_cancel_task(app.state.clusters_cleaning_task, timeout=5)
40+
await cancel_wait_task(app.state.clusters_cleaning_task, max_delay=5)
4141

4242
return _stop
4343

0 commit comments

Comments
 (0)