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)
|
# Fill database by initial values (only if we are not testing)
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
wait_for_db()
|
wait_for_db(max_retries=100)
|
||||||
db.create_all()
|
db.create_all()
|
||||||
if config_name != "testing":
|
if config_name != "testing":
|
||||||
init_db()
|
init_db()
|
||||||
|
11
api/utils.py
11
api/utils.py
@ -30,20 +30,15 @@ def get_user_or_404(user_id):
|
|||||||
return user
|
return user
|
||||||
|
|
||||||
|
|
||||||
MAX_RETRIES = 100
|
def wait_for_db(max_retries):
|
||||||
|
for _ in range(max_retries):
|
||||||
def wait_for_db():
|
|
||||||
for retries in range(MAX_RETRIES):
|
|
||||||
try:
|
try:
|
||||||
with db.engine.connect() as connection:
|
with db.engine.connect() as connection:
|
||||||
connection.execute(text("SELECT 1"))
|
connection.execute(text("SELECT 1"))
|
||||||
print("Successfully connected with database.")
|
|
||||||
return
|
return
|
||||||
except DatabaseError:
|
except DatabaseError:
|
||||||
print(f"Waiting for database... (retry {retries + 1})")
|
|
||||||
time.sleep(3)
|
time.sleep(3)
|
||||||
print("Failed to connect to database.")
|
raise Exception("Failed to connect to database.")
|
||||||
raise Exception("Database not ready after multiple retries.")
|
|
||||||
|
|
||||||
|
|
||||||
def init_db():
|
def init_db():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user