From eaf9016c79440b5d2820516fadc4f23c3347afac Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 26 Dec 2017 20:32:51 -0500 Subject: [PATCH] move ipfs service check to its module --- lib/core/engine.js | 52 +++----------------------- lib/modules/ipfs/index.js | 78 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 78 insertions(+), 52 deletions(-) diff --git a/lib/core/engine.js b/lib/core/engine.js index e69f6aefc..83f12d050 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -1,5 +1,4 @@ let Web3 = require('web3'); -let utils = require('../utils/utils.js'); let Events = require('./events.js'); let Logger = require('./logger.js'); let Config = require('./config.js'); @@ -169,52 +168,11 @@ class Engine { } ipfsService(_options) { - let self = this; - - let storageConfig = this.config.storageConfig; - if (!storageConfig.enabled) { - return; - } - if (storageConfig.provider !== 'ipfs' && storageConfig.available_providers.indexOf("ipfs") < 0) { - return; - } - - let host = _options.host || storageConfig.host; - let port = _options.port || storageConfig.port; - - self.events.on('check:backOnline:IPFS', function () { - self.logger.info('IPFS node detected..'); - }); - - self.servicesMonitor.addCheck('IPFS', function (cb) { - utils.checkIsAvailable('http://' + host + ':' + port, 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('http://' + host + ':' + port + '/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 { - return cb({name: "IPFS ", status: 'off'}); - } - }); + this.registerModule('ipfs', { + addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor), + storageConfig: this.config.storageConfig, + host: _options.host, + port: _options.port }); } diff --git a/lib/modules/ipfs/index.js b/lib/modules/ipfs/index.js index 58616301f..76bf90ea4 100644 --- a/lib/modules/ipfs/index.js +++ b/lib/modules/ipfs/index.js @@ -1,17 +1,85 @@ let UploadIPFS = require('./upload.js'); +let utils = require('../../utils/utils.js'); class IPFS { constructor(embark, options) { this.logger = embark.logger; + this.events = embark.events; + this.buildDir = options.buildDir; + this.storageConfig = options.storageConfig; + this.host = options.host || this.storageConfig.host; + this.port = options.port || this.storageConfig.port; + this.addCheck = options.addCheck; + this.embark = embark; - this.upload_ipfs = new UploadIPFS({ - buildDir: options.buildDir || 'dist/', - storageConfig: options.storageConfig, - configIpfsBin: options.storageConfig.ipfs_bin || "ipfs" + this.commandlineDeploy(); + this.setServiceCheck(); + } + + commandlineDeploy() { + let upload_ipfs = new UploadIPFS({ + buildDir: this.buildDir || 'dist/', + storageConfig: this.storageConfig, + configIpfsBin: this.storageConfig.ipfs_bin || "ipfs" }); - embark.registerUploadCommand('ipfs', this.upload_ipfs.deploy.bind(this.upload_ipfs)); + this.embark.registerUploadCommand('ipfs', upload_ipfs.deploy.bind(upload_ipfs)); + } + + setServiceCheck() { + let self = this; + + let storageConfig = this.storageConfig; + + if (!storageConfig.enabled) { + return; + } + if (storageConfig.provider !== 'ipfs' && storageConfig.available_providers.indexOf("ipfs") < 0) { + return; + } + + self.events.on('check:backOnline:IPFS', function () { + self.logger.info('IPFS node detected..'); + }); + + self.events.on('check:wentOffline:IPFS', function () { + 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 { + return cb({name: "IPFS ", status: 'off'}); + } + }); + }); } }