add storageProcessesLauncher that can work for swarm or ipfs

This commit is contained in:
Jonathan Rainville 2018-05-23 15:10:53 -04:00
parent 56f25ebba1
commit e3a93a6d61
5 changed files with 59 additions and 36 deletions

View File

@ -36,5 +36,9 @@
"blockchainReady": "blockchainReady", "blockchainReady": "blockchainReady",
"init": "init", "init": "init",
"initiated": "initiated" "initiated": "initiated"
},
"storage": {
"init": "init",
"initiated": "initiated"
} }
} }

View File

@ -285,6 +285,7 @@ class Embark {
} }
upload(options) { upload(options) {
const StorageProcessesLauncher = require('./processes/storageProcesses/storageProcessesLauncher');
this.context = options.context || [constants.contexts.upload, constants.contexts.build]; this.context = options.context || [constants.contexts.upload, constants.contexts.build];
@ -336,15 +337,17 @@ class Embark {
} }
checkFn.fn(function (serviceCheckResult) { checkFn.fn(function (serviceCheckResult) {
if (!serviceCheckResult.status || serviceCheckResult.status === 'off') { if (!serviceCheckResult.status || serviceCheckResult.status === 'off') {
engine.events.emit(constants.process.processLaunchRequest, platform.toLowerCase()); const storageProcessesLauncher = new StorageProcessesLauncher({
engine.events.on(constants.process.processLaunchComplete, (processName) => { logger: engine.logger,
if (platform.toLowerCase() !== processName) { events: engine.events
return; });
return storageProcessesLauncher.launchProcess(platform.toLowerCase(), (err) => {
if (err) {
engine.logger.error(err);
return callback({message: __('Cannot upload: {{platform}} node is not running on {{protocol}}://{{host}}:{{port}}.', {platform: platform, protocol: engine.config.storageConfig.protocol, host: engine.config.storageConfig.host, port: engine.config.storageConfig.port})});
} }
console.log('GOT STUFF');
callback(); callback();
}); });
//return callback({message: __('Cannot upload: {{platform}} node is not running on {{protocol}}://{{host}}:{{port}}.', {platform: platform, protocol: engine.config.storageConfig.protocol, host: engine.config.storageConfig.host, port: engine.config.storageConfig.port})});
} }
callback(); callback();
}); });

View File

@ -3,8 +3,6 @@ let utils = require('../../utils/utils.js');
let fs = require('../../core/fs.js'); let fs = require('../../core/fs.js');
let RunCode = require('../../coderunner/runCode'); let RunCode = require('../../coderunner/runCode');
let IpfsApi = require('ipfs-api'); let IpfsApi = require('ipfs-api');
const ProcessLauncher = require('../../process/ProcessLauncher');
const constants = require('../../constants');
class IPFS { class IPFS {
@ -36,30 +34,6 @@ class IPFS {
this.embark.registerUploadCommand('ipfs', upload_ipfs.deploy.bind(upload_ipfs)); this.embark.registerUploadCommand('ipfs', upload_ipfs.deploy.bind(upload_ipfs));
} }
processExited(code) {
this.logger.error('IPFS process ended before the end of this process. Code: ' + code);
}
watchForProcessLaunch() {
const self = this;
self.events.on(constants.process.processLaunchRequest, (processName) => {
if (processName !== 'ipfs' || self.ipfsProcess) {
return;
}
self.ipfsProcess = new ProcessLauncher({
modulePath: utils.joinPath(__dirname, './ipfsProcess.js'),
logger: self.logger,
events: self.events,
silent: true,
exitCallback: self.processExited.bind(this)
});
self.ipfsProcess.send({action: constants.blockchain.init, options: {}});
self.events.emit(constants.process.processLaunchComplete, 'ipfs');
});
}
setServiceCheck() { setServiceCheck() {
let self = this; let self = this;
@ -85,7 +59,7 @@ class IPFS {
} }
self.addCheck('IPFS', function (cb) { self.addCheck('IPFS', function (cb) {
self.logger.info("Checking IPFS version..."); self.logger.trace("Checking IPFS version...");
utils.httpGetJson('http://' + self.host + ':' + self.port + '/api/v0/version', function (err, body) { utils.httpGetJson('http://' + self.host + ':' + self.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);

View File

@ -12,7 +12,7 @@ class IPFSProcess extends ProcessWrapper {
} }
startIPFSDaemon() { startIPFSDaemon() {
shelljs.exec('ipfs daemon', (err, _stdout, _stderr) => { shelljs.exec('ipfs daemon', {silent: true}, (err, _stdout, _stderr) => {
if (err) { if (err) {
console.error(err); console.error(err);
process.exit(1); process.exit(1);
@ -23,8 +23,8 @@ class IPFSProcess extends ProcessWrapper {
} }
process.on('message', (msg) => { process.on('message', (msg) => {
if (msg.action === constants.blockchain.init) { if (msg.action === constants.storage.init) {
ipfsProcess = new IPFSProcess(msg.options); ipfsProcess = new IPFSProcess(msg.options);
return ipfsProcess.send({result: constants.blockchain.initiated}); return ipfsProcess.send({result: constants.storage.initiated});
} }
}); });

View File

@ -0,0 +1,42 @@
const fs = require('../../core/fs');
const utils = require('../../utils/utils');
const ProcessLauncher = require('../../process/ProcessLauncher');
const constants = require('../../constants');
class StorageProcessesLauncher {
constructor(options) {
this.logger = options.logger;
this.events = options.events;
this.processes = {};
}
processExited(storageName, code) {
this.logger.error(__(`Storage process for ${storageName} ended before the end of this process. Code: ${code}`));
}
launchProcess(storageName, callback) {
const self = this;
if (self.processes[storageName]) {
return callback(__('Storage process already started'));
}
const filePath = utils.joinPath(__dirname, `./${storageName}.js`);
fs.access(filePath, (err) => {
if (err) {
callback(__('No process file for this storage type exists. Please start the process locally.'));
}
self.logger.info(__(`Starting ${storageName} process`).cyan);
self.processes[storageName] = new ProcessLauncher({
modulePath: filePath,
logger: self.logger,
events: self.events,
silent: true,
exitCallback: self.processExited.bind(this, storageName)
});
self.processes[storageName].send({action: constants.blockchain.init, options: {}});
callback();
});
}
}
module.exports = StorageProcessesLauncher;