Skip to content

Commit

Permalink
Clean up factory handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdbeek committed Aug 1, 2024
1 parent d83b56b commit 20cedf7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
11 changes: 5 additions & 6 deletions lib/galaxy/web/framework/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def __init__(self):
self.transaction_factory = DefaultWebTransaction
# Set if trace logging is enabled
self.trace_logger = None
self.session_factories = []

def add_ui_controller(self, controller_name, controller):
"""
Expand Down Expand Up @@ -170,14 +171,12 @@ def __call__(self, environ, start_response):
path_info = environ.get("PATH_INFO", "")

try:
self._model.set_request_id(request_id) # Start SQLAlchemy session scope
if self._install_model:
self._install_model.set_request_id(request_id)
for session_factory in self.session_factories:
session_factory.set_request_id(request_id) # Start SQLAlchemy session scope
return self.handle_request(request_id, path_info, environ, start_response)
finally:
self._model.unset_request_id(request_id) # End SQLAlchemy session scope
if self._install_model:
self._install_model.unset_request_id(request_id)
for session_factory in self.session_factories:
session_factory.unset_request_id(request_id) # End SQLAlchemy session scope
self.trace(message="Handle request finished")
if self.trace_logger:
self.trace_logger.context_remove("request_id")
Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/webapps/base/webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ def __init__(

# We need this to set the REQUEST_ID contextvar in model.base *BEFORE* a GalaxyWebTransaction is created.
# This will ensure a SQLAlchemy session is request-scoped for legacy (non-fastapi) endpoints.
self._model = galaxy_app.model
self._install_model = getattr(galaxy_app, "install_model", None)
self.session_factories.append(galaxy_app.model)

def build_apispec(self):
"""
Expand Down
9 changes: 9 additions & 0 deletions lib/galaxy/webapps/galaxy/buildapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import sys
import threading
import traceback
from typing import Optional
from urllib.parse import urljoin

from paste import httpexceptions
Expand All @@ -20,6 +21,7 @@
import galaxy.webapps.base.webapp
from galaxy import util
from galaxy.security.validate_user_input import VALID_PUBLICNAME_RE
from galaxy.structured_app import MinimalApp
from galaxy.util import asbool
from galaxy.util.properties import load_app_properties
from galaxy.web.framework.middleware.error import ErrorMiddleware
Expand All @@ -34,6 +36,13 @@
class GalaxyWebApplication(galaxy.webapps.base.webapp.WebApplication):
injection_aware = True

def __init__(
self, galaxy_app: MinimalApp, session_cookie: str = "galaxysession", name: Optional[str] = None
) -> None:
super().__init__(galaxy_app, session_cookie, name)
self.session_factories.append(galaxy_app.install_model)


def app_factory(*args, **kwargs):
"""
Return a wsgi application serving the root object
Expand Down

0 comments on commit 20cedf7

Please sign in to comment.