From f4abefe14994fd935a11a17137c58aa7a1a2b04b Mon Sep 17 00:00:00 2001 From: Taha Yassine Kraiem Date: Fri, 9 Aug 2024 10:48:15 +0100 Subject: [PATCH] refactor(chalice): refactored heath-check package refactor(chalice): enhanced scope caching dev(chalice): reduced Spot JWT TTL for testing purposes --- api/chalicelib/core/health.py | 4 ++-- api/chalicelib/core/scope.py | 2 +- api/env.default | 2 +- api/routers/subs/health.py | 7 +++++-- ee/api/.gitignore | 1 + ee/api/chalicelib/core/scope.py | 2 +- ee/api/clean-dev.sh | 3 ++- ee/api/env.default | 2 +- ee/api/routers/subs/health.py | 23 ----------------------- 9 files changed, 14 insertions(+), 32 deletions(-) delete mode 100644 ee/api/routers/subs/health.py diff --git a/api/chalicelib/core/health.py b/api/chalicelib/core/health.py index 6734021fe4..574b2649ae 100644 --- a/api/chalicelib/core/health.py +++ b/api/chalicelib/core/health.py @@ -11,7 +11,7 @@ def app_connection_string(name, port, path): namespace = config("POD_NAMESPACE", default="app") conn_string = config("CLUSTER_URL", default="svc.cluster.local") - return f"http://{'.'.join(filter(None,[name,namespace,conn_string]))}:{port}/{path}" + return f"http://{'.'.join(filter(None, [name, namespace, conn_string]))}:{port}/{path}" HEALTH_ENDPOINTS = { @@ -174,7 +174,7 @@ def __get_sessions_stats(*_): } -def get_health(): +def get_health(tenant_id=None): health_map = { "databases": { "postgres": __check_database_pg diff --git a/api/chalicelib/core/scope.py b/api/chalicelib/core/scope.py index 8e90466024..44d3a8963f 100644 --- a/api/chalicelib/core/scope.py +++ b/api/chalicelib/core/scope.py @@ -4,7 +4,7 @@ from chalicelib.utils import helper from chalicelib.utils import pg_client -cache = TTLCache(maxsize=1, ttl=24 * 60 * 60) +cache = TTLCache(maxsize=1, ttl=60) @cached(cache) diff --git a/api/env.default b/api/env.default index eee43e4fb0..7351d63523 100644 --- a/api/env.default +++ b/api/env.default @@ -33,7 +33,7 @@ JWT_REFRESH_SECRET="SET A RANDOM STRING HERE" JWT_SPOT_REFRESH_EXPIRATION=604800 JWT_SPOT_REFRESH_SECRET="SET A RANDOM STRING HERE" JWT_SPOT_SECRET=SECRET -JWT_SPOT_EXPIRATION=6000 +JWT_SPOT_EXPIRATION=300 jwt_secret="SET A RANDOM STRING HERE" pg_dbname=postgres pg_host=postgresql.db.svc.cluster.local diff --git a/api/routers/subs/health.py b/api/routers/subs/health.py index 245f039c70..3a41feb45f 100644 --- a/api/routers/subs/health.py +++ b/api/routers/subs/health.py @@ -1,17 +1,20 @@ from decouple import config +from fastapi import Depends from fastapi import HTTPException, status +import schemas from chalicelib.core import health, tenants +from or_dependencies import OR_context from routers.base import get_routers public_app, app, app_apikey = get_routers() @app.get('/healthz', tags=["health-check"]) -def get_global_health_status(): +def get_global_health_status(context: schemas.CurrentContext = Depends(OR_context)): if config("LOCAL_DEV", cast=bool, default=False): return {"data": ""} - return {"data": health.get_health()} + return {"data": health.get_health(context.tenant_id)} if not tenants.tenants_exists_sync(use_pool=False): diff --git a/ee/api/.gitignore b/ee/api/.gitignore index c6060a76e5..8d6bc0036e 100644 --- a/ee/api/.gitignore +++ b/ee/api/.gitignore @@ -275,3 +275,4 @@ Pipfile.lock /chalicelib/core/db_request_handler.py /routers/subs/spot.py /chalicelib/utils/or_cache/ +/routers/subs/health.py diff --git a/ee/api/chalicelib/core/scope.py b/ee/api/chalicelib/core/scope.py index 4494d71055..9c8e48fd57 100644 --- a/ee/api/chalicelib/core/scope.py +++ b/ee/api/chalicelib/core/scope.py @@ -4,7 +4,7 @@ from chalicelib.utils import helper from chalicelib.utils import pg_client -cache = TTLCache(maxsize=1, ttl=24 * 60 * 60) +cache = TTLCache(maxsize=1000, ttl=60) @cached(cache) diff --git a/ee/api/clean-dev.sh b/ee/api/clean-dev.sh index c9858ca365..ccc3d402f3 100755 --- a/ee/api/clean-dev.sh +++ b/ee/api/clean-dev.sh @@ -95,4 +95,5 @@ rm -rf ./chalicelib/core/usability_testing/ rm -rf ./chalicelib/core/db_request_handler.py rm -rf ./chalicelib/core/db_request_handler.py rm -rf ./routers/subs/spot.py -rm -rf ./chalicelib/utils/or_cache \ No newline at end of file +rm -rf ./chalicelib/utils/or_cache +rm -rf ./routers/subs/health.py \ No newline at end of file diff --git a/ee/api/env.default b/ee/api/env.default index e4d6f1948f..baad697cd5 100644 --- a/ee/api/env.default +++ b/ee/api/env.default @@ -51,7 +51,7 @@ JWT_REFRESH_SECRET="SET A RANDOM STRING HERE" JWT_SPOT_REFRESH_EXPIRATION=604800 JWT_SPOT_REFRESH_SECRET="SET A RANDOM STRING HERE" JWT_SPOT_SECRET=SECRET -JWT_SPOT_EXPIRATION=6000 +JWT_SPOT_EXPIRATION=300 jwt_secret="SET A RANDOM STRING HERE" KAFKA_SERVERS=kafka.db.svc.cluster.local:9092 KAFKA_USE_SSL=false diff --git a/ee/api/routers/subs/health.py b/ee/api/routers/subs/health.py deleted file mode 100644 index 21f9433b71..0000000000 --- a/ee/api/routers/subs/health.py +++ /dev/null @@ -1,23 +0,0 @@ -from fastapi import Depends -from fastapi import HTTPException, status - -import schemas -from chalicelib.core import health, tenants -from or_dependencies import OR_context -from routers.base import get_routers - -public_app, app, app_apikey = get_routers() - - -@app.get('/healthz', tags=["health-check"]) -def get_global_health_status(context: schemas.CurrentContext = Depends(OR_context)): - return {"data": health.get_health(tenant_id=context.tenant_id)} - - -if not tenants.tenants_exists_sync(use_pool=False): - @public_app.get('/health', tags=["health-check"]) - async def get_public_health_status(): - if await tenants.tenants_exists(): - raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Not Found") - - return {"data": health.get_health()}