Skip to content

Commit

Permalink
Add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
RB387 committed Sep 23, 2024
1 parent 399516e commit 971a343
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/magic_di/healthcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ async def ping_dependencies(self, max_concurrency: int = 1) -> None:

try:
for dependency in self.injector.get_dependencies_by_interface(PingableProtocol):
tasks.add(asyncio.ensure_future(dependency.__ping__()))
future = asyncio.ensure_future(self.ping(dependency))
tasks.add(future)

if len(tasks) >= max_concurrency:
tasks, _ = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
Expand All @@ -57,3 +58,20 @@ async def ping_dependencies(self, max_concurrency: int = 1) -> None:
if tasks:
with suppress(asyncio.CancelledError):
await asyncio.gather(*tasks)

async def ping(self, dependency: PingableProtocol) -> None:
"""
Ping a single dependency
:param dependency: Dependency to ping
"""
dependency_name = dependency.__class__.__name__
self.injector.logger.debug("Pinging dependency %s...", dependency_name)

try:
await dependency.__ping__()
except Exception as exc:
self.injector.logger.exception("Failed to ping dependency %s", dependency_name)
raise exc # noqa: TRY201
else:
self.injector.logger.debug("Dependency %s is healthy", dependency_name)

0 comments on commit 971a343

Please sign in to comment.