From 624d16df1fd7c86c4231b2e43bf3d2b191a0d052 Mon Sep 17 00:00:00 2001 From: fumoboy007 <2100868+fumoboy007@users.noreply.github.com> Date: Mon, 9 Sep 2024 16:01:24 -0700 Subject: [PATCH] Fix `AsyncPostgresDB` initialization retry logic. Fixes #439. --- .../migration_service/data/postgres_async_db.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/services/migration_service/data/postgres_async_db.py b/services/migration_service/data/postgres_async_db.py index 9532bf65..b27812d0 100644 --- a/services/migration_service/data/postgres_async_db.py +++ b/services/migration_service/data/postgres_async_db.py @@ -35,15 +35,18 @@ def __init__(self): AsyncPostgresDB.__instance = self async def _init(self, db_conf: DBConfiguration): - # todo make poolsize min and max configurable as well as timeout - # todo add retry and better error message - retries = 3 - for i in range(retries): + # todo make poolsize min and max configurable + max_attempts = 3 + while max_attempts > 0: try: self.pool = await aiopg.create_pool(db_conf.get_dsn(), timeout=db_conf.timeout) + break except Exception as e: print("printing connection exception: " + str(e)) - if retries - i < 1: + + max_attempts -= 1 + if max_attempts == 0: raise e + time.sleep(1) continue