From 68cff4e02862b6a73158ab196639b344dc47abd6 Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Sun, 3 Aug 2025 22:46:22 -0400 Subject: [PATCH] employeed Sqlalchemy - pool pre ping to help with db connect errors. --- app/__init__.py | 17 ++--------------- app/main.py | 3 ++- config.py | 3 +++ 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index a316d47..1ed39fe 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -13,26 +13,13 @@ def create_app(config_class='config.Config'): app = Flask(__name__) app.config.from_object(config_class) + app.config['SQLALCHEMY_ENGINE_OPTIONS'] = config_class.SQLALCHEMY_ENGINE_OPTIONS + db.init_app(app) migrate.init_app(app, db) login_manager.init_app(app) login_manager.login_view = 'auth.login' - with app.app_context(): - # Enable connection ping only for PostgreSQL - if app.config['SQLALCHEMY_DATABASE_URI'].startswith("postgresql"): - from sqlalchemy import event, text - from sqlalchemy.exc import DisconnectionError - - @event.listens_for(db.engine, "engine_connect") - def ping_connection(connection, branch): - if branch: - return - try: - connection.execution_options(isolation_level="AUTOCOMMIT").scalar(text("SELECT 1")) - except: - raise DisconnectionError() - from app.main import main from app.auth import auth diff --git a/app/main.py b/app/main.py index 48f19e8..28b953a 100644 --- a/app/main.py +++ b/app/main.py @@ -21,7 +21,8 @@ def index(): background_url = None if image: - filename = image.filepath.replace('/images/', '') + path = image.thumb_path or image.filepath + filename = path.replace('/images/', '') background_url = url_for('main.serve_image', filename=filename) return render_template('index.html', background_url=background_url) diff --git a/config.py b/config.py index c1eabaf..f1bcdb9 100644 --- a/config.py +++ b/config.py @@ -28,3 +28,6 @@ class Config: SQLALCHEMY_TRACK_MODIFICATIONS = False + SQLALCHEMY_ENGINE_OPTIONS = { + "pool_pre_ping": True + }