Skip to content

Commit

Permalink
Merge pull request #8 from teams-notifier/db-check-on-startup
Browse files Browse the repository at this point in the history
perform small connection test on startup
  • Loading branch information
babs authored Nov 24, 2024
2 parents 9606f09 + 9e66531 commit 172c4da
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 172c4da

Please sign in to comment.