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

View File

@ -56,6 +56,17 @@ function httpsGet(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) {
let result = shelljs.exec(cmd, options || {silent: true});
if (result.code !== 0) {
@ -120,6 +131,7 @@ module.exports = {
checkIsAvailable: checkIsAvailable,
httpGet: httpGet,
httpsGet: httpsGet,
httpGetJson: httpGetJson,
runCmd: runCmd,
cd: cd,
sed: sed,