From 38cb642e980183ab41cbc52088519a4728e137dc Mon Sep 17 00:00:00 2001 From: Josh Dover Date: Mon, 15 Jun 2020 13:31:02 -0600 Subject: [PATCH] Fix plugin lifecycle log to only include server plugins (#68686) Co-authored-by: Elastic Machine --- .../server/plugins/plugins_system.test.ts | 32 +++++++++++++++++++ src/core/server/plugins/plugins_system.ts | 17 +++++----- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/core/server/plugins/plugins_system.test.ts b/src/core/server/plugins/plugins_system.test.ts index 8b318ad1b735e..70983e4fd087b 100644 --- a/src/core/server/plugins/plugins_system.test.ts +++ b/src/core/server/plugins/plugins_system.test.ts @@ -34,6 +34,7 @@ import { PluginWrapper } from './plugin'; import { PluginName } from './types'; import { PluginsSystem } from './plugins_system'; import { coreMock } from '../mocks'; +import { Logger } from '../logging'; const logger = loggingServiceMock.create(); function createPlugin( @@ -435,6 +436,21 @@ describe('setup', () => { `[Error: Setup lifecycle of "timeout-setup" plugin wasn't completed in 30sec. Consider disabling the plugin and re-start.]` ); }); + + it('logs only server-side plugins', async () => { + [ + createPlugin('order-0'), + createPlugin('order-not-run', { server: false }), + createPlugin('order-1'), + ].forEach((plugin, index) => { + jest.spyOn(plugin, 'setup').mockResolvedValue(`setup-as-${index}`); + jest.spyOn(plugin, 'start').mockResolvedValue(`started-as-${index}`); + pluginsSystem.addPlugin(plugin); + }); + await pluginsSystem.setupPlugins(setupDeps); + const log = logger.get.mock.results[0].value as jest.Mocked; + expect(log.info).toHaveBeenCalledWith(`Setting up [2] plugins: [order-1,order-0]`); + }); }); describe('start', () => { @@ -461,4 +477,20 @@ describe('start', () => { `[Error: Start lifecycle of "timeout-start" plugin wasn't completed in 30sec. Consider disabling the plugin and re-start.]` ); }); + + it('logs only server-side plugins', async () => { + [ + createPlugin('order-0'), + createPlugin('order-not-run', { server: false }), + createPlugin('order-1'), + ].forEach((plugin, index) => { + jest.spyOn(plugin, 'setup').mockResolvedValue(`setup-as-${index}`); + jest.spyOn(plugin, 'start').mockResolvedValue(`started-as-${index}`); + pluginsSystem.addPlugin(plugin); + }); + await pluginsSystem.setupPlugins(setupDeps); + await pluginsSystem.startPlugins(startDeps); + const log = logger.get.mock.results[0].value as jest.Mocked; + expect(log.info).toHaveBeenCalledWith(`Starting [2] plugins: [order-1,order-0]`); + }); }); diff --git a/src/core/server/plugins/plugins_system.ts b/src/core/server/plugins/plugins_system.ts index e0401006ffac9..1bf1f4b189a67 100644 --- a/src/core/server/plugins/plugins_system.ts +++ b/src/core/server/plugins/plugins_system.ts @@ -66,15 +66,16 @@ export class PluginsSystem { return contracts; } - const sortedPlugins = this.getTopologicallySortedPluginNames(); - this.log.info(`Setting up [${this.plugins.size}] plugins: [${[...sortedPlugins]}]`); - - for (const pluginName of sortedPlugins) { - const plugin = this.plugins.get(pluginName)!; - if (!plugin.includesServerPlugin) { - continue; - } + const sortedPlugins = new Map( + [...this.getTopologicallySortedPluginNames()] + .map((pluginName) => [pluginName, this.plugins.get(pluginName)!] as [string, PluginWrapper]) + .filter(([pluginName, plugin]) => plugin.includesServerPlugin) + ); + this.log.info( + `Setting up [${sortedPlugins.size}] plugins: [${[...sortedPlugins.keys()].join(',')}]` + ); + for (const [pluginName, plugin] of sortedPlugins) { this.log.debug(`Setting up plugin "${pluginName}"...`); const pluginDeps = new Set([...plugin.requiredPlugins, ...plugin.optionalPlugins]); const pluginDepContracts = Array.from(pluginDeps).reduce((depContracts, dependencyName) => {