Skip to content

Commit

Permalink
Close cache database connection queue pre-fork
Browse files Browse the repository at this point in the history
and re-open after fork. Fixes
galaxyproject#10246.
  • Loading branch information
mvdbeek committed Sep 20, 2020
1 parent cd4909c commit 4a822ad
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/galaxy/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def __init__(self, **kwargs):
self.datatypes_registry.load_external_metadata_tool(self.toolbox)
# Load history import/export tools.
load_lib_tools(self.toolbox)
self.toolbox.persist_cache()
self.toolbox.persist_cache(register_postfork=True)
# visualizations registry: associates resources with visualizations, controls how to render
self.visualizations_registry = VisualizationsRegistry(
self,
Expand Down
12 changes: 11 additions & 1 deletion lib/galaxy/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,19 @@ def __init__(self, config_filenames, tool_root_dir, app, save_integrated_tool_pa
save_integrated_tool_panel=save_integrated_tool_panel,
)

def persist_cache(self):
def persist_cache(self, register_postfork=False):
"""
Persists any modified tool cache files to disk.
Set ``register_postfork`` to stop database thread queue,
close database connection and register re-open function
that re-opens the database after forking.
"""
for region in self.cache_regions.values():
region.persist()
if register_postfork:
region.close()
self.app.application_stack.register_postfork_function(region.reopen_ro)

def can_load_config_file(self, config_filename):
if config_filename == self.app.config.shed_tool_config_file and not self.app.config.is_set('shed_tool_config_file'):
Expand Down
10 changes: 8 additions & 2 deletions lib/galaxy/tools/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ def __init__(self, cache_dir):
# Switch SqliteDict back to readonly
self._cache.flag = 'r'

def close(self):
self._cache.close()

def reopen_ro(self):
self._cache = SqliteDict(self.cache_file, flag='r', encode=encoder, decode=decoder, autocommit=False)
self.writeable_cache_file = None

def get(self, config_file):
tool_document = self._cache.get(config_file)
if not tool_document:
Expand All @@ -65,8 +72,7 @@ def persist(self):
if self.writeable_cache_file:
self._cache.commit()
os.rename(self.writeable_cache_file.name, self.cache_file)
self._cache = SqliteDict(self.cache_file, flag='r', encode=encoder, decode=decoder, autocommit=False)
self.writeable_cache_file = None
self.reopen_ro()

def set(self, config_file, tool_source):
self.make_writable()
Expand Down

0 comments on commit 4a822ad

Please sign in to comment.