lisk-framework
Version:
Lisk blockchain application platform
85 lines • 2.9 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const logger_1 = require("../logger");
const endpoint_1 = require("../endpoint");
const system_dirs_1 = require("../system_dirs");
const channels_1 = require("./channels");
const modulePath = process.argv[2];
const moduleExportName = process.argv[3];
const Klass = require(modulePath)[moduleExportName];
let channel;
let plugin;
let logger;
const _loadPlugin = async (config, appConfig) => {
plugin = new Klass();
const pluginName = plugin.name;
const dirs = (0, system_dirs_1.systemDirs)(appConfig.system.dataPath);
logger = (0, logger_1.createLogger)({
logLevel: appConfig.system.logLevel,
name: `plugin_${pluginName}`,
});
channel = new channels_1.IPCChannel(logger, pluginName, plugin.events, plugin.endpoint ? (0, endpoint_1.getEndpointHandlers)(plugin.endpoint) : new Map(), Buffer.from(appConfig.genesis.chainID, 'hex'), {
socketsPath: dirs.sockets,
});
await channel.registerToBus();
logger.debug({ plugin: pluginName }, 'Plugin is registered to bus');
await plugin.init({ appConfig, config, logger });
await plugin.load();
logger.debug({ plugin: pluginName }, 'Plugin is successfully loaded');
if (process.send) {
process.send({ action: 'loaded' });
}
};
const _unloadPlugin = async (code = 0) => {
const pluginName = plugin.name;
logger.debug({ plugin: pluginName }, 'Unloading plugin');
try {
await plugin.unload();
logger.debug({ plugin: pluginName }, 'Successfully unloaded plugin');
channel.cleanup();
if (process.send) {
process.send({ action: 'unloaded' });
}
process.exit(code);
}
catch (error) {
logger.debug({ plugin: pluginName, err: error }, 'Fail to unload plugin');
channel.cleanup();
if (process.send) {
process.send({ action: 'unloadedWithError', err: error });
}
process.exit(1);
}
};
process.on('message', ({ action, config, appConfig, }) => {
const internalWorker = async () => {
if (action === 'load') {
await _loadPlugin(config, appConfig);
}
else if (action === 'unload') {
await _unloadPlugin();
}
else {
console.error(`Unknown child process plugin action: ${action}`);
}
};
internalWorker().catch((err) => {
if (logger) {
logger.error({ err }, 'Fail to handle message.');
return;
}
console.error(err);
process.exit(1);
});
});
process.on('disconnect', () => {
const internalWorker = async () => {
await _unloadPlugin(1);
};
internalWorker().catch((err) => err);
});
process.once('SIGINT', () => {
});
process.once('SIGTERM', () => {
});
//# sourceMappingURL=child_process_loader.js.map
;