Before move to Gunicorn

This commit is contained in:
Lucas Mathews
2024-06-15 20:11:04 +02:00
parent a1d326c018
commit 1c614f23b6
11 changed files with 96 additions and 56 deletions

View File

@@ -1,9 +1,12 @@
# Lucas Mathews - Fontys Student ID: 5023572
# Banking System Manager File
# Banking System Databas File
from config import CONFIG # Import Config
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.exc import OperationalError
import time
from logger import event_logger
db_type = CONFIG.get('database', 'type')
db_user = CONFIG.get('database', 'user')
@@ -14,11 +17,31 @@ db_name = CONFIG.get('database', 'name')
db_url : str = f"{db_type}://{db_user}:{db_password}@{db_ip}:{db_port}/{db_name}"
engine = create_engine(db_url, echo=True) # Creates the database engine
try: # Retry connecting to the database with a retry mechanism
max_retries = 10
retries = 0
engine = None
while retries < max_retries:
try:
engine = create_engine(db_url, echo=True)
break
except OperationalError as e:
event_logger(f"Failed to connect to database: {e}")
retries += 1
event_logger(f"Retrying ({retries}/{max_retries})...")
time.sleep(10) # Wait 10 seconds before retrying
from class_base import Base # Imports the base class required by SQLAlchemy
if engine:
from class_base import Base # Imports the base class required by SQLAlchemy
Base.metadata.create_all(bind=engine) # Creates the tables in the database from the classes
Session = sessionmaker(bind=engine) # Creates a session to interact with the database
session = Session() # Creates a session object
Base.metadata.create_all(bind=engine) # Creates the tables in the database from the classes
except Exception as e:
event_logger(f"Error: {e}")
Session = sessionmaker(bind=engine) # Creates a session to interact with the database
session = Session() # Creates a session object
finally:
if 'session' in locals():
session.close() # Close the session when done
event_logger("Database operations completed.")