mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-30 20:46:47 +00:00
Refactor plugin loading (#5726)
- Pre-fetch all PluginConfig objects - Only update plugin config active status when changed - Reference: https://github.com/inventree/InvenTree/issues/5586
This commit is contained in:
parent
e3fef5b226
commit
550d549325
@ -164,7 +164,7 @@ class PluginsRegistry:
|
|||||||
|
|
||||||
logger.info('Loading plugins')
|
logger.info('Loading plugins')
|
||||||
|
|
||||||
# Set maintanace mode
|
# Set maintenance mode
|
||||||
_maintenance = bool(get_maintenance_mode())
|
_maintenance = bool(get_maintenance_mode())
|
||||||
if not _maintenance:
|
if not _maintenance:
|
||||||
set_maintenance_mode(True)
|
set_maintenance_mode(True)
|
||||||
@ -232,7 +232,7 @@ class PluginsRegistry:
|
|||||||
|
|
||||||
logger.info('Start unloading plugins')
|
logger.info('Start unloading plugins')
|
||||||
|
|
||||||
# Set maintanace mode
|
# Set maintenance mode
|
||||||
_maintenance = bool(get_maintenance_mode())
|
_maintenance = bool(get_maintenance_mode())
|
||||||
if not _maintenance:
|
if not _maintenance:
|
||||||
set_maintenance_mode(True) # pragma: no cover
|
set_maintenance_mode(True) # pragma: no cover
|
||||||
@ -416,7 +416,7 @@ class PluginsRegistry:
|
|||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
|
|
||||||
# region general internal loading /activating / deactivating / deloading
|
# region general internal loading / activating / deactivating / unloading
|
||||||
def _init_plugins(self, disabled: str = None):
|
def _init_plugins(self, disabled: str = None):
|
||||||
"""Initialise all found plugins.
|
"""Initialise all found plugins.
|
||||||
|
|
||||||
@ -435,14 +435,19 @@ class PluginsRegistry:
|
|||||||
if active:
|
if active:
|
||||||
self.plugins[key] = plugin
|
self.plugins[key] = plugin
|
||||||
else:
|
else:
|
||||||
# Deactivate plugin in db
|
# Deactivate plugin in db (if currently set as active)
|
||||||
if not settings.PLUGIN_TESTING: # pragma: no cover
|
if not settings.PLUGIN_TESTING and plugin.db.active: # pragma: no cover
|
||||||
plugin.db.active = False
|
plugin.db.active = False
|
||||||
plugin.db.save(no_reload=True)
|
plugin.db.save(no_reload=True)
|
||||||
self.plugins_inactive[key] = plugin.db
|
self.plugins_inactive[key] = plugin.db
|
||||||
self.plugins_full[key] = plugin
|
self.plugins_full[key] = plugin
|
||||||
|
|
||||||
logger.debug('Starting plugin initialisation')
|
logger.debug('Starting plugin initialization')
|
||||||
|
|
||||||
|
# Fetch and cache list of existing plugin configuration instances
|
||||||
|
plugin_configs = {
|
||||||
|
cfg.key: cfg for cfg in PluginConfig.objects.all()
|
||||||
|
}
|
||||||
|
|
||||||
# Initialize plugins
|
# Initialize plugins
|
||||||
for plg in self.plugin_modules:
|
for plg in self.plugin_modules:
|
||||||
@ -451,7 +456,12 @@ class PluginsRegistry:
|
|||||||
plg_key = slugify(plg.SLUG if getattr(plg, 'SLUG', None) else plg_name) # keys are slugs!
|
plg_key = slugify(plg.SLUG if getattr(plg, 'SLUG', None) else plg_name) # keys are slugs!
|
||||||
|
|
||||||
try:
|
try:
|
||||||
plg_db, _created = PluginConfig.objects.get_or_create(key=plg_key, name=plg_name)
|
if plg_key in plugin_configs:
|
||||||
|
# Configuration already exists
|
||||||
|
plg_db = plugin_configs[plg_key]
|
||||||
|
else:
|
||||||
|
# Configuration needs to be created
|
||||||
|
plg_db, _created = PluginConfig.objects.get_or_create(key=plg_key, name=plg_name)
|
||||||
except (OperationalError, ProgrammingError) as error:
|
except (OperationalError, ProgrammingError) as error:
|
||||||
# Exception if the database has not been migrated yet - check if test are running - raise if not
|
# Exception if the database has not been migrated yet - check if test are running - raise if not
|
||||||
if not settings.PLUGIN_TESTING:
|
if not settings.PLUGIN_TESTING:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user