From 1fcc67c002763b13aeb213c6675ff790657cf409 Mon Sep 17 00:00:00 2001 From: Zach Wright Date: Wed, 13 Mar 2024 13:23:17 -0400 Subject: [PATCH 1/3] KPMP-5204: add db reconnect --- app.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index 72bd2ce..1047bed 100644 --- a/app.py +++ b/app.py @@ -26,6 +26,7 @@ logger = logging.getLogger("atlas-file-service") logging.basicConfig(level=logging.ERROR) + class MYSQLConnection: def __init__(self): logger.debug( @@ -46,12 +47,17 @@ def __init__(self): "Can't load environment variables from docker... trying local .env file instead...", connectError ) - def get_db_cursor(self): + def get_db_cursor(self, connect_try=0): try: self.cursor = self.database.cursor(buffered=False, dictionary=True) return self.cursor except Exception as error: - logger.error("Can't get mysql cursor", error) + if connect_try < 5: + logger.info("Can't get cursor. Trying to reconnect to the database.") + connect_try = connect_try + 1 + self.get_db_connection() + return self.get_db_cursor(connect_try) + logger.error("Tried too many times to get mysql cursor. Exiting.", error) os.sys.exit() def get_db_connection(self): @@ -120,4 +126,3 @@ def downloadDerivedFileS3PS(packageId, objectName): logger.error(err) except botocore.exceptions.ParamValidationError as error: logger.error(err) - From 8e4e3a5edfb68c87fabfdb5a3cb45c34d268d009 Mon Sep 17 00:00:00 2001 From: Zach Wright Date: Wed, 13 Mar 2024 13:39:04 -0400 Subject: [PATCH 2/3] KPMP-5204: put that else --- app.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index 1047bed..0a29f27 100644 --- a/app.py +++ b/app.py @@ -57,8 +57,9 @@ def get_db_cursor(self, connect_try=0): connect_try = connect_try + 1 self.get_db_connection() return self.get_db_cursor(connect_try) - logger.error("Tried too many times to get mysql cursor. Exiting.", error) - os.sys.exit() + else: + logger.error("Tried too many times to get mysql cursor. Exiting.", error) + os.sys.exit() def get_db_connection(self): try: From bd59a025c286a328f639b8f0e0854c6e2955035e Mon Sep 17 00:00:00 2001 From: Zach Wright Date: Thu, 14 Mar 2024 13:50:46 -0400 Subject: [PATCH 3/3] KPMP-5204: make it a pooled connection --- app.py | 1 + 1 file changed, 1 insertion(+) diff --git a/app.py b/app.py index 0a29f27..ed77853 100644 --- a/app.py +++ b/app.py @@ -69,6 +69,7 @@ def get_db_connection(self): port=self.port, password=self.password, database=self.database_name, + pool_name="atlas-file-service" ) self.database.get_warnings = True return self.database