From c92f6f88650dc153f94a6d4aaaafa546091da343 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Wed, 5 Sep 2018 10:39:09 +0100 Subject: [PATCH] Fix dynamic IPC --- cmd/cmd_controller.js | 48 +++++++++++++++++++---------- lib/core/engine.js | 15 ++------- lib/core/ipc.js | 2 +- lib/tests/test.js | 72 +++++++++++++++++++++++-------------------- 4 files changed, 74 insertions(+), 63 deletions(-) diff --git a/cmd/cmd_controller.js b/cmd/cmd_controller.js index 6aeb514f..ba337313 100644 --- a/cmd/cmd_controller.js +++ b/cmd/cmd_controller.js @@ -79,18 +79,20 @@ class EmbarkController { context: self.context, useDashboard: options.useDashboard, webServerConfig: webServerConfig, - webpackConfigName: options.webpackConfigName, - ipcRole: 'server' + webpackConfigName: options.webpackConfigName }); - engine.init(); - if (!options.useDashboard) { - engine.logger.info('========================'.bold.green); - engine.logger.info((__('Welcome to Embark') + ' ' + this.version).yellow.bold); - engine.logger.info('========================'.bold.green); - } - - async.parallel([ + async.waterfall([ + function initEngine(callback) { + engine.init({}, () => { + if (!options.useDashboard) { + engine.logger.info('========================'.bold.green); + engine.logger.info((__('Welcome to Embark') + ' ' + engine.version).yellow.bold); + engine.logger.info('========================'.bold.green); + } + callback(); + }); + }, function startDashboard(callback) { if (!options.useDashboard) { return callback(); @@ -182,9 +184,12 @@ class EmbarkController { context: this.context, webpackConfigName: options.webpackConfigName }); - engine.init(); + async.waterfall([ + function initEngine(callback) { + engine.init({}, callback); + }, function startServices(callback) { let pluginList = engine.plugins.listPlugins(); if (pluginList.length > 0) { @@ -250,8 +255,11 @@ class EmbarkController { context: this.context, webpackConfigName: options.webpackConfigName }); - engine.init(); + async.waterfall([ + function initEngine(callback) { + engine.init({}, callback); + }, function startServices(callback) { let pluginList = engine.plugins.listPlugins(); if (pluginList.length > 0) { @@ -350,9 +358,12 @@ class EmbarkController { logFile: options.logFile, context: this.context }); - engine.init(); + async.waterfall([ + function (callback) { + engine.init({}, callback); + }, function (callback) { let pluginList = engine.plugins.listPlugins(); if (pluginList.length > 0) { @@ -436,12 +447,17 @@ class EmbarkController { context: this.context, webpackConfigName: options.webpackConfigName }); - engine.init(); - let platform = engine.config.storageConfig.upload.provider; + + let platform; async.waterfall([ - + function initEngine(callback) { + engine.init({}, () => { + platform = engine.config.storageConfig.upload.provider; + callback(); + }); + }, function startServices(callback) { engine.startService("processManager"); diff --git a/lib/core/engine.js b/lib/core/engine.js index 822ddf6d..7554d43f 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -17,11 +17,11 @@ class Engine { this.context = options.context; this.useDashboard = options.useDashboard; this.webServerConfig = options.webServerConfig; - this.ipcRole = options.ipcRole; this.webpackConfigName = options.webpackConfigName; } - init(_options) { + init(_options, callback) { + callback = callback || function() {}; const Events = require('./events.js'); const Logger = require('./logger.js'); const Config = require('./config.js'); @@ -38,22 +38,13 @@ class Engine { utils.interceptLogs(console, this.logger); } - if (this.ipcRole) { - this.ipc = new IPC({logger: this.logger, ipcRole: this.ipcRole}); - - if(this.ipc.isServer()) { - this.ipc.serve(); - } - - return; - } - this.ipc = new IPC({logger: this.logger, ipcRole: 'client'}); this.ipc.connect((err) => { if(err) { this.ipc = new IPC({logger: this.logger, ipcRole: 'server'}); this.ipc.serve(); } + callback(); }); } diff --git a/lib/core/ipc.js b/lib/core/ipc.js index 49b0c6a3..a046817c 100644 --- a/lib/core/ipc.js +++ b/lib/core/ipc.js @@ -6,7 +6,7 @@ class IPC { constructor(options) { this.logger = options.logger; this.socketPath = options.socketPath || fs.dappPath(".embark/embark.ipc"); - this.ipcRole = options.ipcRole || "server"; + this.ipcRole = options.ipcRole; ipc.config.silent = true; this.connected = false; } diff --git a/lib/tests/test.js b/lib/tests/test.js index c9d621a3..8c61b15a 100644 --- a/lib/tests/test.js +++ b/lib/tests/test.js @@ -116,48 +116,52 @@ class Test { } init(callback) { + let self = this; this.engine = new Engine({ env: this.options.env || 'test', // TODO: config will need to detect if this is a obj embarkConfig: this.options.embarkConfig || 'embark.json', - interceptLogs: false, - ipcRole: 'client' + interceptLogs: false }); + async.waterfall([ + function initEngine(cb) { + self.engine.init({ + logger: new TestLogger({logLevel: self.options.loglevel}) + }, cb); + }, + function startServices(cb) { + self.versions_default = self.engine.config.contractsConfig.versions; + // Reset contract config to nothing to make sure we deploy only what we want + self.engine.config.contractsConfig = { + contracts: {}, + versions: self.versions_default + }; - this.engine.init({ - logger: new TestLogger({logLevel: this.options.loglevel}) - }); + self.engine.startService("libraryManager"); + self.engine.startService("codeRunner"); + self.initDeployServices(); + self.engine.startService("codeGenerator"); + self.engine.startService("codeCoverage"); - this.versions_default = this.engine.config.contractsConfig.versions; - // Reset contract config to nothing to make sure we deploy only what we want - this.engine.config.contractsConfig = { - contracts: {}, - versions: this.versions_default - }; - - this.engine.startService("libraryManager"); - this.engine.startService("codeRunner"); - this.initDeployServices(); - this.engine.startService("codeGenerator"); - this.engine.startService("codeCoverage"); - - if (this.options.node === 'embark') { - return this.engine.ipc.connect((err) => { - if (err) { - this.engine.logger.error(err.message || err); - this.engine.logger.error("Could not connect to Embark's IPC. Is embark running?"); - process.exit(1); + if (self.options.node === 'embark') { + return self.engine.ipc.connect((err) => { + if (err) { + this.engine.logger.error(err.message || err); + this.engine.logger.error("Could not connect to Embark's IPC. Is embark running?"); + process.exit(1); + } + self.engine.ipc.request('blockchain:node', {}, (err, node) => { + if (err) { + return self.engine.logger.error(err.message || err); + } + self.options.node = node; + cb(); + }); + }); } - this.engine.ipc.request('blockchain:node', {}, (err, node) => { - if (err) { - return this.engine.logger.error(err.message || err); - } - this.options.node = node; - callback(); - }); - }); - } - callback(); + cb(); + } + ], callback); } onReady(callback) {