From 3584a20fd7837a8776bdb1c2c33b992b28039603 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 19 Jul 2018 23:31:28 +0300 Subject: [PATCH] first stab at refactor blockchain launcher --- lib/contracts/blockchain.js | 75 +++++++++++++++++++++++++++++--- lib/contracts/deploy_manager.js | 6 +++ lib/core/engine.js | 10 +++++ lib/core/events.js | 3 ++ lib/index.js | 4 ++ lib/processes/process_manager.js | 39 +++++++++++++++++ 6 files changed, 132 insertions(+), 5 deletions(-) create mode 100644 lib/processes/process_manager.js diff --git a/lib/contracts/blockchain.js b/lib/contracts/blockchain.js index c3803ba45..96477db62 100644 --- a/lib/contracts/blockchain.js +++ b/lib/contracts/blockchain.js @@ -21,16 +21,26 @@ class Blockchain { this.isWeb3Ready = false; this.web3StartedInProcess = false; + this.registerProcessLaunch(); + if (!this.web3) { this.initWeb3(); } else { this.isWeb3Ready = true; } - this.registerServiceCheck(); + //this.registerServiceCheck(); this.registerRequests(); this.registerWeb3Object(); } + registerProcessLaunch() { + const self = this; + this.events.request('processes:register', 'blockchain', (cb) => { + self.startBlockchainNode(cb); + }); + } + + //initWeb3() { initWeb3(cb) { if (!cb) { cb = function(){}; @@ -39,17 +49,68 @@ class Blockchain { this.events.emit(WEB3_READY); return cb(); } + const self = this; this.web3 = new Web3(); if (this.contractsConfig.deployment.type !== "rpc" && this.contractsConfig.deployment.type !== "ws") { const message = __("contracts config error: unknown deployment type %s", this.contractsConfig.deployment.type); this.logger.error(message); - return cb(message); + //return cb(message); } const protocol = (this.contractsConfig.deployment.type === "rpc") ? this.contractsConfig.deployment.protocol : 'ws'; - + + this.web3Endpoint = utils.buildUrl(protocol, this.contractsConfig.deployment.host, this.contractsConfig.deployment.port);//`${protocol}://${this.contractsConfig.deployment.host}:${this.contractsConfig.deployment.port}`; + + const providerOptions = { + web3: this.web3, + accountsConfig: this.contractsConfig.deployment.accounts, + blockchainConfig: this.blockchainConfig, + logger: this.logger, + isDev: this.isDev, + type: this.contractsConfig.deployment.type, + web3Endpoint: self.web3Endpoint + }; + this.provider = new Provider(providerOptions); + + self.assertNodeConnection(true, (err) => { + if (!err) { + self.provider.startWeb3Provider(() => { + self.provider.fundAccounts(() => { + self.registerWeb3Object(); + + self.events.emit("check:backOnline:Ethereum"); + }); + }); + return; + } + + self.events.request("processes:launch", "blockchain", () => { + console.dir("======> blockchain launched!"); + self.provider.startWeb3Provider(() => { + self.provider.fundAccounts(() => { + self.registerWeb3Object(); + + self.events.emit("check:backOnline:Ethereum"); + }); + }); + }); + }); + } + + initWeb3_old() { + const self = this; + this.web3 = new Web3(); + + if (this.contractsConfig.deployment.type !== "rpc" && this.contractsConfig.deployment.type !== "ws") { + const message = __("contracts config error: unknown deployment type %s", this.contractsConfig.deployment.type); + this.logger.error(message); + //return cb(message); + } + + const protocol = (this.contractsConfig.deployment.type === "rpc") ? this.contractsConfig.deployment.protocol : 'ws'; + this.web3Endpoint = utils.buildUrl(protocol, this.contractsConfig.deployment.host, this.contractsConfig.deployment.port);//`${protocol}://${this.contractsConfig.deployment.host}:${this.contractsConfig.deployment.port}`; const providerOptions = { web3: this.web3, @@ -90,10 +151,11 @@ class Blockchain { return next(); } self.web3StartedInProcess = true; - self.startBlockchainNode(() => { + //self.startBlockchainNode(() => { + self.events.request("processes:launch", "blockchain", () => { // Need to re-initialize web3 to connect to the new blockchain node self.provider.stop(); - self.initWeb3(cb); + //self.initWeb3(cb); }); }); }, @@ -121,6 +183,7 @@ class Blockchain { } startBlockchainNode(callback) { + console.dir("==> startBlockchainNode"); const self = this; let blockchainProcess = new BlockchainProcessLauncher({ events: self.events, @@ -133,9 +196,11 @@ class Blockchain { blockchainProcess.startBlockchainNode(); self.events.once(constants.blockchain.blockchainReady, () => { + console.dir("==> blockchainReady"); callback(); }); self.events.once(constants.blockchain.blockchainExit, () => { + console.dir("==> blockchainExit"); self.provider.stop(); callback(); }); diff --git a/lib/contracts/deploy_manager.js b/lib/contracts/deploy_manager.js index 7290ad591..fbdb48cd1 100644 --- a/lib/contracts/deploy_manager.js +++ b/lib/contracts/deploy_manager.js @@ -1,6 +1,7 @@ let async = require('async'); const utils = require('../utils/utils.js'); +//require("../utils/debug_util.js")(__filename, async); class DeployManager { constructor(options) { @@ -90,6 +91,11 @@ class DeployManager { self.blockchain.onReady(() => { self.blockchain.assertNodeConnection(callback); }); + //self.blockchain.onReady(() => { + // self.blockchain.assertNodeConnection((err) => { + // callback(err); + // }); + //}); }, // TODO: this can be done on the fly or as part of the initialization diff --git a/lib/core/engine.js b/lib/core/engine.js index 71b8ec2ed..7b55bafca 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -81,6 +81,7 @@ class Engine { "namingSystem": this.namingSystem, "web3": this.web3Service, "libraryManager": this.libraryManagerService, + "processManager": this.processManagerService, "storage": this.storageService }; @@ -95,6 +96,15 @@ class Engine { return service.apply(this, [options]); } + processManagerService(_options) { + const ProcessManager = require('../processes/process_manager.js'); + const processManager = new ProcessManager({ + events: this.events, + logger: this.logger, + plugins: this.plugins + }); + } + pipelineService(_options) { const self = this; this.events.emit("status", "Building Assets"); diff --git a/lib/core/events.js b/lib/core/events.js index 6bc9bcd1f..cd2218fdd 100644 --- a/lib/core/events.js +++ b/lib/core/events.js @@ -11,6 +11,9 @@ function log(eventType, eventName) { if (['end', 'prefinish', 'error', 'new', 'demo', 'block', 'version'].indexOf(eventName) >= 0) { return; } + if (eventType.indexOf("log") >= 0) { + return; + } //console.log(eventType, eventName); } diff --git a/lib/index.js b/lib/index.js index 4eb31f114..8945a2930 100644 --- a/lib/index.js +++ b/lib/index.js @@ -117,6 +117,7 @@ class Embark { engine.logger.info(__("loaded plugins") + ": " + pluginList.join(", ")); } + engine.startService("processManager"); engine.startService("serviceMonitor"); engine.startService("libraryManager"); engine.startService("codeRunner"); @@ -192,6 +193,7 @@ class Embark { engine.logger.info(__("loaded plugins") + ": " + pluginList.join(", ")); } + engine.startService("processManager"); engine.startService("libraryManager"); engine.startService("codeRunner"); engine.startService("web3"); @@ -320,6 +322,7 @@ class Embark { engine.logger.info(__("loaded plugins") + ": " + pluginList.join(", ")); } + engine.startService("processManager"); engine.startService("serviceMonitor"); engine.startService("libraryManager"); engine.startService("pipeline"); @@ -379,6 +382,7 @@ class Embark { function startServices(callback) { + engine.startService("processManager"); engine.startService("serviceMonitor"); engine.startService("libraryManager"); engine.startService("codeRunner"); diff --git a/lib/processes/process_manager.js b/lib/processes/process_manager.js new file mode 100644 index 000000000..4da9ddeab --- /dev/null +++ b/lib/processes/process_manager.js @@ -0,0 +1,39 @@ + +class ProcessManager { + constructor(options) { + const self = this; + this.logger = options.logger; + this.events = options.events; + this.plugins = options.plugins; + this.processes = {}; + + self.events.setCommandHandler('processes:register', (name, cb) => { + console.dir("=====> registering " + name); + this.processes[name] = { + state: 'unstarted', + cb: cb + } + }); + + self.events.setCommandHandler('processes:launch', (name, cb) => { + let process = self.processes[name]; + // TODO: should make distinction between starting and running + if (process.state != 'unstarted') { + console.dir("=====> already started " + name); + return cb(); + } + console.dir("=====> launching " + name); + process.state = 'starting'; + //let pry = require('pryjs'); + //eval(pry.it); + process.cb.apply(process.cb, [() => { + process.state = 'running'; + console.dir("=====> launched " + name); + cb(); + }]); + }); + } + +} + +module.exports = ProcessManager;