From e926126d63f83b47dce2c5ca0130059e281cd2aa Mon Sep 17 00:00:00 2001 From: Marcus Andersson Date: Wed, 10 Nov 2021 19:06:55 +0100 Subject: [PATCH] Chore: Prevent preloaded plugins from crashing Grafana (#41490) * 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. --- public/app/app.ts | 9 ++------- public/app/features/plugins/pluginPreloader.ts | 15 +++++++++++++++ public/app/features/plugins/plugin_loader.ts | 3 +-- 3 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 public/app/features/plugins/pluginPreloader.ts diff --git a/public/app/app.ts b/public/app/app.ts index 84eb75b4ed0..c2818eded44 100644 --- a/public/app/app.ts +++ b/public/app/app.ts @@ -25,7 +25,7 @@ import { standardTransformersRegistry, } from '@grafana/data'; import { arrayMove } from 'app/core/utils/arrayMove'; -import { importPluginModule } from 'app/features/plugins/plugin_loader'; +import { preloadPlugins } from './features/plugins/pluginPreloader'; import { locationService, registerEchoBackend, @@ -125,12 +125,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 324e352bf08..c7523e25364 100644 --- a/public/app/features/plugins/plugin_loader.ts +++ b/public/app/features/plugins/plugin_loader.ts @@ -78,7 +78,6 @@ grafanaRuntime.SystemJS.config({ function exposeToPlugin(name: string, component: any) { grafanaRuntime.SystemJS.registerDynamic(name, [], true, (require: any, exports: any, module: { exports: any }) => { - console.log('registerDynamic callback', name); module.exports = component; }); } @@ -184,7 +183,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);