Skip to content

Commit 4ef8985

Browse files
committed
adds logs on shutdown
1 parent 88f8cdb commit 4ef8985

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

packages/service-library/src/servicelib/redis/_client.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from ..async_utils import cancel_wait_task
1515
from ..background_task import periodic
16-
from ..logging_utils import log_catch
16+
from ..logging_utils import log_catch, log_context
1717
from ._constants import (
1818
DEFAULT_DECODE_RESPONSES,
1919
DEFAULT_HEALTH_CHECK_INTERVAL,
@@ -77,16 +77,20 @@ async def _periodic_check_health() -> None:
7777
)
7878

7979
async def shutdown(self) -> None:
80-
if self._health_check_task:
81-
assert self._health_check_task_started_event # nosec
82-
await self._health_check_task_started_event.wait() # NOTE: wait for the health check task to have started once before we can cancel it
83-
await cancel_wait_task(self._health_check_task)
80+
with log_context(
81+
_logger, level=logging.DEBUG, msg=f"Shutdown RedisClientSDK {self}"
82+
):
83+
if self._health_check_task:
84+
assert self._health_check_task_started_event # nosec
85+
# NOTE: wait for the health check task to have started once before we can cancel it
86+
await self._health_check_task_started_event.wait()
87+
await cancel_wait_task(self._health_check_task, max_delay=3)
8488

85-
await self._client.aclose(close_connection_pool=True)
89+
await self._client.aclose(close_connection_pool=True)
8690

8791
async def ping(self) -> bool:
8892
with log_catch(_logger, reraise=False):
89-
# FIXME: this ping should NOT retry!?
93+
# NOTE: retry_* input parameters from aioredis.from_url do not apply for the ping call
9094
await self._client.ping()
9195
return True
9296
return False

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from contextlib import AbstractAsyncContextManager
1111

1212
import pytest
13+
from pytest_mock import MockerFixture
1314
from redis.exceptions import LockError, LockNotOwnedError
1415
from servicelib.redis import RedisClientSDK
1516
from settings_library.redis import RedisDatabase, RedisSettings
@@ -133,11 +134,17 @@ async def test_regression_fails_on_redis_service_outage(
133134
mock_redis_socket_timeout: None,
134135
pause_container_in_context: Callable[[str], AbstractAsyncContextManager[None]],
135136
redis_client_sdk: RedisClientSDK,
137+
mocker: MockerFixture,
136138
):
139+
140+
redis_client_ping = mocker.spy(redis_client_sdk._client, "ping")
141+
137142
assert await redis_client_sdk.ping() is True
143+
assert redis_client_ping.call_count == 1
138144

139145
async with pause_container_in_context("redis"):
140146
# no connection available any longer should not hang but timeout
141147
assert await redis_client_sdk.ping() is False
148+
assert redis_client_ping.call_count == 2
142149

143150
assert await redis_client_sdk.ping() is True

0 commit comments

Comments
 (0)