Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core-container): handle failing optional plugins gracefully #2657

Merged
merged 5 commits into from Jun 5, 2019
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Next

fix(core-container): handle failing optional plugins gracefully

  • Loading branch information...
supaiku0 committed Jun 4, 2019
commit 754606e94db9aea12acf1c59500f94ed3fe7a7b0
@@ -1,20 +1,22 @@
import { Container } from "@arkecosystem/core-interfaces";
import { Container, Logger } from "@arkecosystem/core-interfaces";
import Hoek from "@hapi/hoek";
import { asValue } from "awilix";
import isString from "lodash.isstring";
import semver from "semver";

export class PluginRegistrar {
private container: any;
private container: Container.IContainer;
private plugins: any;
private options: any;
private deregister: any;
private failedPlugins: Record<string, Error>;

constructor(container: Container.IContainer, options: Record<string, any> = {}) {
this.container = container;
this.plugins = container.config.get("plugins");
this.options = this.castOptions(options);
this.deregister = [];
this.failedPlugins = {};
}

/**
@@ -29,6 +31,16 @@ export class PluginRegistrar {
break;
}
}

const failedPlugins: number = Object.keys(this.failedPlugins).length;
if (failedPlugins > 0) {
const logger = this.container.resolvePlugin<Logger.ILogger>("logger");
logger.warn(`Failed to load ${failedPlugins} optional plugins.`);

for (const [name, error] of Object.entries(this.failedPlugins)) {
logger.warn(`Plugin '${name}': ${error.message}`);
}
}
}

/**
@@ -47,16 +59,20 @@ export class PluginRegistrar {
* @param {Object} options
* @return {void}
*/
public async register(name, options = {}) {
if (!this.shouldBeRegistered(name)) {
return;
}
private async register(name, options = {}) {
try {
if (!this.shouldBeRegistered(name)) {
return;
}

if (this.plugins[name]) {
options = Hoek.applyToDefaults(this.plugins[name], options);
}
if (this.plugins[name]) {
options = Hoek.applyToDefaults(this.plugins[name], options);
}

return this.registerWithContainer(name, options);
return this.registerWithContainer(name, options);
} catch (error) {
this.failedPlugins[name] = error;
}
}

/**
@@ -66,7 +82,13 @@ export class PluginRegistrar {
* @return {void}
*/
private async registerWithContainer(plugin, options = {}) {
const item: any = this.resolve(plugin);
let item: any;
try {
item = this.resolve(plugin);
} catch (error) {
this.failedPlugins[plugin] = error;
return;
}

if (!item.plugin.register) {
return;
@@ -91,18 +113,26 @@ export class PluginRegistrar {
options = this.applyToDefaults(name, defaults, options);
this.container.register(`pkg.${alias || name}.opts`, asValue(options));

plugin = await item.plugin.register(this.container, options);
this.container.register(
alias || name,
asValue({
name,
version,
plugin,
}),
);

if (item.plugin.deregister) {
this.deregister.push({ plugin: item.plugin, options });
try {
plugin = await item.plugin.register(this.container, options);
this.container.register(
alias || name,
asValue({
name,
version,
plugin,
}),
);

if (item.plugin.deregister) {
this.deregister.push({ plugin: item.plugin, options });
}
} catch (error) {
if (item.plugin.required) {
this.container.forceExit(`Failed to register required plugin '${name}'`, error);
} else {
this.failedPlugins[name] = error;
}
}
}

@@ -27,6 +27,8 @@ export interface IContainer {

silentShutdown: boolean;

shuttingDown: boolean;

isReady: boolean;

setUp(version: string, variables: any, options?: any): Promise<void>;
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.