server/migrations: implement database connection timeout

This commit is contained in:
Shyam Sunder 2020-03-13 22:43:31 -04:00
parent e3401b3993
commit a616cf6987

View file

@ -4,6 +4,7 @@ import sys
import alembic import alembic
import sqlalchemy as sa import sqlalchemy as sa
import logging.config import logging.config
from time import sleep
# make szurubooru module importable # make szurubooru module importable
dir_to_self = os.path.dirname(os.path.realpath(__file__)) dir_to_self = os.path.dirname(os.path.realpath(__file__))
@ -56,7 +57,16 @@ def run_migrations_online():
prefix='sqlalchemy.', prefix='sqlalchemy.',
poolclass=sa.pool.NullPool) poolclass=sa.pool.NullPool)
with connectable.connect() as connection: def connect_with_timeout(connectable, timeout=45):
dt = 5
for _ in range(int(timeout / dt)):
try:
return connectable.connect()
except sa.exc.OperationalError:
sleep(dt)
return connectable.connect()
with connect_with_timeout(connectable) as connection:
alembic.context.configure( alembic.context.configure(
connection=connection, connection=connection,
target_metadata=target_metadata, target_metadata=target_metadata,