employeed Sqlalchemy - pool pre ping to help with db connect errors.
This commit is contained in:
+2
-15
@@ -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
|
||||
|
||||
|
||||
+2
-1
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user