2016-09-22 22:24:01 +00:00
|
|
|
var Web3 = require('web3');
|
|
|
|
var async = require('async');
|
2017-02-23 22:18:05 +00:00
|
|
|
var http = require('http');
|
2017-02-18 20:27:08 +00:00
|
|
|
var utils = require('./utils.js');
|
2016-09-22 22:24:01 +00:00
|
|
|
|
2017-03-04 19:20:28 +00:00
|
|
|
// TODO: needs a refactor and be done in a different way
|
2016-09-22 22:24:01 +00:00
|
|
|
var ServicesMonitor = function(options) {
|
|
|
|
this.logger = options.logger;
|
|
|
|
this.interval = options.interval || 5000;
|
|
|
|
this.config = options.config;
|
2016-10-30 01:41:50 +00:00
|
|
|
this.serverHost = options.serverHost || 'localhost';
|
2016-10-25 23:33:24 +00:00
|
|
|
this.serverPort = options.serverPort || 8000;
|
2016-10-30 02:00:54 +00:00
|
|
|
this.runWebserver = options.runWebserver;
|
2016-10-31 02:37:06 +00:00
|
|
|
this.version = options.version;
|
2016-09-22 22:24:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ServicesMonitor.prototype.startMonitor = function() {
|
2017-01-15 23:46:40 +00:00
|
|
|
this.check();
|
2016-09-22 22:24:01 +00:00
|
|
|
this.monitor = setInterval(this.check.bind(this), this.interval);
|
|
|
|
};
|
|
|
|
|
|
|
|
ServicesMonitor.prototype.stopMonitor = function() {
|
|
|
|
};
|
|
|
|
|
|
|
|
ServicesMonitor.prototype.check = function() {
|
|
|
|
var self = this;
|
|
|
|
async.waterfall([
|
|
|
|
function connectWeb3(callback) {
|
|
|
|
self.logger.trace('connectWeb3');
|
|
|
|
var web3 = new Web3();
|
|
|
|
var web3Endpoint = 'http://' + self.config.blockchainConfig.rpcHost + ':' + self.config.blockchainConfig.rpcPort;
|
|
|
|
web3.setProvider(new web3.providers.HttpProvider(web3Endpoint));
|
|
|
|
callback(null, web3, []);
|
|
|
|
},
|
2016-10-22 16:11:47 +00:00
|
|
|
function addEmbarkVersion(web3, result, callback) {
|
|
|
|
self.logger.trace('addEmbarkVersion');
|
2016-10-31 02:37:06 +00:00
|
|
|
result.push(('Embark ' + self.version).green);
|
2016-10-22 16:11:47 +00:00
|
|
|
callback(null, web3, result);
|
|
|
|
},
|
2016-09-22 22:24:01 +00:00
|
|
|
function checkEthereum(web3, result, callback) {
|
|
|
|
self.logger.trace('checkEthereum');
|
2016-10-22 16:11:47 +00:00
|
|
|
var service;
|
|
|
|
if (web3.isConnected()) {
|
2017-02-22 13:21:45 +00:00
|
|
|
service = (web3.version.node.split("/")[0] + " " + web3.version.node.split("/")[1].split("-")[0] + " (Ethereum)").green;
|
2016-10-22 16:11:47 +00:00
|
|
|
} else {
|
|
|
|
service = "No Blockchain node found".red;
|
|
|
|
}
|
2016-09-22 22:24:01 +00:00
|
|
|
result.push(service);
|
|
|
|
callback(null, web3, result);
|
|
|
|
},
|
|
|
|
function checkWhisper(web3, result, callback) {
|
|
|
|
self.logger.trace('checkWhisper');
|
|
|
|
web3.version.getWhisper(function(err, res) {
|
2017-02-22 13:21:45 +00:00
|
|
|
var service = 'Whisper';
|
2016-09-22 22:24:01 +00:00
|
|
|
result.push(err ? service.red : service.green);
|
|
|
|
callback(null, result);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function checkIPFS(result, callback) {
|
|
|
|
self.logger.trace('checkIPFS');
|
2016-10-02 23:43:19 +00:00
|
|
|
|
2017-02-18 20:27:08 +00:00
|
|
|
utils.checkIsAvailable('http://localhost:5001', function(available) {
|
|
|
|
if (available) {
|
2017-02-23 22:18:05 +00:00
|
|
|
//Ideally this method should be in an IPFS API JSONRPC wrapper
|
|
|
|
//The URL should also be flexible to accept non-default IPFS url
|
|
|
|
self.logger.trace("Checking IPFS version...");
|
|
|
|
http.get('http://localhost:5001/api/v0/version', function(res) {
|
|
|
|
var body = '';
|
|
|
|
res.on('data', function(d) {
|
|
|
|
body += d;
|
|
|
|
});
|
|
|
|
res.on('end', function() {
|
|
|
|
var parsed = JSON.parse(body);
|
|
|
|
if(parsed.Version){
|
|
|
|
result.push(("IPFS " + parsed.Version).green);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
result.push("IPFS".green);
|
|
|
|
}
|
|
|
|
callback(null, result);
|
|
|
|
});
|
|
|
|
res.on('error', function(err) {
|
|
|
|
self.logger.trace("Check IPFS version error: " + err);
|
|
|
|
result.push("IPFS".green);
|
|
|
|
callback(null, result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2017-02-18 20:27:08 +00:00
|
|
|
result.push('IPFS'.red);
|
2017-02-24 00:45:25 +00:00
|
|
|
return callback(null, result);
|
2016-10-02 23:43:19 +00:00
|
|
|
}
|
|
|
|
});
|
2016-09-22 22:24:01 +00:00
|
|
|
},
|
|
|
|
function checkDevServer(result, callback) {
|
2017-03-07 03:08:43 +00:00
|
|
|
var host = self.serverHost || self.config.webServerConfig.host;
|
|
|
|
var port = self.serverPort || self.config.webServerConfig.port;
|
2016-09-22 22:24:01 +00:00
|
|
|
self.logger.trace('checkDevServer');
|
2017-03-04 19:20:28 +00:00
|
|
|
var devServer = 'Webserver (http://' + host + ':' + port + ')';
|
2016-10-30 02:00:54 +00:00
|
|
|
devServer = (self.runWebserver) ? devServer.green : devServer.red;
|
|
|
|
result.push(devServer);
|
2016-09-22 22:24:01 +00:00
|
|
|
callback(null, result);
|
|
|
|
}
|
|
|
|
], function(err, result) {
|
2016-10-31 01:47:21 +00:00
|
|
|
if (err) {
|
|
|
|
self.logger.error(err.message);
|
|
|
|
} else {
|
|
|
|
self.logger.availableServices(result);
|
|
|
|
}
|
2016-09-22 22:24:01 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = ServicesMonitor;
|