move ipfs service check to its module

This commit is contained in:
Iuri Matias 2017-12-26 20:32:51 -05:00
parent 50f1072372
commit eaf9016c79
2 changed files with 78 additions and 52 deletions

View File

@ -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
});
}

View File

@ -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'});
}
});
});
}
}