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.
This commit is contained in:
Marcus Andersson 2021-11-10 19:06:55 +01:00 committed by GitHub
parent e0a60cf459
commit e926126d63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 9 deletions

View File

@ -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<Promise<any>> = [];
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, {

View File

@ -0,0 +1,15 @@
import { PreloadPlugin } from '@grafana/data';
import { importPluginModule } from './plugin_loader';
export async function preloadPlugins(pluginsToPreload: PreloadPlugin[] = []): Promise<void> {
await Promise.all(pluginsToPreload.map(preloadPlugin));
}
async function preloadPlugin(plugin: PreloadPlugin): Promise<void> {
const { path, version } = plugin;
try {
await importPluginModule(path, version);
} catch (error: unknown) {
console.error(`Failed to load plugin: ${path} (version: ${version})`, error);
}
}

View File

@ -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);