refactor DB health-check

This commit is contained in:
David Rodenkirchen
2024-09-03 17:33:37 +02:00
parent 1ca7db6427
commit eb7d94d46c
3 changed files with 26 additions and 8 deletions
+13 -1
View File
@@ -29,6 +29,17 @@ class DatabaseService:
self._database_config = database_config
self._connection_pool: Optional[aiomysql.Pool] = None
async def is_healthy(self) -> bool:
try:
async with self._connection_pool.acquire() as conn:
async with conn.cursor() as _:
return True
except aiomysql.OperationalError:
return False
except Exception as e:
logger.error(f"Failed to acquire a connection: {e}")
return False
async def init_db_pool(self) -> bool:
logger.info(
f"Connecting to database '{self._database_config.db_name}' on "
@@ -42,7 +53,7 @@ class DatabaseService:
password=self._database_config.db_password,
db=self._database_config.db_name,
minsize=1,
maxsize=20
maxsize=40
)
except aiomysql.OperationalError:
return False
@@ -215,6 +226,7 @@ class DatabaseService:
except aiomysql.InterfaceError:
pool_init_result = await self.init_db_pool()
if not pool_init_result:
print(self._connection_pool)
raise NoDatabaseConnectionError
return await self.get_news(dt_start, dt_end)
except Exception as e: