Skip to content

Commit 0d3c0a4

Browse files
committed
Add health checks
1 parent e50babe commit 0d3c0a4

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

diracx-routers/pyproject.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ types = [
4141
]
4242

4343
[project.entry-points."diracx.services"]
44-
jobs = "diracx.routers.jobs:router"
45-
config = "diracx.routers.configuration:router"
46-
auth = "diracx.routers.auth:router"
4744
".well-known" = "diracx.routers.auth.well_known:router"
45+
auth = "diracx.routers.auth:router"
46+
config = "diracx.routers.configuration:router"
47+
health = "diracx.routers.health:router"
48+
jobs = "diracx.routers.jobs:router"
4849

4950
[project.entry-points."diracx.access_policies"]
5051
WMSAccessPolicy = "diracx.routers.jobs.access_policies:WMSAccessPolicy"

diracx-routers/src/diracx/routers/fastapi_classes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,10 @@ def __init__(
8282
*,
8383
dependencies=None,
8484
require_auth: bool = True,
85+
include_in_schema: bool = True,
8586
path_root: str = "/api",
8687
):
87-
super().__init__(dependencies=dependencies)
88+
super().__init__(dependencies=dependencies, include_in_schema=include_in_schema)
8889
self.diracx_require_auth = require_auth
8990
self.diracx_path_root = path_root
9091

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from __future__ import annotations
2+
3+
__all__ = ["router"]
4+
5+
from ..fastapi_classes import DiracxRouter
6+
from .probes import router as probes_router
7+
8+
router = DiracxRouter(require_auth=False, include_in_schema=False)
9+
router.include_router(probes_router)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""Health probes for use in Kubernetes and other orchestration systems."""
2+
3+
from __future__ import annotations
4+
5+
__all__ = ["router"]
6+
7+
from fastapi import HTTPException
8+
from starlette.responses import JSONResponse
9+
10+
from ..dependencies import AuthDB, Config
11+
from ..fastapi_classes import DiracxRouter
12+
13+
router = DiracxRouter(require_auth=False)
14+
15+
16+
@router.get("/live", include_in_schema=False)
17+
async def liveness():
18+
"""Returns a simple status to indicate the app is running."""
19+
return JSONResponse(content={"status": "live"})
20+
21+
22+
@router.get("/ready", include_in_schema=False)
23+
async def readiness(config: Config, auth_db: AuthDB):
24+
"""Readiness endpoint.
25+
26+
Checks if at least the configuration is loaded and the AuthDB database
27+
connection is available.
28+
"""
29+
if not any(vo_registry.Users for vo_registry in config.Registry.values()):
30+
raise HTTPException(status_code=503, detail="No users in registry")
31+
try:
32+
await auth_db.ping()
33+
except Exception as e:
34+
raise HTTPException(status_code=503, detail="AuthDB ping failed") from e
35+
return JSONResponse(content={"status": "ready"})
36+
37+
38+
@router.get("/startup", include_in_schema=False)
39+
async def startup_check(config: Config, auth_db: AuthDB):
40+
"""Startup endpoint.
41+
42+
Checks if at least the configuration is loaded and the AuthDB database
43+
connection is available.
44+
"""
45+
if not any(vo_registry.Users for vo_registry in config.Registry.values()):
46+
raise HTTPException(status_code=503, detail="No users in registry")
47+
try:
48+
await auth_db.ping()
49+
except Exception as e:
50+
raise HTTPException(status_code=503, detail="AuthDB ping failed") from e
51+
return JSONResponse(content={"status": "startup complete"})

0 commit comments

Comments
 (0)