-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reorganize healthz router definition.
Add healthz route to main server.
- Loading branch information
1 parent
d1891c5
commit 2c602ad
Showing
3 changed files
with
61 additions
and
41 deletions.
There are no files selected for viewing
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,42 @@ | ||
from asyncio import Task | ||
from typing import Any | ||
|
||
from fastapi import APIRouter, HTTPException, Request | ||
|
||
from .. import __version__ | ||
from ..config import config | ||
|
||
health_router = APIRouter() | ||
"""An API Router for a health endpoint""" | ||
|
||
|
||
@health_router.get( | ||
"/healthz", | ||
description=("Return health information about the running application and its tasks, if any."), | ||
include_in_schema=True, | ||
response_model=dict[str, Any], | ||
tags=["internal", "health"], | ||
) | ||
async def get_healthz(request: Request) -> dict: | ||
"""A healthz route for application health or liveliness checking. | ||
For any tasks running in the event loop and registered in the FastAPI app's | ||
state, check whether that task is still running; return a 500 along with | ||
relevant exception information if the task has ended. | ||
""" | ||
server_ok = True | ||
healthz_response: dict[str, Any] = dict(name=config.asgi.title, version=__version__) | ||
|
||
task: Task | ||
for task in request.app.state.tasks: | ||
task_response: dict[str, bool | str | None] = {"task_running": True, "task_exception": None} | ||
if task.done(): | ||
server_ok = False | ||
task_response["task_running"] = False | ||
task_response["task_exception"] = str(task.exception()) | ||
healthz_response[task.get_name()] = task_response | ||
|
||
if not server_ok: | ||
raise HTTPException(status_code=500, detail=healthz_response) | ||
else: | ||
return healthz_response |