fix/add geth check

This commit is contained in:
Iuri Matias 2019-07-23 14:55:16 -04:00
parent f09d99f215
commit 7c040062c5
7 changed files with 74 additions and 48 deletions

View File

@ -29,8 +29,8 @@ class CodeRunner {
}, },
// TODO: ideally shouldn't be needed or should be done through an API // TODO: ideally shouldn't be needed or should be done through an API
sandbox: { sandbox: {
EmbarkJS, EmbarkJS, // TODO: can just use registerVar in the embarkjs plugin
Web3, Web3, // TODO: can just use registerVar in the web3.js plugin
}, },
}, this.logger); }, this.logger);

View File

@ -120,8 +120,8 @@ class Console {
__("possible commands are:"), __("possible commands are:"),
// TODO: this help commands should be passed through an API // TODO: this help commands should be passed through an API
chalk.cyan("swarm") + " - " + __("instantiated swarm-api object configured to the current environment (available if swarm is enabled)"), // chalk.cyan("swarm") + " - " + __("instantiated swarm-api object configured to the current environment (available if swarm is enabled)"),
chalk.cyan("EmbarkJS") + " - " + __("EmbarkJS static functions for Storage, Messages, Names, etc."), // chalk.cyan("EmbarkJS") + " - " + __("EmbarkJS static functions for Storage, Messages, Names, etc."),
chalk.cyan("log [process] on/off") + " - " + __("Activate or deactivate the logs of a sub-process. Options: blockchain, ipfs, webserver"), chalk.cyan("log [process] on/off") + " - " + __("Activate or deactivate the logs of a sub-process. Options: blockchain, ipfs, webserver"),
]; ];

View File

@ -163,8 +163,8 @@ class EmbarkController {
engine.registerModuleGroup("coreComponents"); engine.registerModuleGroup("coreComponents");
engine.registerModuleGroup("blockchain"); // engine.registerModuleGroup("blockchain");
engine.registerModuleGroup("compiler"); // engine.registerModuleGroup("compiler");
// engine.startService("processManager"); // engine.startService("processManager");
// engine.startService("web3"); // engine.startService("web3");

View File

@ -102,7 +102,7 @@ class Engine {
const ServicesMonitor = require('./services_monitor.js'); const ServicesMonitor = require('./services_monitor.js');
this.servicesMonitor = new ServicesMonitor({events: this.events, logger: this.logger, plugins: this.plugins}); this.servicesMonitor = new ServicesMonitor({events: this.events, logger: this.logger, plugins: this.plugins});
this.servicesMonitor.addCheck('Embark', function (cb) { this.servicesMonitor.addCheck('Embark', (cb) => {
return cb({name: 'Embark ' + this.version, status: 'on'}); return cb({name: 'Embark ' + this.version, status: 'on'});
}, 0); }, 0);

View File

@ -14,7 +14,6 @@ class ServicesMonitor {
this.working = false; this.working = false;
self.events.setCommandHandler("services:register", (checkName, checkFn, time, initialStatus) => { self.events.setCommandHandler("services:register", (checkName, checkFn, time, initialStatus) => {
console.dir("check added for "+ checkName)
self.addCheck(checkName, checkFn, time, initialStatus); self.addCheck(checkName, checkFn, time, initialStatus);
}); });
} }
@ -37,10 +36,10 @@ ServicesMonitor.prototype.initCheck = function (checkName) {
} }
check.status = obj.status; check.status = obj.status;
const newState = {name: obj.name, status: obj.status, serviceName: checkName}; const newState = {name: obj.name, status: obj.status, serviceName: checkName};
if (!deepEqual(newState, self.checkState[checkName])) { // if (!deepEqual(newState, self.checkState[checkName])) {
self.checkState[checkName] = {name: obj.name, status: obj.status, serviceName: checkName}; self.checkState[checkName] = {name: obj.name, status: obj.status, serviceName: checkName};
self.events.emit("servicesState", self.checkState); self.events.emit("servicesState", self.checkState);
} // }
}); });
if (check.interval !== 0) { if (check.interval !== 0) {

View File

@ -0,0 +1,47 @@
const WebSocket = require("ws");
const http = require("http");
const LIVENESS_CHECK=`{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":42}`;
const parseAndRespond = (data, cb) => {
const resp = JSON.parse(data);
const [_, version, __] = resp.result.split('/');
cb(null, version);
}
const rpc = (host, port, cb) => {
const options = {
hostname: host, // TODO(andremedeiros): get from config
port: port, // TODO(andremedeiros): get from config
method: "POST",
timeout: 1000,
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(LIVENESS_CHECK)
}
};
const req = http.request(options, (res) => {
let data = "";
res.on("data", chunk => data = data + chunk);
res.on("end", () => parseAndRespond(data, cb));
});
req.on("error", (e) => cb(e));
req.write(LIVENESS_CHECK);
req.end();
}
const ws = (host, port, cb) => {
const conn = new WebSocket("ws://" + host + ":" + port);
conn.on("message", (data) => {
parseAndRespond(data, cb)
conn.close();
});
conn.on("open", () => conn.send(LIVENESS_CHECK));
conn.on("error", (e) => cb(e));
}
module.exports = {
ws,
rpc
}

View File

@ -1,5 +1,6 @@
const { normalizeInput } = require('embark-utils'); const { normalizeInput } = require('embark-utils');
import { BlockchainProcessLauncher } from './blockchainProcessLauncher'; import { BlockchainProcessLauncher } from './blockchainProcessLauncher';
import {ws, rpc} from './check.js';
class Geth { class Geth {
@ -13,7 +14,7 @@ class Geth {
this.isDev = options.isDev; this.isDev = options.isDev;
this.events = embark.events; this.events = embark.events;
this.plugins = options.plugins; this.plugins = options.plugins;
let plugin = this.plugins.createPlugin('gethplugin', {}); // let plugin = this.plugins.createPlugin('gethplugin', {});
this.events.request("blockchain:node:register", "geth", (readyCb) => { this.events.request("blockchain:node:register", "geth", (readyCb) => {
console.dir('registering blockchain node') console.dir('registering blockchain node')
@ -32,44 +33,23 @@ class Geth {
}) })
} }
registerServiceCheck() { _getNodeState(err, version, cb) {
console.dir("registerServiceCheck") if (err) return cb({ name: "Ethereum node not found", status: 'off' });
this.events.request("services:register", 'Ethereum', function (cb) {
cb({ name: "go-ethereum 1.1", status: 'on' })
// async.waterfall([
// function checkNodeConnection(next) {
// if (!self.provider || !self.provider.connected()) {
// return next(NO_NODE, { name: "No Blockchain node found", status: 'off' });
// }
// next();
// },
// function checkVersion(next) {
// // TODO: web3_clientVersion method is currently not implemented in web3.js 1.0
// self.web3._requestManager.send({ method: 'web3_clientVersion', params: [] }, (err, version) => {
// if (err || !version) {
// self.isWeb3Ready = false;
// return next(null, { name: "Ethereum node not found", status: 'off' });
// }
// if (version.indexOf("/") < 0) {
// self.events.emit(WEB3_READY);
// self.isWeb3Ready = true;
// return next(null, { name: version, status: 'on' });
// }
// let nodeName = version.split("/")[0];
// let versionNumber = version.split("/")[1].split("-")[0];
// let name = nodeName + " " + versionNumber + " (Ethereum)";
// self.events.emit(WEB3_READY); let nodeName = "go-ethereum"
// self.isWeb3Ready = true; let versionNumber = version.split("-")[0];
// return next(null, { name: name, status: 'on' }); let name = nodeName + " " + versionNumber + " (Ethereum)";
// }); return cb({ name, status: 'on' });
// } }
// ], (err, statusObj) => {
// if (err && err !== NO_NODE) { // TODO: need to get correct port taking into account the proxy
// return cb(err); registerServiceCheck() {
// } this.events.request("services:register", 'Ethereum', (cb) => {
// cb(statusObj); const {rpcHost, rpcPort, wsRPC, wsHost, wsPort} = this.blockchainConfig;
// }); if (wsRPC) {
return ws(wsHost, wsPort+10, (err, version) => this._getNodeState(err, version, cb));
}
rpc(rpcHost, rpcPort+10, (err, version) => this._getNodeState(err, version, cb));
}, 5000, 'off'); }, 5000, 'off');
} }