Skip to content

Commit

Permalink
Merge pull request #700 from permitio/roe/per-11128-fix-opals-healthy…
Browse files Browse the repository at this point in the history
…-endpoint-for-offline-mode

Health Check: Report healthy if Offline Mode is active.
  • Loading branch information
roekatz authored Nov 14, 2024
2 parents c7e5bb4 + 7512e68 commit f16e2f3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
19 changes: 15 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 All @@ -281,11 +285,20 @@ def _configure_api_routes(self, app: FastAPI):
async def healthy():
"""returns 200 if updates keep being successfully fetched from the
server and applied to the policy store."""
# TODO: Client would only report unhealthy if server -> policy-store transactions failed, but not if server connection gets disconnected.
healthy = await self.policy_store.is_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():
# Offline Mode is active. That is enabled, client is "ready" (data loaded) but not "healthy" (latest updates failed).
# TODO: Maybe if updates were fetched from server, 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 +309,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 f16e2f3

Please sign in to comment.