Skip to content

Commit

Permalink
upgrade usage of aioredis
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderegg committed Mar 17, 2022
1 parent b90cfae commit 6c5c763
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
19 changes: 13 additions & 6 deletions packages/pytest-simcore/src/pytest_simcore/redis_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ async def redis_client(
redis_settings: RedisSettings,
) -> AsyncIterator[aioredis.Redis]:
"""Creates a redis client to communicate with a redis service ready"""
client = await aioredis.create_redis_pool(redis_settings.dsn, encoding="utf-8")
client = aioredis.from_url(
redis_settings.dsn, encoding="utf-8", decode_responses=True
)

yield client

await client.flushall()
client.close()
await client.wait_closed()
await client.disconnect()


# HELPERS --
Expand All @@ -77,6 +78,12 @@ async def redis_client(
reraise=True,
)
async def wait_till_redis_responsive(redis_url: Union[URL, str]) -> None:
client = await aioredis.create_redis_pool(str(redis_url), encoding="utf-8")
client.close()
await client.wait_closed()
try:
client = aioredis.from_url(
f"{redis_url}", encoding="utf-8", decode_responses=True
)

if not await client.ping():
raise ConnectionError(f"{redis_url=} not available")
finally:
await client.disconnect()
14 changes: 9 additions & 5 deletions services/web/server/src/simcore_service_webserver/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ async def _create_client(address: str) -> aioredis.Redis:
reraise=True,
):
with attempt:
client = await aioredis.create_redis_pool(address, encoding="utf-8")
client = aioredis.from_url(
redis_settings.dsn, encoding="utf-8", decode_responses=True
)
if not await client.ping():
raise ConnectionError(
f"Connection to {redis_settings.dsn!r} failed"
)
log.info(
"Connection to %s succeeded with %s [%s]",
f"redis at {address=}",
Expand Down Expand Up @@ -89,10 +95,8 @@ async def _create_client(address: str) -> aioredis.Redis:
log.critical("Invalid redis lock manager in app")

# close clients
client.close()
await client.wait_closed()
client_lock_db.close()
await client_lock_db.wait_closed()
await client.disconnect()
await client_lock_db.disconnect()
# delete lock manager
await lock_manager.destroy()

Expand Down
7 changes: 4 additions & 3 deletions services/web/server/tests/integration/01/test_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,11 @@ def __drop_and_recreate_postgres__(

@pytest.fixture(autouse=True)
async def __delete_all_redis_keys__(redis_settings: RedisSettings):
client = await aioredis.create_redis_pool(redis_settings.dsn, encoding="utf-8")
client = aioredis.from_url(
redis_settings.dsn, encoding="utf-8", decode_responses=True
)
await client.flushall()
client.close()
await client.wait_closed()
await client.disconnect()

yield
# do nothing on teadown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ def __drop_and_recreate_postgres__(database_from_template_before_each_function)

@pytest.fixture(autouse=True)
async def __delete_all_redis_keys__(redis_settings: RedisSettings):
client = await aioredis.create_redis_pool(redis_settings.dsn, encoding="utf-8")
client = aioredis.from_url(
redis_settings.dsn, encoding="utf-8", decode_responses=True
)
await client.flushall()
client.close()
await client.wait_closed()
await client.disconnect()

yield
# do nothing on teadown
Expand Down
7 changes: 4 additions & 3 deletions services/web/server/tests/unit/with_dbs/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,13 @@ def redis_service(docker_services, docker_ip) -> URL:

@pytest.fixture
async def redis_client(redis_service: URL):
client = await aioredis.create_redis_pool(str(redis_service), encoding="utf-8")
client = aioredis.from_url(
f"{redis_service}", encoding="utf-8", decode_responses=True
)
yield client

await client.flushall()
client.close()
await client.wait_closed()
await client.disconnect()


def _is_redis_responsive(host: str, port: int) -> bool:
Expand Down

0 comments on commit 6c5c763

Please sign in to comment.