From 8cf2143e7a35c3884ec3c32090b677a0629429c4 Mon Sep 17 00:00:00 2001 From: "kacper.golem" Date: Thu, 16 May 2024 13:51:52 +0200 Subject: [PATCH 1/2] OCT-1643: Teardown session after request finished --- backend/app/__init__.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/backend/app/__init__.py b/backend/app/__init__.py index 96b5c9c289..900e2816c4 100644 --- a/backend/app/__init__.py +++ b/backend/app/__init__.py @@ -2,6 +2,7 @@ from flask import Flask +from app.engine.epochs_settings import register_epoch_settings from app.extensions import ( db, migrate, @@ -13,12 +14,11 @@ init_scheduler, init_subgraph, ) -from app.logging import init_logger from app.infrastructure import events, routes, apscheduler # noqa from app.infrastructure.exception_handler import ExceptionHandler -from app.settings import ProdConfig, DevConfig -from app.engine.epochs_settings import register_epoch_settings +from app.logging import init_logger from app.modules.registry import register_services +from app.settings import ProdConfig, DevConfig def create_app(config=None): @@ -31,6 +31,7 @@ def create_app(config=None): template_folder=f"{config.PROJECT_ROOT}/templates", static_folder=f"{config.PROJECT_ROOT}/static", ) + app.teardown_request(teardown_session) app.config.from_object(config) register_extensions(app) @@ -57,3 +58,7 @@ def register_extensions(app): def register_errorhandlers(app): handler = ExceptionHandler() app.register_error_handler(Exception, handler) + + +def teardown_session(*args, **kwargs): + db.session.remove() From aa454c6eeaf658ded1b71b8a04d718798179adff Mon Sep 17 00:00:00 2001 From: "kacper.golem" Date: Thu, 16 May 2024 15:12:20 +0200 Subject: [PATCH 2/2] fix api tests --- backend/app/__init__.py | 6 +----- backend/startup.py | 7 +++++++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/backend/app/__init__.py b/backend/app/__init__.py index 900e2816c4..1608020af3 100644 --- a/backend/app/__init__.py +++ b/backend/app/__init__.py @@ -31,7 +31,7 @@ def create_app(config=None): template_folder=f"{config.PROJECT_ROOT}/templates", static_folder=f"{config.PROJECT_ROOT}/static", ) - app.teardown_request(teardown_session) + app.config.from_object(config) register_extensions(app) @@ -58,7 +58,3 @@ def register_extensions(app): def register_errorhandlers(app): handler = ExceptionHandler() app.register_error_handler(Exception, handler) - - -def teardown_session(*args, **kwargs): - db.session.remove() diff --git a/backend/startup.py b/backend/startup.py index 33aa0bd56a..a8a5ba2348 100644 --- a/backend/startup.py +++ b/backend/startup.py @@ -11,6 +11,7 @@ # we prevent this monkeypatching import os +from app.extensions import db os.environ["EVENTLET_NO_GREENDNS"] = "yes" import eventlet # noqa @@ -20,5 +21,11 @@ app = create_app() + +@app.teardown_request +def teardown_session(*args, **kwargs): + db.session.remove() + + if __name__ == "__main__": eventlet.wsgi.server(eventlet.listen(("0.0.0.0", 5000)), app, log=app.logger)