From 723286cc8eefdbcf344d5142b58d7a3caffb53ae Mon Sep 17 00:00:00 2001 From: Marcus Andersson Date: Tue, 18 Jan 2022 14:06:04 +0100 Subject: [PATCH] Chore: Prevent preloaded plugins from crashing Grafana (#41490) (#44143) * if a plugin fails to load, we will not crash grafana. * preventing the preloaded plugings to crash the whole app on failure. * updated to unkown. * fixed issue with angular by moving the preloadPlugin import to the same row as we did import the importPluginModule. (cherry picked from commit e926126d63f83b47dce2c5ca0130059e281cd2aa) --- public/app/app.ts | 9 ++------- public/app/features/plugins/pluginPreloader.ts | 15 +++++++++++++++ public/app/features/plugins/plugin_loader.ts | 2 +- 3 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 public/app/features/plugins/pluginPreloader.ts diff --git a/public/app/app.ts b/public/app/app.ts index c65d3edf89c..d0a2c61b930 100644 --- a/public/app/app.ts +++ b/public/app/app.ts @@ -24,8 +24,8 @@ import { standardTransformersRegistry, } from '@grafana/data'; import { arrayMove } from 'app/core/utils/arrayMove'; -import { importPluginModule } from 'app/features/plugins/plugin_loader'; import { registerEchoBackend, setEchoSrv, setPanelRenderer, setQueryRunnerFactory } from '@grafana/runtime'; +import { preloadPlugins } from './features/plugins/pluginPreloader'; import { Echo } from './core/services/echo/Echo'; import { reportPerformance } from './core/services/echo/EchoSrv'; import { PerformanceBackend } from './core/services/echo/backends/PerformanceBackend'; @@ -106,12 +106,7 @@ export class GrafanaApp { this.angularApp.init(); // Preload selected app plugins - const promises: Array> = []; - for (const plugin of config.pluginsToPreload) { - promises.push(importPluginModule(plugin.path, plugin.version)); - } - - await Promise.all(promises); + await preloadPlugins(config.pluginsToPreload); ReactDOM.render( React.createElement(AppWrapper, { diff --git a/public/app/features/plugins/pluginPreloader.ts b/public/app/features/plugins/pluginPreloader.ts new file mode 100644 index 00000000000..34c10c44097 --- /dev/null +++ b/public/app/features/plugins/pluginPreloader.ts @@ -0,0 +1,15 @@ +import { PreloadPlugin } from '@grafana/data'; +import { importPluginModule } from './plugin_loader'; + +export async function preloadPlugins(pluginsToPreload: PreloadPlugin[] = []): Promise { + await Promise.all(pluginsToPreload.map(preloadPlugin)); +} + +async function preloadPlugin(plugin: PreloadPlugin): Promise { + const { path, version } = plugin; + try { + await importPluginModule(path, version); + } catch (error: unknown) { + console.error(`Failed to load plugin: ${path} (version: ${version})`, error); + } +} diff --git a/public/app/features/plugins/plugin_loader.ts b/public/app/features/plugins/plugin_loader.ts index 4a71b2bbb45..c707a3b95ce 100644 --- a/public/app/features/plugins/plugin_loader.ts +++ b/public/app/features/plugins/plugin_loader.ts @@ -180,7 +180,7 @@ export async function importPluginModule(path: string, version?: string): Promis if (typeof builtIn === 'function') { return await builtIn(); } else { - return Promise.resolve(builtIn); + return builtIn; } } return grafanaRuntime.SystemJS.import(path);