Skip to content

Commit

Permalink
perform small connection test on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
babs committed Nov 24, 2024
1 parent 9606f09 commit 9e66531
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 4 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,16 @@ async def my_periodic_task():
async def init_helpers(app: web.Application):
"""Initialize a connection pool."""
try:
app["helpers"] = Helpers(
helpers = Helpers(
MessageHelper(app, ADAPTER, CONFIG),
MSGraphHelper(app, ADAPTER, CONFIG),
DBHelper(app, CONFIG),
)
app["helpers"] = helpers
await helpers.db.check_connection()
except Exception as e:
logger.exception(e)
raise e

yield

Expand Down
4 changes: 4 additions & 0 deletions helpers/db_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ async def pool(self) -> asyncpg.Pool:
async def acquire(self) -> asyncpg.pool.PoolAcquireContext:
return (await self.pool()).acquire()

async def check_connection(self):
async with await self.acquire() as connection:
await connection.fetchval("SELECT 1")

async def save_message_for_deletion(self, conv_id: str, activity_id: str) -> None:
connection: asyncpg.pool.PoolConnectionProxy
async with await self.acquire() as connection:
Expand Down

0 comments on commit 9e66531

Please sign in to comment.