From d102cf38a122ed976e684db943b450512c761618 Mon Sep 17 00:00:00 2001 From: Navan Chauhan Date: Sat, 19 Sep 2020 15:49:22 +0530 Subject: fix startup crash if database not found --- api.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'api.py') diff --git a/api.py b/api.py index fd5dfe9..db7a881 100644 --- a/api.py +++ b/api.py @@ -16,9 +16,16 @@ def gen_word(N, min_N_dig, min_N_low): shuffle(chars) return ''.join(chars) +dbFailing = False + import mysql.connector as con -mycon = con.connect(host=flask_app.config['DB_HOST'],user=flask_app.config['DB_USER'],password=flask_app.config['DB_PASSWORD'],port=flask_app.config['DB_PORT'],database=flask_app.config['DB_NAME']) -mycursor = mycon.cursor() +from mysql.connector.errors import InterfaceError +try: + mycon = con.connect(host=flask_app.config['DB_HOST'],user=flask_app.config['DB_USER'],password=flask_app.config['DB_PASSWORD'],port=flask_app.config['DB_PORT'],database=flask_app.config['DB_NAME']) + mycursor = mycon.cursor() +except InterfaceError: + print("Could not connect to the database!") + dbFailing = True """ @flask_app.route("/") @@ -38,6 +45,8 @@ async def API_Version(): @app.get("/v1/status/{job_id}") async def get_status(job_id: str): + if dbFailing: + return {"message":"Could not connect to the database"} sqlQuery = 'select id, protein_name, ligand_name, date, description, done from curieweb where id="%s"' % (job_id) mycursor.execute(sqlQuery) records = mycursor.fetchall() @@ -48,6 +57,8 @@ async def get_status(job_id: str): @app.get("/v1/3DModels/{job_id}") async def get_models(job_id: str): + if dbFailing: + return {"message":"Could not connect to the database"} sqlQuery = 'select done from curieweb where id="%s"' % (job_id) mycursor.execute(sqlQuery) records = mycursor.fetchall() @@ -59,6 +70,8 @@ async def get_models(job_id: str): @app.get("/v1/Report/{job_id}") async def get_report(job_id:str): + if dbFailing: + return {"message":"Could not connect to the database"} sqlQuery = 'select done from curieweb where id="%s"' % (job_id) mycursor.execute(sqlQuery) records = mycursor.fetchall() @@ -71,6 +84,8 @@ async def get_report(job_id:str): @app.post("/v1/docking/automatic") async def docking_automatic(pdb: str, smiles:str,compound_name:str,email:str,description:str): + if dbFailing: + return {"message":"Could not connect to the database"} if len(pdb) != 0: return {"message": "Invalid PDB ID"} -- cgit v1.2.3