-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DependenciesHealthcheck dependency
- Loading branch information
Showing
11 changed files
with
217 additions
and
32 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
* @RB387 @jerry-git | ||
* @RB387 @jerry-git @erhosen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import asyncio | ||
from asyncio import Future | ||
from contextlib import suppress | ||
from dataclasses import dataclass | ||
from typing import Any, Protocol | ||
|
||
from magic_di import Connectable, ConnectableProtocol, DependencyInjector | ||
|
||
|
||
class PingableProtocol(ConnectableProtocol, Protocol): | ||
async def __ping__(self) -> None: ... | ||
|
||
|
||
@dataclass | ||
class DependenciesHealthcheck(Connectable): | ||
""" | ||
Injectable Healthcheck component that pings all injected dependencies | ||
that implement the PingableProtocol | ||
Example usage: | ||
```python | ||
from app.components.services.health import DependenciesHealthcheck | ||
async def main(redis: Redis, deps_healthcheck: DependenciesHealthcheck) -> None: | ||
await deps_healthcheck.ping_dependencies() # redis will be pinged if it has method __ping__ | ||
inject_and_run(main) | ||
``` | ||
""" | ||
|
||
injector: DependencyInjector | ||
|
||
async def ping_dependencies(self, max_concurrency: int = 1) -> None: | ||
""" | ||
Ping all dependencies that implement the PingableProtocol | ||
:param max_concurrency: Maximum number of concurrent pings | ||
""" | ||
tasks: set[Future[Any]] = set() | ||
|
||
try: | ||
for dependency in self.injector.get_dependencies_by_interface(PingableProtocol): | ||
tasks.add(asyncio.ensure_future(dependency.__ping__())) | ||
|
||
if len(tasks) >= max_concurrency: | ||
tasks, _ = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED) | ||
|
||
if tasks: | ||
await asyncio.gather(*tasks) | ||
tasks = set() | ||
|
||
finally: | ||
for task in tasks: | ||
task.cancel() | ||
|
||
if tasks: | ||
with suppress(asyncio.CancelledError): | ||
await asyncio.gather(*tasks) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from dataclasses import dataclass | ||
|
||
import pytest | ||
from magic_di import Connectable, DependencyInjector | ||
from magic_di.healthcheck import DependenciesHealthcheck | ||
|
||
|
||
@dataclass | ||
class PingableDatabase(Connectable): | ||
ping_count: int = 0 | ||
|
||
async def __ping__(self) -> None: | ||
self.ping_count += 1 | ||
|
||
|
||
@dataclass | ||
class Service(Connectable): | ||
ping_count: int = 0 | ||
|
||
|
||
@dataclass | ||
class PingableService(Connectable): | ||
db: PingableDatabase | ||
ping_count: int = 0 | ||
|
||
async def __ping__(self) -> None: | ||
self.ping_count += 1 | ||
|
||
|
||
@pytest.mark.asyncio() | ||
async def test_healthcheck(injector: DependencyInjector) -> None: | ||
async def main(_: PingableService) -> None: ... | ||
|
||
await injector.inject(main)() | ||
|
||
injected_db = injector.inject(PingableDatabase)() | ||
injected_srv = injector.inject(PingableService)() | ||
injected_srv_not_pingable = injector.inject(Service)() | ||
|
||
assert injected_db.ping_count == 0 | ||
assert injected_srv.ping_count == 0 | ||
assert injected_srv_not_pingable.ping_count == 0 | ||
|
||
healthcheck = injector.inject(DependenciesHealthcheck)() | ||
|
||
await healthcheck.ping_dependencies(max_concurrency=1) | ||
|
||
assert injected_db.ping_count == 1 | ||
assert injected_srv.ping_count == 1 | ||
assert injected_srv_not_pingable.ping_count == 0 | ||
|
||
await healthcheck.ping_dependencies(max_concurrency=3) | ||
|
||
assert injected_db.ping_count == 2 # noqa: PLR2004 | ||
assert injected_srv.ping_count == 2.0 # noqa: PLR2004 | ||
assert injected_srv_not_pingable.ping_count == 0 |
Oops, something went wrong.