first stab at refactor blockchain launcher

This commit is contained in:
Iuri Matias 2018-07-19 23:31:28 +03:00
parent 8f8852147e
commit 6ef66ac97a
3 changed files with 109 additions and 12 deletions

View File

@ -21,34 +21,87 @@ class Blockchain {
this.isWeb3Ready = false; this.isWeb3Ready = false;
this.web3StartedInProcess = false; this.web3StartedInProcess = false;
this.registerProcessLaunch();
if (!this.web3) { if (!this.web3) {
this.initWeb3(); this.initWeb3();
} else { } else {
this.isWeb3Ready = true; this.isWeb3Ready = true;
} }
this.registerServiceCheck(); //this.registerServiceCheck();
this.registerRequests(); this.registerRequests();
this.registerWeb3Object(); this.registerWeb3Object();
} }
initWeb3(cb) { registerProcessLaunch() {
if (!cb) { const self = this;
cb = function(){}; this.events.request('processes:register', 'blockchain', (cb) => {
} self.startBlockchainNode(cb);
if (this.isWeb3Ready) { });
return cb(); }
}
initWeb3() {
const self = this; const self = this;
this.web3 = new Web3(); this.web3 = new Web3();
if (this.contractsConfig.deployment.type !== "rpc" && this.contractsConfig.deployment.type !== "ws") { if (this.contractsConfig.deployment.type !== "rpc" && this.contractsConfig.deployment.type !== "ws") {
const message = __("contracts config error: unknown deployment type %s", this.contractsConfig.deployment.type); const message = __("contracts config error: unknown deployment type %s", this.contractsConfig.deployment.type);
this.logger.error(message); this.logger.error(message);
return cb(message); //return cb(message);
} }
const protocol = (this.contractsConfig.deployment.type === "rpc") ? this.contractsConfig.deployment.protocol : 'ws'; 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}`; this.web3Endpoint = utils.buildUrl(protocol, this.contractsConfig.deployment.host, this.contractsConfig.deployment.port);//`${protocol}://${this.contractsConfig.deployment.host}:${this.contractsConfig.deployment.port}`;
const providerOptions = { const providerOptions = {
@ -90,10 +143,11 @@ class Blockchain {
return next(); return next();
} }
self.web3StartedInProcess = true; 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 // Need to re-initialize web3 to connect to the new blockchain node
self.provider.stop(); self.provider.stop();
self.initWeb3(cb); //self.initWeb3(cb);
}); });
}); });
}, },
@ -120,6 +174,7 @@ class Blockchain {
} }
startBlockchainNode(callback) { startBlockchainNode(callback) {
console.dir("==> startBlockchainNode");
const self = this; const self = this;
let blockchainProcess = new BlockchainProcessLauncher({ let blockchainProcess = new BlockchainProcessLauncher({
events: self.events, events: self.events,
@ -132,9 +187,11 @@ class Blockchain {
blockchainProcess.startBlockchainNode(); blockchainProcess.startBlockchainNode();
self.events.once(constants.blockchain.blockchainReady, () => { self.events.once(constants.blockchain.blockchainReady, () => {
console.dir("==> blockchainReady");
callback(); callback();
}); });
self.events.once(constants.blockchain.blockchainExit, () => { self.events.once(constants.blockchain.blockchainExit, () => {
console.dir("==> blockchainExit");
self.provider.stop(); self.provider.stop();
callback(); callback();
}); });

View File

@ -1,6 +1,7 @@
let async = require('async'); let async = require('async');
const utils = require('../utils/utils.js'); const utils = require('../utils/utils.js');
//require("../utils/debug_util.js")(__filename, async);
const ContractDeployer = require('./contract_deployer.js'); const ContractDeployer = require('./contract_deployer.js');
const cloneDeep = require('clone-deep'); const cloneDeep = require('clone-deep');

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;