refactor service check

This commit is contained in:
Iuri Matias 2017-12-30 21:44:59 -05:00
parent e569c08953
commit 0e502c1537
2 changed files with 20 additions and 28 deletions

View File

@ -50,37 +50,17 @@ class IPFS {
self.logger.info('IPFS node is offline..'); self.logger.info('IPFS node is offline..');
}); });
let server = 'http://' + this.host + ':' + this.port;
self.logger.info(server);
this.addCheck('IPFS', function (cb) { this.addCheck('IPFS', function (cb) {
utils.checkIsAvailable(server, function (available) {
if (available) {
//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..."); self.logger.trace("Checking IPFS version...");
utils.httpGet(server + '/api/v0/version', function (err, body) { utils.httpGetJson('http://' + this.host + ':' + this.port + '/api/v0/version', function (err, body) {
if (err) { if (err) {
self.logger.trace("Check IPFS version error: " + err); self.logger.trace("Check IPFS version error: " + err);
return cb({name: "IPFS ", status: 'off'}); return cb({name: "IPFS ", status: 'off'});
} }
try { if (body.Version) {
let parsed = JSON.parse(body); return cb({name: ("IPFS " + body.Version), status: 'on'});
if (parsed.Version) {
return cb({name: ("IPFS " + parsed.Version), status: 'on'});
} }
else {
return cb({name: "IPFS ", status: 'on'}); return cb({name: "IPFS ", status: 'on'});
}
}
catch (e) {
return cb({name: "IPFS ", status: 'off'});
}
});
}
else {
return cb({name: "IPFS ", status: 'off'});
}
}); });
}); });
} }

View File

@ -56,6 +56,17 @@ function httpsGet(url, callback) {
httpGetRequest(https, url, callback); httpGetRequest(https, url, callback);
} }
function httpGetJson(url, callback) {
httpGetRequest(http, url, function(err, body) {
try {
let parsed = JSON.parse(body);
return callback(err, parsed);
} catch(e) {
return callback(e);
}
});
}
function runCmd(cmd, options) { function runCmd(cmd, options) {
let result = shelljs.exec(cmd, options || {silent: true}); let result = shelljs.exec(cmd, options || {silent: true});
if (result.code !== 0) { if (result.code !== 0) {
@ -120,6 +131,7 @@ module.exports = {
checkIsAvailable: checkIsAvailable, checkIsAvailable: checkIsAvailable,
httpGet: httpGet, httpGet: httpGet,
httpsGet: httpsGet, httpsGet: httpsGet,
httpGetJson: httpGetJson,
runCmd: runCmd, runCmd: runCmd,
cd: cd, cd: cd,
sed: sed, sed: sed,