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
sandbox: {
EmbarkJS,
Web3,
EmbarkJS, // TODO: can just use registerVar in the embarkjs plugin
Web3, // TODO: can just use registerVar in the web3.js plugin
},
}, this.logger);

View File

@ -120,8 +120,8 @@ class Console {
__("possible commands are:"),
// 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("EmbarkJS") + " - " + __("EmbarkJS static functions for Storage, Messages, Names, etc."),
// 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("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("blockchain");
engine.registerModuleGroup("compiler");
// engine.registerModuleGroup("blockchain");
// engine.registerModuleGroup("compiler");
// engine.startService("processManager");
// engine.startService("web3");

View File

@ -102,7 +102,7 @@ class Engine {
const ServicesMonitor = require('./services_monitor.js');
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'});
}, 0);

View File

@ -14,7 +14,6 @@ class ServicesMonitor {
this.working = false;
self.events.setCommandHandler("services:register", (checkName, checkFn, time, initialStatus) => {
console.dir("check added for "+ checkName)
self.addCheck(checkName, checkFn, time, initialStatus);
});
}
@ -37,10 +36,10 @@ ServicesMonitor.prototype.initCheck = function (checkName) {
}
check.status = obj.status;
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.events.emit("servicesState", self.checkState);
}
// }
});
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');
import { BlockchainProcessLauncher } from './blockchainProcessLauncher';
import {ws, rpc} from './check.js';
class Geth {
@ -13,7 +14,7 @@ class Geth {
this.isDev = options.isDev;
this.events = embark.events;
this.plugins = options.plugins;
let plugin = this.plugins.createPlugin('gethplugin', {});
// let plugin = this.plugins.createPlugin('gethplugin', {});
this.events.request("blockchain:node:register", "geth", (readyCb) => {
console.dir('registering blockchain node')
@ -32,44 +33,23 @@ class Geth {
})
}
registerServiceCheck() {
console.dir("registerServiceCheck")
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)";
_getNodeState(err, version, cb) {
if (err) return cb({ name: "Ethereum node not found", status: 'off' });
// self.events.emit(WEB3_READY);
// self.isWeb3Ready = true;
// return next(null, { name: name, status: 'on' });
// });
// }
// ], (err, statusObj) => {
// if (err && err !== NO_NODE) {
// return cb(err);
// }
// cb(statusObj);
// });
let nodeName = "go-ethereum"
let versionNumber = version.split("-")[0];
let name = nodeName + " " + versionNumber + " (Ethereum)";
return cb({ name, status: 'on' });
}
// TODO: need to get correct port taking into account the proxy
registerServiceCheck() {
this.events.request("services:register", 'Ethereum', (cb) => {
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');
}