Improved function body
This commit is contained in:
parent
d3d3c98f99
commit
dd9e9ce110
@ -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()
|
||||
|
11
api/utils.py
11
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():
|
||||
|
Loading…
x
Reference in New Issue
Block a user