From a616cf69871585559c953ae9f45979e88b7b43c8 Mon Sep 17 00:00:00 2001 From: Shyam Sunder Date: Fri, 13 Mar 2020 22:43:31 -0400 Subject: [PATCH] server/migrations: implement database connection timeout --- server/szurubooru/migrations/env.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/server/szurubooru/migrations/env.py b/server/szurubooru/migrations/env.py index 59d031f6..e7d512ef 100644 --- a/server/szurubooru/migrations/env.py +++ b/server/szurubooru/migrations/env.py @@ -4,6 +4,7 @@ import sys import alembic import sqlalchemy as sa import logging.config +from time import sleep # make szurubooru module importable dir_to_self = os.path.dirname(os.path.realpath(__file__)) @@ -56,7 +57,16 @@ def run_migrations_online(): prefix='sqlalchemy.', 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( connection=connection, target_metadata=target_metadata,