From 0e502c15373860fbb2d62a2d953578f296cdf776 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 30 Dec 2017 21:44:59 -0500 Subject: [PATCH] refactor service check --- lib/modules/ipfs/index.js | 36 ++++++++---------------------------- lib/utils/utils.js | 12 ++++++++++++ 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/lib/modules/ipfs/index.js b/lib/modules/ipfs/index.js index f3685c7e1..f4be19ed8 100644 --- a/lib/modules/ipfs/index.js +++ b/lib/modules/ipfs/index.js @@ -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'}); }); }); } diff --git a/lib/utils/utils.js b/lib/utils/utils.js index e06d61948..f50bb4758 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -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,