Wait db connection, login required, dev mode enable

This commit is contained in:
Luciano Gervasoni
2025-04-04 12:28:22 +02:00
parent 4dbe2e55ef
commit 76079d7bd0
8 changed files with 105 additions and 54 deletions

View File

@@ -2,6 +2,7 @@ import argparse
import os
import psycopg
import re
import time
connection_info = "host={} port={} dbname={} user={} password={} connect_timeout=60".format(
os.environ.get("DB_HOST", "localhost"),
@@ -11,6 +12,29 @@ connection_info = "host={} port={} dbname={} user={} password={} connect_timeout
os.environ.get("DB_PASSWORD", "supermatitos")
)
def wait_connection():
connected = False
while (not connected):
try:
# Connect to an existing database
with psycopg.connect(connection_info) as conn:
# Open a cursor to perform database operations
with conn.cursor() as cur:
# Create URLs table
c = cur.execute("SELECT 1;").fetchall()
connected = True
except psycopg.OperationalError as e:
# Connection not ready...
# print(".", end="")
time.sleep(2)
except Exception as e:
# Connection not ready...
# print("e", end="")
time.sleep(2)
print("DB connection ready")
def initialize_tables():
# Connect to an existing database
with psycopg.connect(connection_info) as conn:
@@ -137,6 +161,9 @@ if __name__ == '__main__':
parser.add_argument('--initialize_data', help='Insert data', action='store_true', default=False)
args = parser.parse_args()
# Wait for DB connection
wait_connection()
if (args.initialize_tables):
print("Initializing tables")
initialize_tables()