feat(chalice): dynamic health-check endpoint
This commit is contained in:
parent
0fe1adf522
commit
11af708520
3 changed files with 16 additions and 14 deletions
|
|
@ -68,7 +68,7 @@ def update(tenant_id, user_id, data: schemas.UpdateTenantSchema):
|
|||
return edit_client(tenant_id=tenant_id, changes=changes)
|
||||
|
||||
|
||||
def tenants_exists():
|
||||
with pg_client.PostgresClient() as cur:
|
||||
def tenants_exists(use_pool=True):
|
||||
with pg_client.PostgresClient(use_pool=use_pool) as cur:
|
||||
cur.execute(f"SELECT EXISTS(SELECT 1 FROM public.tenants)")
|
||||
return cur.fetchone()["exists"]
|
||||
|
|
|
|||
|
|
@ -87,9 +87,10 @@ class PostgresClient:
|
|||
long_query = False
|
||||
unlimited_query = False
|
||||
|
||||
def __init__(self, long_query=False, unlimited_query=False):
|
||||
def __init__(self, long_query=False, unlimited_query=False, use_pool=True):
|
||||
self.long_query = long_query
|
||||
self.unlimited_query = unlimited_query
|
||||
self.use_pool = use_pool
|
||||
if unlimited_query:
|
||||
long_config = dict(_PG_CONFIG)
|
||||
long_config["application_name"] += "-UNLIMITED"
|
||||
|
|
@ -100,7 +101,7 @@ class PostgresClient:
|
|||
long_config["options"] = f"-c statement_timeout=" \
|
||||
f"{config('pg_long_timeout', cast=int, default=5 * 60) * 1000}"
|
||||
self.connection = psycopg2.connect(**long_config)
|
||||
elif not config('PG_POOL', cast=bool, default=True):
|
||||
elif not use_pool or not config('PG_POOL', cast=bool, default=True):
|
||||
single_config = dict(_PG_CONFIG)
|
||||
single_config["application_name"] += "-NOPOOL"
|
||||
single_config["options"] = f"-c statement_timeout={config('PG_TIMEOUT', cast=int, default=30) * 1000}"
|
||||
|
|
@ -120,11 +121,12 @@ class PostgresClient:
|
|||
try:
|
||||
self.connection.commit()
|
||||
self.cursor.close()
|
||||
if self.long_query or self.unlimited_query:
|
||||
if not self.use_pool or self.long_query or self.unlimited_query:
|
||||
self.connection.close()
|
||||
except Exception as error:
|
||||
logging.error("Error while committing/closing PG-connection", error)
|
||||
if str(error) == "connection already closed" \
|
||||
and self.use_pool \
|
||||
and not self.long_query \
|
||||
and not self.unlimited_query \
|
||||
and config('PG_POOL', cast=bool, default=True):
|
||||
|
|
@ -134,6 +136,7 @@ class PostgresClient:
|
|||
raise error
|
||||
finally:
|
||||
if config('PG_POOL', cast=bool, default=True) \
|
||||
and self.use_pool \
|
||||
and not self.long_query \
|
||||
and not self.unlimited_query:
|
||||
postgreSQL_pool.putconn(self.connection)
|
||||
|
|
|
|||
|
|
@ -1,15 +1,14 @@
|
|||
from typing import Union
|
||||
|
||||
from fastapi import Body, Depends, Request
|
||||
|
||||
import schemas
|
||||
from chalicelib.core import health
|
||||
from or_dependencies import OR_context
|
||||
from chalicelib.core import health, tenants
|
||||
from routers.base import get_routers
|
||||
|
||||
public_app, app, app_apikey = get_routers()
|
||||
|
||||
health_router = public_app
|
||||
|
||||
@public_app.get('/health', tags=["dashboard"])
|
||||
def get_global_health():
|
||||
if tenants.tenants_exists(use_pool=False):
|
||||
health_router = app
|
||||
|
||||
|
||||
@health_router.get('/health', tags=["health-check"])
|
||||
def get_global_health_status():
|
||||
return {"data": health.get_health()}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue