Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
RB387 committed Sep 20, 2024
1 parent 12f9b33 commit 35c51e5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Dependency Injector with minimal boilerplate code, built-in support for FastAPI
* [Custom integrations](#custom-integrations)
* [Manual injection](#manual-injection)
* [Forced injections](#forced-injections)
* [Healthcheck](#healthcheck)
* [Testing](#testing)
* [Default simple mock](#default-simple-mock)
* [Custom mocks](#custom-mocks)
Expand Down Expand Up @@ -330,6 +331,38 @@ class Service(Connectable):
dependency: Annotated[NonConnectableDependency, Injectable]
```

## Healthchecks
You can implement `Pingable` protocol to define healthchecks for your clients. The `DependenciesHealthcheck` will call the `__ping__` method on all injected clients that implement this protocol.

```python
from magic_di.healthcheck import DependenciesHealthcheck


class Service(Connectable):
def __init__(self, db: Database):
self.db = db

def is_connected(self):
return self.db.connected

async def __ping__(self) -> None:
if not self.is_connected():
raise Exception("Service is not connected")


@app.get(path="/hello-world")
def hello_world(service: Provide[Service]) -> dict:
return {
"is_connected": service.is_connected()
}


@app.get(path="/healthcheck")
async def hello_world(healthcheck: Provide[DependenciesHealthcheck]) -> dict:
await healthcheck.ping_dependencies()
return {"alive": True}
```

## Testing
If you need to mock a dependency in tests, you can easily do so by using the `injector.override` context manager and still use this dependency injector.

Expand Down
3 changes: 2 additions & 1 deletion src/magic_di/healthcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ async def __ping__(self) -> None: ...
@dataclass
class DependenciesHealthcheck(Connectable):
"""
Injectable Healthcheck component that pings all dependencies that implement the PingableProtocol
Injectable Healthcheck component that pings all injected dependencies
that implement the PingableProtocol
Example usage:
Expand Down

0 comments on commit 35c51e5

Please sign in to comment.