first stab at refactor blockchain launcher

This commit is contained in:
Iuri Matias 2018-07-19 23:31:28 +03:00
parent 2192347631
commit 80c617cc60
6 changed files with 132 additions and 17 deletions

View File

@ -21,34 +21,87 @@ 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();
}
initWeb3(cb) {
if (!cb) {
cb = function(){};
}
if (this.isWeb3Ready) {
return cb();
}
registerProcessLaunch() {
const self = this;
this.events.request('processes:register', 'blockchain', (cb) => {
self.startBlockchainNode(cb);
});
}
initWeb3() {
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 = {
@ -90,10 +143,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);
});
});
},
@ -120,6 +174,7 @@ class Blockchain {
}
startBlockchainNode(callback) {
console.dir("==> startBlockchainNode");
const self = this;
let blockchainProcess = new BlockchainProcessLauncher({
events: self.events,
@ -132,9 +187,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) {
@ -87,11 +88,12 @@ class DeployManager {
// TODO: could be implemented as an event (beforeDeployAll)
function checkIsConnectedToBlockchain(callback) {
self.blockchain.onReady(() => {
self.blockchain.assertNodeConnection((err) => {
callback(err);
});
});
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

@ -121,6 +121,7 @@ class Embark {
engine.logger.info(__("loaded plugins") + ": " + pluginList.join(", "));
}
engine.startService("processManager");
engine.startService("serviceMonitor");
engine.startService("libraryManager");
engine.startService("codeRunner");
@ -195,6 +196,7 @@ class Embark {
engine.logger.info(__("loaded plugins") + ": " + pluginList.join(", "));
}
engine.startService("processManager");
engine.startService("libraryManager");
engine.startService("codeRunner");
engine.startService("web3");
@ -323,6 +325,7 @@ class Embark {
engine.logger.info(__("loaded plugins") + ": " + pluginList.join(", "));
}
engine.startService("processManager");
engine.startService("serviceMonitor");
engine.startService("libraryManager");
engine.startService("pipeline");
@ -382,6 +385,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;