Skip to content

Commit

Permalink
Simplifications.
Browse files Browse the repository at this point in the history
  • Loading branch information
azasypkin committed Jul 1, 2021
1 parent d13a75c commit c1ac601
Show file tree
Hide file tree
Showing 7 changed files with 497 additions and 353 deletions.
6 changes: 3 additions & 3 deletions src/core/server/plugins/discovery/plugin_manifest_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { coerce } from 'semver';
import { promisify } from 'util';
import { snakeCase } from 'lodash';
import { isConfigPath, PackageInfo } from '../../config';
import { PluginManifest } from '../types';
import { PluginManifest, PluginType } from '../types';
import { PluginDiscoveryError } from './plugin_discovery_error';
import { isCamelCase } from './is_camel_case';

Expand Down Expand Up @@ -179,8 +179,8 @@ export async function parseManifest(
);
}

const type = manifest.type ?? 'standard';
if (type !== 'preboot' && type !== 'standard') {
const type = manifest.type ?? PluginType.standard;
if (type !== PluginType.preboot && type !== PluginType.standard) {
throw PluginDiscoveryError.invalidManifest(
manifestPath,
new Error(
Expand Down
65 changes: 10 additions & 55 deletions src/core/server/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import { isConfigSchema } from '@kbn/config-schema';

import { Logger } from '../logging';
import {
Plugin,
AsyncPlugin,
Plugin,
PluginConfigDescriptor,
PluginInitializer,
PluginInitializerContext,
PluginManifest,
PluginInitializer,
PluginOpaqueId,
PluginConfigDescriptor,
PluginType,
PrebootPlugin,
} from './types';
import { CorePreboot, CoreSetup, CoreStart } from '..';
Expand All @@ -39,25 +40,6 @@ export interface CommonPluginWrapper {
readonly includesUiPlugin: PluginManifest['ui'];
}

export interface PrebootPluginWrapper<TSetup = unknown, TPluginsSetup extends object = object>
extends CommonPluginWrapper {
setup(setupContext: CorePreboot, plugins: TPluginsSetup): TSetup;
stop(): Promise<void>;
}

export interface StandardPluginWrapper<
TSetup = unknown,
TStart = unknown,
TPluginsSetup extends object = object,
TPluginsStart extends object = object
> extends CommonPluginWrapper {
readonly startDependencies: Promise<[CoreStart, TPluginsStart, TStart]>;

setup(setupContext: CoreSetup<TPluginsStart>, plugins: TPluginsSetup): TSetup | Promise<TSetup>;
start(startContext: CoreStart, plugins: TPluginsStart): TStart | Promise<TStart>;
stop(): Promise<void>;
}

/**
* Lightweight wrapper around discovered plugin that is responsible for instantiating
* plugin and dispatching proper context and dependencies into plugin's lifecycle hooks.
Expand Down Expand Up @@ -127,11 +109,11 @@ export class PluginWrapper<
): TSetup | Promise<TSetup> {
this.instance = this.createPluginInstance();

if (this.isStandardPluginInstance(this.instance)) {
return this.instance.setup(setupContext as CoreSetup, plugins);
if (this.isPrebootPluginInstance(this.instance)) {
return this.instance.setup(setupContext as CorePreboot, plugins);
}

return this.instance.setup(setupContext as CorePreboot, plugins);
return this.instance.setup(setupContext as CoreSetup, plugins);
}

/**
Expand All @@ -146,8 +128,8 @@ export class PluginWrapper<
throw new Error(`Plugin "${this.name}" can't be started since it isn't set up.`);
}

if (!this.isStandardPluginInstance(this.instance)) {
throw new Error(`Plugin "${this.name}" is not a standard plugin and cannot be started.`);
if (this.isPrebootPluginInstance(this.instance)) {
throw new Error(`Plugin "${this.name}" is a preboot plugin and cannot be started.`);
}

const startContract = this.instance.start(startContext, plugins);
Expand Down Expand Up @@ -197,25 +179,6 @@ export class PluginWrapper<
return configDescriptor;
}

/**
* Indicates whether plugin is a standard plugin (manifest `type` property is equal to `standard`).
*/
public isStandardPlugin(): this is StandardPluginWrapper<
TSetup,
TStart,
TPluginsSetup,
TPluginsStart
> {
return this.isStandardPluginInstance(this.instance);
}

/**
* Indicates whether plugin is a preboot plugin (manifest `type` property is equal to `preboot`).
*/
public isPrebootPlugin(): this is PrebootPluginWrapper<TSetup, TPluginsSetup> {
return this.isPrebootPluginInstance(this.instance);
}

private createPluginInstance() {
this.log.debug('Initializing plugin');

Expand Down Expand Up @@ -248,17 +211,9 @@ export class PluginWrapper<
return instance;
}

private isStandardPluginInstance(
instance: PluginWrapper['instance']
): instance is
| Plugin<TSetup, TStart, TPluginsSetup, TPluginsStart>
| AsyncPlugin<TSetup, TStart, TPluginsSetup, TPluginsStart> {
return this.manifest.type === 'standard';
}

private isPrebootPluginInstance(
instance: PluginWrapper['instance']
): instance is PrebootPlugin<TSetup, TPluginsSetup> {
return this.manifest.type === 'preboot';
return this.manifest.type === PluginType.preboot;
}
}
6 changes: 3 additions & 3 deletions src/core/server/plugins/plugin_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { shareReplay } from 'rxjs/operators';
import type { RequestHandlerContext } from 'src/core/server';
import { CoreContext } from '../core_context';
import { PluginWrapper, PrebootPluginWrapper, StandardPluginWrapper } from './plugin';
import { PluginWrapper } from './plugin';
import {
PluginsServicePrebootSetupDeps,
PluginsServiceSetupDeps,
Expand Down Expand Up @@ -94,7 +94,7 @@ export function createPluginInitializerContext(
export function createPluginPrebootSetupContext(
coreContext: CoreContext,
deps: PluginsServicePrebootSetupDeps,
plugin: PrebootPluginWrapper
plugin: PluginWrapper
): CorePreboot {
return {
elasticsearch: {
Expand Down Expand Up @@ -128,7 +128,7 @@ export function createPluginPrebootSetupContext(
export function createPluginSetupContext<TPlugin, TPluginDependencies>(
coreContext: CoreContext,
deps: PluginsServiceSetupDeps,
plugin: StandardPluginWrapper<TPlugin, TPluginDependencies>
plugin: PluginWrapper<TPlugin, TPluginDependencies>
): CoreSetup {
const router = deps.http.createRouter('', plugin.opaqueId);

Expand Down
43 changes: 22 additions & 21 deletions src/core/server/plugins/plugins_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import Path from 'path';
import { Observable } from 'rxjs';
import { filter, first, map, concatMap, tap, toArray } from 'rxjs/operators';
import { pick, getFlattenedObject } from '@kbn/std';
import { concatMap, filter, first, map, tap, toArray } from 'rxjs/operators';
import { getFlattenedObject, pick } from '@kbn/std';

import { CoreService } from '../../types';
import { CoreContext } from '../core_context';
Expand All @@ -18,10 +18,10 @@ import { discover, PluginDiscoveryError, PluginDiscoveryErrorType } from './disc
import { PluginWrapper } from './plugin';
import {
DiscoveredPlugin,
PluginConfigDescriptor,
PluginName,
InternalPluginInfo,
PluginConfigDescriptor,
PluginDependencies,
PluginName,
PluginType,
} from './types';
import { PluginsConfig, PluginsConfigType } from './plugins_config';
Expand Down Expand Up @@ -115,26 +115,27 @@ export class PluginsService implements CoreService<PluginsServiceSetup, PluginsS
await this.handleDiscoveryErrors(error$);
await this.handleDiscoveredPlugins(plugin$);

const uiPlugins = this.pluginsSystem.uiPlugins();
const plugins = this.pluginsSystem.getPlugins();
const pluginTree = this.pluginsSystem.getPluginDependencies();
const prebootUIPlugins = this.pluginsSystem.uiPlugins(PluginType.preboot);
const standardUIPlugins = this.pluginsSystem.uiPlugins(PluginType.standard);
return {
preboot: {
pluginTree: pluginTree.preboot,
pluginPaths: plugins.preboot.map((plugin) => plugin.path),
pluginTree: this.pluginsSystem.getPluginDependencies(PluginType.preboot),
pluginPaths: this.pluginsSystem.getPlugins(PluginType.preboot).map((plugin) => plugin.path),
uiPlugins: {
internal: this.uiPluginInternalInfo,
public: uiPlugins.preboot,
browserConfigs: this.generateUiPluginsConfigs(uiPlugins.preboot),
public: prebootUIPlugins,
browserConfigs: this.generateUiPluginsConfigs(prebootUIPlugins),
},
},
standard: {
pluginTree: pluginTree.standard,
pluginPaths: plugins.standard.map((plugin) => plugin.path),
pluginTree: this.pluginsSystem.getPluginDependencies(PluginType.standard),
pluginPaths: this.pluginsSystem
.getPlugins(PluginType.standard)
.map((plugin) => plugin.path),
uiPlugins: {
internal: this.uiPluginInternalInfo,
public: uiPlugins.standard,
browserConfigs: this.generateUiPluginsConfigs(uiPlugins.standard),
public: standardUIPlugins,
browserConfigs: this.generateUiPluginsConfigs(standardUIPlugins),
},
},
};
Expand All @@ -153,7 +154,7 @@ export class PluginsService implements CoreService<PluginsServiceSetup, PluginsS
return;
}

await this.pluginsSystem.setupPrebootPlugins(deps);
await this.pluginsSystem.setupPlugins(PluginType.preboot, deps);
this.registerPluginStaticDirs(deps);
}

Expand All @@ -164,7 +165,7 @@ export class PluginsService implements CoreService<PluginsServiceSetup, PluginsS

let contracts = new Map<PluginName, unknown>();
if (config.initialize) {
contracts = await this.pluginsSystem.setupStandardPlugins(deps);
contracts = await this.pluginsSystem.setupPlugins(PluginType.standard, deps);
this.registerPluginStaticDirs(deps);
} else {
this.log.info('Plugin initialization disabled.');
Expand All @@ -178,15 +179,15 @@ export class PluginsService implements CoreService<PluginsServiceSetup, PluginsS

public async start(deps: PluginsServiceStartDeps) {
this.log.debug('Plugins service starts plugins');
const contracts = await this.pluginsSystem.startStandardPlugins(deps);
await this.pluginsSystem.stopPrebootPlugins();
const contracts = await this.pluginsSystem.startPlugins(PluginType.standard, deps);
await this.pluginsSystem.stopPlugins(PluginType.preboot);
return { contracts };
}

public async stop() {
this.log.debug('Stopping plugins service');
await this.pluginsSystem.stopPrebootPlugins();
await this.pluginsSystem.stopStandardPlugins();
await this.pluginsSystem.stopPlugins(PluginType.preboot);
await this.pluginsSystem.stopPlugins(PluginType.standard);
}

private generateUiPluginsConfigs(
Expand Down
Loading

0 comments on commit c1ac601

Please sign in to comment.