Skip to content

Commit b8b6f95

Browse files
author
Andrei Neagu
committed
refactored redis client
1 parent d928d88 commit b8b6f95

File tree

14 files changed

+32
-9
lines changed

14 files changed

+32
-9
lines changed

packages/celery-library/src/celery_library/common.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ async def create_task_manager(
4848
client_name="celery_tasks",
4949
)
5050

51+
# GCR please address https://github.com/ITISFoundation/osparc-simcore/issues/8159
52+
await redis_client_sdk.setup()
53+
5154
return CeleryTaskManager(
5255
app,
5356
settings,

packages/service-library/src/servicelib/fastapi/redis_lifespan.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ async def redis_client_sdk_lifespan(_: FastAPI, state: State) -> AsyncIterator[S
5151
redis_dsn_with_secrets,
5252
client_name=redis_state.REDIS_CLIENT_NAME,
5353
)
54+
await redis_client.setup()
5455

5556
try:
5657
yield {"REDIS_CLIENT_SDK": redis_client, **called_state}

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def redis(self) -> aioredis.Redis:
4545
return self._client
4646

4747
def __post_init__(self) -> None:
48+
self._health_check_task_started_event = asyncio.Event()
4849
self._client = aioredis.from_url(
4950
self.redis_dsn,
5051
# Run 3 retries with exponential backoff strategy source: https://redis.readthedocs.io/en/stable/backoff.html
@@ -61,8 +62,8 @@ def __post_init__(self) -> None:
6162
)
6263
# NOTE: connection is done here already
6364
self._is_healthy = False
64-
self._health_check_task_started_event = asyncio.Event()
6565

66+
async def setup(self) -> None:
6667
@periodic(interval=self.health_check_interval)
6768
async def _periodic_check_health() -> None:
6869
assert self._health_check_task_started_event # nosec
@@ -74,6 +75,12 @@ async def _periodic_check_health() -> None:
7475
name=f"redis_service_health_check_{self.redis_dsn}__{uuid4()}",
7576
)
7677

78+
# NOTE: this achieves 2 very important things:
79+
# - ensure redis is working
80+
# - before shutting down an initialized Redis connection it must
81+
# make at least one call to the servicer, otherwise tests might hang
82+
await self.ping()
83+
7784
_logger.info(
7885
"Connection to %s succeeded with %s",
7986
f"redis at {self.redis_dsn=}",
@@ -85,12 +92,10 @@ async def shutdown(self) -> None:
8592
_logger, level=logging.DEBUG, msg=f"Shutdown RedisClientSDK {self}"
8693
):
8794
if self._health_check_task:
88-
assert self._health_check_task_started_event # nosec
89-
# NOTE: wait for the health check task to have started once before we can cancel it
90-
await self._health_check_task_started_event.wait()
91-
await cancel_wait_task(
92-
self._health_check_task, max_delay=_HEALTHCHECK_TASK_TIMEOUT_S
93-
)
95+
with log_catch(_logger, reraise=False):
96+
await cancel_wait_task(
97+
self._health_check_task, max_delay=_HEALTHCHECK_TASK_TIMEOUT_S
98+
)
9499

95100
await self._client.aclose(close_connection_pool=True)
96101

@@ -99,6 +104,7 @@ async def ping(self) -> bool:
99104
# NOTE: retry_* input parameters from aioredis.from_url do not apply for the ping call
100105
await self._client.ping()
101106
return True
107+
102108
return False
103109

104110
@property

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ async def setup(self) -> None:
2727
health_check_interval=config.health_check_interval,
2828
client_name=f"{self.client_name}",
2929
)
30+
await self._client_sdks[config.database].setup()
3031

3132
async def shutdown(self) -> None:
3233
await asyncio.gather(

packages/service-library/tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ async def _(
9090
assert client.redis_dsn == redis_resources_dns
9191
assert client.client_name == "pytest"
9292

93+
await client.setup()
94+
9395
yield client
9496

9597
await client.shutdown()

packages/service-library/tests/deferred_tasks/example_app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ def __init__(
9595
)
9696

9797
async def setup(self) -> None:
98+
await self._redis_client.setup()
9899
await self._manager.setup()
99100

100101

packages/service-library/tests/deferred_tasks/test__base_deferred_handler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ async def redis_client_sdk(
5757
decode_responses=False,
5858
client_name="pytest",
5959
)
60+
await sdk.setup()
6061
yield sdk
6162
await sdk.shutdown()
6263

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ async def test_redis_client_sdk_setup_shutdown(
115115

116116
# ensure health check task sets the health to True
117117
client._is_healthy = False # noqa: SLF001
118+
119+
await client.setup()
118120
async for attempt in AsyncRetrying(
119121
wait=wait_fixed(0.1),
120122
stop=stop_after_delay(10),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ async def on_startup() -> None:
1818
app.state.redis_client_sdk = RedisClientSDK(
1919
redis_locks_dsn, client_name=APP_NAME
2020
)
21+
await app.state.redis_client_sdk.setup()
2122

2223
async def on_shutdown() -> None:
2324
redis_client_sdk: None | RedisClientSDK = app.state.redis_client_sdk

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ async def on_startup() -> None:
1919
app.state.redis_client_sdk = RedisClientSDK(
2020
redis_locks_dsn, client_name=APP_NAME
2121
)
22+
await app.state.redis_client_sdk.setup()
2223

2324
async def on_shutdown() -> None:
2425
redis_client_sdk: None | RedisClientSDK = app.state.redis_client_sdk

0 commit comments

Comments
 (0)