2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-30 12:36:45 +00:00

reload without integration apps if loading fails

This commit is contained in:
Matthias 2021-11-20 03:16:03 +01:00
parent e301971159
commit be24d141de
No known key found for this signature in database
GPG Key ID: F50EF5741D33E076

View File

@ -57,15 +57,20 @@ class PluginAppConfig(AppConfig):
if not _maintenance: if not _maintenance:
set_maintenance_mode(True) set_maintenance_mode(True)
try: registered_sucessfull = False
# we are using the db so for migrations etc we need to try this block blocked_plugin = None
self._init_plugins() while not registered_sucessfull:
self._activate_plugins() try:
except (OperationalError, ProgrammingError): # we are using the db so for migrations etc we need to try this block
# Exception if the database has not been migrated yet self._init_plugins(blocked_plugin)
logger.info('Database not accessible while loading plugins') self._activate_plugins()
except PluginLoadingError as error: registered_sucessfull = True
logger.error(f'Encountered an error with {error.path}:\n{error.message}') except (OperationalError, ProgrammingError):
# Exception if the database has not been migrated yet
logger.info('Database not accessible while loading plugins')
except PluginLoadingError as error:
logger.error(f'Encountered an error with {error.path}:\n{error.message}')
blocked_plugin = error.path
# remove maintenance # remove maintenance
if not _maintenance: if not _maintenance:
@ -121,8 +126,13 @@ class PluginAppConfig(AppConfig):
logger.info(f'Found {len(settings.PLUGINS)} plugins!') logger.info(f'Found {len(settings.PLUGINS)} plugins!')
logger.info(", ".join([a.__module__ for a in settings.PLUGINS])) logger.info(", ".join([a.__module__ for a in settings.PLUGINS]))
def _init_plugins(self): def _init_plugins(self, disabled=None):
"""initialise all found plugins""" """initialise all found plugins
:param disabled: loading path of disabled app, defaults to None
:type disabled: str, optional
:raises error: PluginLoadingError
"""
from plugin.models import PluginConfig from plugin.models import PluginConfig
logger.info('Starting plugin initialisation') logger.info('Starting plugin initialisation')
@ -146,6 +156,20 @@ class PluginAppConfig(AppConfig):
# always activate if testing # always activate if testing
if settings.PLUGIN_TESTING or (plugin_db_setting and plugin_db_setting.active): if settings.PLUGIN_TESTING or (plugin_db_setting and plugin_db_setting.active):
# check if the plugin was blocked -> threw an error
if disabled:
if plugin.__name__==disabled:
# errors are bad so disable the plugin in the database
# but only if not in testing mode as that breaks in the GH pipeline
if not settings.PLUGIN_TESTING:
plugin_db_setting.active = False
# TODO save the error to the plugin
plugin_db_setting.save()
# add to inactive plugins so it shows up in the ui
settings.INTEGRATION_PLUGINS_INACTIVE[plug_key] = plugin_db_setting
continue # continue -> the plugin is not loaded
# init package # init package
# now we can be sure that an admin has activated the plugin -> as of Nov 2021 there are not many checks in place # now we can be sure that an admin has activated the plugin -> as of Nov 2021 there are not many checks in place
# but we could enhance those to check signatures, run the plugin against a whitelist etc. # but we could enhance those to check signatures, run the plugin against a whitelist etc.
@ -162,14 +186,18 @@ class PluginAppConfig(AppConfig):
# save for later reference # save for later reference
settings.INTEGRATION_PLUGINS_INACTIVE[plug_key] = plugin_db_setting settings.INTEGRATION_PLUGINS_INACTIVE[plug_key] = plugin_db_setting
def _activate_plugins(self): def _activate_plugins(self, force_reload=False):
"""run integration functions for all plugins""" """run integration functions for all plugins
:param force_reload: force reload base apps, defaults to False
:type force_reload: bool, optional
"""
# activate integrations # activate integrations
plugins = settings.INTEGRATION_PLUGINS.items() plugins = settings.INTEGRATION_PLUGINS.items()
logger.info(f'Found {len(plugins)} active plugins') logger.info(f'Found {len(plugins)} active plugins')
self.activate_integration_globalsettings(plugins) self.activate_integration_globalsettings(plugins)
self.activate_integration_app(plugins) self.activate_integration_app(plugins, force_reload=force_reload)
def _deactivate_plugins(self): def _deactivate_plugins(self):
"""run integration deactivation functions for all plugins""" """run integration deactivation functions for all plugins"""
@ -209,7 +237,14 @@ class PluginAppConfig(AppConfig):
# endregion # endregion
# region integration_app # region integration_app
def activate_integration_app(self, plugins): def activate_integration_app(self, plugins, force_reload=False):
"""activate AppMixin plugins - add custom apps and reload
:param plugins: list of IntegrationPlugins that should be installed
:type plugins: dict
:param force_reload: only reload base apps, defaults to False
:type force_reload: bool, optional
"""
from common.models import InvenTreeSetting from common.models import InvenTreeSetting
if settings.PLUGIN_TESTING or InvenTreeSetting.get_setting('ENABLE_PLUGINS_APP'): if settings.PLUGIN_TESTING or InvenTreeSetting.get_setting('ENABLE_PLUGINS_APP'):
@ -225,9 +260,9 @@ class PluginAppConfig(AppConfig):
settings.INTEGRATION_APPS_PATHS += [plugin_path] settings.INTEGRATION_APPS_PATHS += [plugin_path]
apps_changed = True apps_changed = True
if apps_changed: if apps_changed or force_reload:
# if apps were changed reload # if apps were changed reload
if settings.INTEGRATION_APPS_LOADING: if settings.INTEGRATION_APPS_LOADING or force_reload:
settings.INTEGRATION_APPS_LOADING = False settings.INTEGRATION_APPS_LOADING = False
self._reload_apps(populate=True) self._reload_apps(populate=True)
self._reload_apps() self._reload_apps()