Wrap call to ensure session is closed

Otherwise there's an idle transaction left in the database (+locks)
This commit is contained in:
John Davis
2024-04-02 10:08:53 -04:00
parent cc23a9dcd3
commit 6a800c0f78
3 changed files with 8 additions and 9 deletions
+1 -1
View File
@@ -324,7 +324,7 @@ class AdminController(BaseUIController, Admin):
message = escape(kwd.get("message", ""))
status = kwd.get("status", "done")
if "regenerate_statistics_button" in kwd:
trans.app.shed_counter.generate_statistics()
trans.app.shed_counter.generate_statistics(trans.sa_session)
message = "Successfully regenerated statistics"
return trans.fill_template("/webapps/tool_shed/admin/statistics.mako", message=message, status=status)
+5
View File
@@ -45,4 +45,9 @@ def init(
result.security_agent = CommunityRBACAgent(result)
result.shed_counter = shed_statistics.ShedCounter(result)
session = result.session()
with session.begin():
result.shed_counter.generate_statistics(session)
return result
+2 -8
View File
@@ -23,14 +23,8 @@ class ShedCounter:
self.unique_owners = 0
self.unique_valid_tools = 0
self.workflows = 0
self.generate_statistics()
@property
def sa_session(self):
"""Returns a SQLAlchemy session"""
return self.model.context
def generate_statistics(self):
def generate_statistics(self, sa_session):
self.custom_datatypes = 0
self.deleted_repositories = 0
self.deprecated_repositories = 0
@@ -42,7 +36,7 @@ class ShedCounter:
self.unique_valid_tools = 0
self.workflows = 0
unique_user_ids = []
for repository in self.sa_session.scalars(select(Repository)):
for repository in sa_session.scalars(select(Repository)):
self.repositories += 1
self.total_clones += repository.times_downloaded
is_deleted = repository.deleted