From cdce6815ebe28c689b2cb385b5d0952d732a4f61 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 18 Nov 2024 09:33:00 +0000 Subject: [PATCH] Better management of plugins file - Use file hash to determine if it should be reloaded --- src/backend/InvenTree/InvenTree/settings.py | 3 ++- src/backend/InvenTree/plugin/installer.py | 16 ++++++++++++++-- src/backend/InvenTree/plugin/registry.py | 14 ++++++-------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/backend/InvenTree/InvenTree/settings.py b/src/backend/InvenTree/InvenTree/settings.py index 7e294b8ac8..443a572d26 100644 --- a/src/backend/InvenTree/InvenTree/settings.py +++ b/src/backend/InvenTree/InvenTree/settings.py @@ -160,7 +160,8 @@ PLUGIN_RETRY = get_setting( 'INVENTREE_PLUGIN_RETRY', 'PLUGIN_RETRY', 3, typecast=int ) # How often should plugin loading be tried? -PLUGIN_FILE_CHECKED = False # Was the plugin file checked? +# Hash of the plugin file (will be updated on each change) +PLUGIN_FILE_HASH = '' STATICFILES_DIRS = [] diff --git a/src/backend/InvenTree/plugin/installer.py b/src/backend/InvenTree/plugin/installer.py index 9cc3917a85..6d652f7360 100644 --- a/src/backend/InvenTree/plugin/installer.py +++ b/src/backend/InvenTree/plugin/installer.py @@ -103,6 +103,19 @@ def get_install_info(packagename: str) -> str: return info +def plugins_file_hash(): + """Return the file hash for the plugins file.""" + import hashlib + + pf = settings.PLUGIN_FILE + + if not pf or not pf.exists(): + return None + + with pf.open('rb') as f: + return hashlib.md5(f.read()).hexdigest() + + def install_plugins_file(): """Install plugins from the plugins file.""" logger.info('Installing plugins from plugins file') @@ -127,9 +140,8 @@ def install_plugins_file(): log_error('pip') return False - # Update static files + # Collect plugin static files plugin.staticfiles.collect_plugins_static_files() - plugin.staticfiles.clear_plugins_static_files() # At this point, the plugins file has been installed return True diff --git a/src/backend/InvenTree/plugin/registry.py b/src/backend/InvenTree/plugin/registry.py index 4c29e06fea..5f75002035 100644 --- a/src/backend/InvenTree/plugin/registry.py +++ b/src/backend/InvenTree/plugin/registry.py @@ -287,6 +287,7 @@ class PluginsRegistry: if collect: logger.info('Collecting plugins') + self.install_plugin_file() self.plugin_modules = self.collect_plugins() self.plugins_loaded = False @@ -430,16 +431,13 @@ class PluginsRegistry: def install_plugin_file(self): """Make sure all plugins are installed in the current environment.""" - if settings.PLUGIN_FILE_CHECKED: - logger.info('Plugin file was already checked') - return True + from plugin.installer import install_plugins_file, plugins_file_hash - from plugin.installer import install_plugins_file + file_hash = plugins_file_hash() - if install_plugins_file(): - settings.PLUGIN_FILE_CHECKED = True - return 'first_run' - return False + if file_hash != settings.PLUGIN_FILE_HASH: + install_plugins_file() + settings.PLUGIN_FILE_HASH = file_hash # endregion