Revert "Chore(api): Async chalice.core.tenants:tenants_exists, and propagate. (#1695)"
This reverts commit 3e3d639362.
This commit is contained in:
parent
a3d3dc5e46
commit
89c16e1f1e
12 changed files with 22 additions and 78 deletions
26
api/app.py
26
api/app.py
|
|
@ -18,15 +18,6 @@ from routers.subs import insights, metrics, v1_api, health
|
|||
loglevel = config("LOGLEVEL", default=logging.WARNING)
|
||||
print(f">Loglevel set to: {loglevel}")
|
||||
logging.basicConfig(level=loglevel)
|
||||
from orpy import application
|
||||
from psycopg.rows import dict_row
|
||||
|
||||
|
||||
class ORPYAsyncConnection(AsyncConnection):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, row_factory=dict_row, **kwargs)
|
||||
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
|
|
@ -47,27 +38,10 @@ async def lifespan(app: FastAPI):
|
|||
for job in app.schedule.get_jobs():
|
||||
ap_logger.info({"Name": str(job.id), "Run Frequency": str(job.trigger), "Next Run": str(job.next_run_time)})
|
||||
|
||||
|
||||
database = {
|
||||
"host": config("pg_host", default="localhost"),
|
||||
"dbname": config("pg_dbname", default="orpy"),
|
||||
"user": config("pg_user", default="orpy"),
|
||||
"password": config("pg_password", default="orpy"),
|
||||
"port": config("pg_port", cast=int, default=5432),
|
||||
"application_name": config("APP_NAME", default="PY"),
|
||||
}
|
||||
|
||||
database = " ".join("{}={}".format(k, v) for k, v in database.items())
|
||||
database = psycopg_pool.AsyncConnectionPool(database, connection_class=ORPYAsyncConnection)
|
||||
application.set(Application(
|
||||
database,
|
||||
)
|
||||
|
||||
# App listening
|
||||
yield
|
||||
|
||||
# Shutdown
|
||||
database.close()
|
||||
logging.info(">>>>> shutting down <<<<<")
|
||||
app.schedule.shutdown(wait=False)
|
||||
await pg_client.terminate()
|
||||
|
|
|
|||
|
|
@ -11,10 +11,10 @@ from chalicelib.utils.TimeUTC import TimeUTC
|
|||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def create_tenant(data: schemas.UserSignupSchema):
|
||||
def create_tenant(data: schemas.UserSignupSchema):
|
||||
logger.info(f"==== Signup started at {TimeUTC.to_human_readable(TimeUTC.now())} UTC")
|
||||
errors = []
|
||||
if await tenants.tenants_exists():
|
||||
if tenants.tenants_exists():
|
||||
return {"errors": ["tenants already registered"]}
|
||||
|
||||
email = data.email
|
||||
|
|
|
|||
|
|
@ -52,14 +52,7 @@ def edit_tenant(tenant_id, changes):
|
|||
return helper.dict_to_camel_case(cur.fetchone())
|
||||
|
||||
|
||||
def tenants_exists_sync(use_pool=True):
|
||||
def tenants_exists(use_pool=True):
|
||||
with pg_client.PostgresClient(use_pool=use_pool) as cur:
|
||||
cur.execute("SELECT EXISTS(SELECT 1 FROM public.tenants)")
|
||||
cur.execute(f"SELECT EXISTS(SELECT 1 FROM public.tenants)")
|
||||
return cur.fetchone()["exists"]
|
||||
|
||||
|
||||
async def tenants_exists(use_pool=True):
|
||||
async with application.get().database.connection() as cnx:
|
||||
async with cnx.transaction() as txn:
|
||||
row = await txn.execute("SELECT EXISTS(SELECT 1 FROM public.tenants)")
|
||||
return await row.fetchone()["exists"]
|
||||
|
|
|
|||
11
api/orpy.py
11
api/orpy.py
|
|
@ -1,11 +0,0 @@
|
|||
from collections import namedtuple
|
||||
from contextvars import ContextVar
|
||||
|
||||
|
||||
Application = namedtuple(
|
||||
"Application",
|
||||
(
|
||||
"database",
|
||||
),
|
||||
)
|
||||
application: Application = ContextVar("application", default=None)
|
||||
|
|
@ -16,5 +16,3 @@ pydantic[email]==2.3.0
|
|||
apscheduler==3.10.4
|
||||
|
||||
redis==5.0.1
|
||||
|
||||
psycopg[pool,binary]==3.1.12
|
||||
|
|
|
|||
|
|
@ -22,20 +22,19 @@ public_app, app, app_apikey = get_routers()
|
|||
|
||||
|
||||
@public_app.get('/signup', tags=['signup'])
|
||||
async def get_all_signup():
|
||||
return {"data": {"tenants": await tenants.tenants_exists(),
|
||||
def get_all_signup():
|
||||
return {"data": {"tenants": tenants.tenants_exists(),
|
||||
"sso": None,
|
||||
"ssoProvider": None,
|
||||
"enforceSSO": None,
|
||||
"edition": license.EDITION}}
|
||||
|
||||
|
||||
|
||||
if not tenants.tenants_exists_sync(use_pool=False):
|
||||
if not tenants.tenants_exists(use_pool=False):
|
||||
@public_app.post('/signup', tags=['signup'])
|
||||
@public_app.put('/signup', tags=['signup'])
|
||||
async def signup_handler(data: schemas.UserSignupSchema = Body(...)):
|
||||
content = await signup.create_tenant(data)
|
||||
def signup_handler(data: schemas.UserSignupSchema = Body(...)):
|
||||
content = signup.create_tenant(data)
|
||||
if "errors" in content:
|
||||
return content
|
||||
refresh_token = content.pop("refreshToken")
|
||||
|
|
|
|||
|
|
@ -14,10 +14,10 @@ def get_global_health_status():
|
|||
return {"data": health.get_health()}
|
||||
|
||||
|
||||
if not tenants.tenants_exists_sync(use_pool=False):
|
||||
if not tenants.tenants_exists(use_pool=False):
|
||||
@public_app.get('/health', tags=["health-check"])
|
||||
async def get_public_health_status():
|
||||
if await tenants.tenants_exists():
|
||||
def get_public_health_status():
|
||||
if tenants.tenants_exists():
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Not Found")
|
||||
|
||||
return {"data": health.get_health()}
|
||||
|
|
|
|||
|
|
@ -13,10 +13,10 @@ from chalicelib.utils.TimeUTC import TimeUTC
|
|||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def create_tenant(data: schemas.UserSignupSchema):
|
||||
def create_tenant(data: schemas.UserSignupSchema):
|
||||
logger.info(f"==== Signup started at {TimeUTC.to_human_readable(TimeUTC.now())} UTC")
|
||||
errors = []
|
||||
if not config("MULTI_TENANTS", cast=bool, default=False) and await tenants.tenants_exists():
|
||||
if not config("MULTI_TENANTS", cast=bool, default=False) and tenants.tenants_exists():
|
||||
return {"errors": ["tenants already registered"]}
|
||||
|
||||
email = data.email
|
||||
|
|
|
|||
|
|
@ -78,14 +78,7 @@ def edit_tenant(tenant_id, changes):
|
|||
return helper.dict_to_camel_case(cur.fetchone())
|
||||
|
||||
|
||||
def tenants_exists_sync(use_pool=True):
|
||||
def tenants_exists(use_pool=True):
|
||||
with pg_client.PostgresClient(use_pool=use_pool) as cur:
|
||||
cur.execute("SELECT EXISTS(SELECT 1 FROM public.tenants)")
|
||||
cur.execute(f"SELECT EXISTS(SELECT 1 FROM public.tenants)")
|
||||
return cur.fetchone()["exists"]
|
||||
|
||||
|
||||
async def tenants_exists(use_pool=True):
|
||||
async with application.get().database as cnx:
|
||||
async with cnx.transaction() as txn:
|
||||
row = await txn.execute(f"SELECT EXISTS(SELECT 1 FROM public.tenants)")
|
||||
return await row.fetchone()["exists"]
|
||||
|
|
|
|||
|
|
@ -26,5 +26,3 @@ python-multipart==0.0.6
|
|||
redis==5.0.1
|
||||
#confluent-kafka==2.1.0
|
||||
azure-storage-blob==12.19.0
|
||||
|
||||
psycopg[pool,binary]==3.1.12
|
||||
|
|
|
|||
|
|
@ -27,8 +27,8 @@ public_app, app, app_apikey = get_routers()
|
|||
|
||||
|
||||
@public_app.get('/signup', tags=['signup'])
|
||||
async def get_all_signup():
|
||||
return {"data": {"tenants": await tenants.tenants_exists(),
|
||||
def get_all_signup():
|
||||
return {"data": {"tenants": tenants.tenants_exists(),
|
||||
"sso": SAML2_helper.is_saml2_available(),
|
||||
"ssoProvider": SAML2_helper.get_saml2_provider(),
|
||||
"enforceSSO": config("enforce_SSO", cast=bool, default=False) and helper.is_saml2_available(),
|
||||
|
|
@ -38,8 +38,8 @@ async def get_all_signup():
|
|||
if config("MULTI_TENANTS", cast=bool, default=False) or not tenants.tenants_exists(use_pool=False):
|
||||
@public_app.post('/signup', tags=['signup'])
|
||||
@public_app.put('/signup', tags=['signup'])
|
||||
async def signup_handler(data: schemas.UserSignupSchema = Body(...)):
|
||||
content = await signup.create_tenant(data)
|
||||
def signup_handler(data: schemas.UserSignupSchema = Body(...)):
|
||||
content = signup.create_tenant(data)
|
||||
if "errors" in content:
|
||||
return content
|
||||
refresh_token = content.pop("refreshToken")
|
||||
|
|
|
|||
|
|
@ -16,8 +16,8 @@ def get_global_health_status(context: schemas.CurrentContext = Depends(OR_contex
|
|||
|
||||
if not tenants.tenants_exists(use_pool=False):
|
||||
@public_app.get('/health', tags=["health-check"])
|
||||
async def get_public_health_status():
|
||||
if await tenants.tenants_exists():
|
||||
def get_public_health_status():
|
||||
if tenants.tenants_exists():
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Not Found")
|
||||
|
||||
return {"data": health.get_health()}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue