From dd9e9ce11048f8a02af8e8d800b5561d4aeee74c Mon Sep 17 00:00:00 2001 From: Marcin-Ramotowski Date: Wed, 11 Jun 2025 19:57:15 +0000 Subject: [PATCH] Improved function body --- api/app.py | 2 +- api/utils.py | 11 +++-------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/api/app.py b/api/app.py index 8f3c1a2..8ca7625 100644 --- a/api/app.py +++ b/api/app.py @@ -53,7 +53,7 @@ def create_app(config_name="default"): # Fill database by initial values (only if we are not testing) with app.app_context(): - wait_for_db() + wait_for_db(max_retries=100) db.create_all() if config_name != "testing": init_db() diff --git a/api/utils.py b/api/utils.py index 26eda1d..e1256a4 100644 --- a/api/utils.py +++ b/api/utils.py @@ -30,20 +30,15 @@ def get_user_or_404(user_id): return user -MAX_RETRIES = 100 - -def wait_for_db(): - for retries in range(MAX_RETRIES): +def wait_for_db(max_retries): + for _ in range(max_retries): try: with db.engine.connect() as connection: connection.execute(text("SELECT 1")) - print("Successfully connected with database.") return except DatabaseError: - print(f"Waiting for database... (retry {retries + 1})") time.sleep(3) - print("Failed to connect to database.") - raise Exception("Database not ready after multiple retries.") + raise Exception("Failed to connect to database.") def init_db():