Skip to content

Commit

Permalink
Health Check: Report healthy if Offline Mode is active.
Browse files Browse the repository at this point in the history
The health check would instead return a boolean `online` in its response json, to reflect whether data is up to date with server (or loaded from backup)
  • Loading branch information
roekatz committed Nov 13, 2024
1 parent c7e5bb4 commit d0601a3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
18 changes: 14 additions & 4 deletions packages/opal-client/opal_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ def _init_fast_api_app(self):
self._configure_lifecycle_callbacks(app)
return app

async def _is_ready(self):
# Data loaded from file or from server
return self._backup_loaded or await self.policy_store.is_ready()

def _configure_api_routes(self, app: FastAPI):
"""mounts the api routes on the app object."""

Expand Down Expand Up @@ -285,7 +289,15 @@ async def healthy():

if healthy:
return JSONResponse(
status_code=status.HTTP_200_OK, content={"status": "ok"}
status_code=status.HTTP_200_OK,
content={"status": "ok", "online": True},
)
elif self.offline_mode_enabled and await self._is_ready():
# When offline mode is enabled, OPAL should be considered healthy when no server updates are available
# TODO: Maybe if server updates were available but storing them to OPA wasn't successful, we should return 503 even with offline mode enabled
return JSONResponse(
status_code=status.HTTP_200_OK,
content={"status": "ok", "online": False},
)
else:
return JSONResponse(
Expand All @@ -296,9 +308,7 @@ async def healthy():
@app.get("/ready", include_in_schema=False)
async def ready():
"""returns 200 if the policy store is ready to serve requests."""
ready = self._backup_loaded or await self.policy_store.is_ready()

if ready:
if await self._is_ready():
return JSONResponse(
status_code=status.HTTP_200_OK, content={"status": "ok"}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ async def log_transaction(self, transaction: StoreTransaction):
async def is_healthy(self) -> bool:
raise NotImplementedError()

async def is_ready(self) -> bool:
raise NotImplementedError()

async def full_export(self, writer: AsyncTextIOWrapper) -> None:
raise NotImplementedError()

Expand Down

0 comments on commit d0601a3

Please sign in to comment.