Implement /api/health API endpoint that checks database connection
#1890
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
On this branch, I implemented a dedicated health check API endpoint, which is at
/api/health. Unlike the/versionAPI endpoint, this new endpoint checks whether the FastAPI app can talk to the Postgres server. I plan to use this new endpoint for Cloudflare health checks and for KubernetesreadinessProbes.The reason the
HealthResponse(Pydantic model) class's definition is in theapi.pyfile is that themodels.pyfile seemed to me to contain only SQLAlchemy models—not Pydantic models—and the Pydantic models I saw elsewhere were in the same files as the functions that use them. The only function that uses this Pydantic model is inapi.py.An analogous endpoint in
nmdc-runtimewas introduced into that application here.Screenshots
When database connection is healthy, the endpoint returns an HTTP 200 response with both properties set to
true.When database connection is unhealthy, the endpoint returns an HTTP 503 response with the database property set to
false.Related issues
Fixes: #1889