first stab at refactor blockchain launcher

This commit is contained in:
Iuri Matias 2018-07-19 23:31:28 +03:00
parent f5e3f2f274
commit 3584a20fd7
6 changed files with 132 additions and 5 deletions

View File

@ -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();
});

View File

@ -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

View File

@ -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");

View File

@ -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);
}

View File

@ -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");

View File

@ -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;