refactor DB health-check
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user