From e3a93a6d61aebcc764be0865bdd9dd801b316a75 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 23 May 2018 15:10:53 -0400 Subject: [PATCH] add storageProcessesLauncher that can work for swarm or ipfs --- lib/constants.json | 4 ++ lib/index.js | 15 ++++--- lib/modules/ipfs/index.js | 28 +------------ .../storageProcesses/ipfs.js} | 6 +-- .../storageProcessesLauncher.js | 42 +++++++++++++++++++ 5 files changed, 59 insertions(+), 36 deletions(-) rename lib/{modules/ipfs/ipfsProcess.js => processes/storageProcesses/ipfs.js} (72%) create mode 100644 lib/processes/storageProcesses/storageProcessesLauncher.js diff --git a/lib/constants.json b/lib/constants.json index bc9144327..0315ffbe2 100644 --- a/lib/constants.json +++ b/lib/constants.json @@ -36,5 +36,9 @@ "blockchainReady": "blockchainReady", "init": "init", "initiated": "initiated" + }, + "storage": { + "init": "init", + "initiated": "initiated" } } diff --git a/lib/index.js b/lib/index.js index b4a1db00b..4b2fd38d7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -285,6 +285,7 @@ class Embark { } upload(options) { + const StorageProcessesLauncher = require('./processes/storageProcesses/storageProcessesLauncher'); this.context = options.context || [constants.contexts.upload, constants.contexts.build]; @@ -336,15 +337,17 @@ class Embark { } checkFn.fn(function (serviceCheckResult) { if (!serviceCheckResult.status || serviceCheckResult.status === 'off') { - engine.events.emit(constants.process.processLaunchRequest, platform.toLowerCase()); - engine.events.on(constants.process.processLaunchComplete, (processName) => { - if (platform.toLowerCase() !== processName) { - return; + const storageProcessesLauncher = new StorageProcessesLauncher({ + logger: engine.logger, + events: engine.events + }); + 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(); }); - //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(); }); diff --git a/lib/modules/ipfs/index.js b/lib/modules/ipfs/index.js index 69ec9cc15..ea32c36ca 100644 --- a/lib/modules/ipfs/index.js +++ b/lib/modules/ipfs/index.js @@ -3,8 +3,6 @@ let utils = require('../../utils/utils.js'); let fs = require('../../core/fs.js'); let RunCode = require('../../coderunner/runCode'); let IpfsApi = require('ipfs-api'); -const ProcessLauncher = require('../../process/ProcessLauncher'); -const constants = require('../../constants'); class IPFS { @@ -36,30 +34,6 @@ class 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() { let self = this; @@ -85,7 +59,7 @@ class IPFS { } 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) { if (err) { self.logger.trace("Check IPFS version error: " + err); diff --git a/lib/modules/ipfs/ipfsProcess.js b/lib/processes/storageProcesses/ipfs.js similarity index 72% rename from lib/modules/ipfs/ipfsProcess.js rename to lib/processes/storageProcesses/ipfs.js index 18fc0ae81..36202b4da 100644 --- a/lib/modules/ipfs/ipfsProcess.js +++ b/lib/processes/storageProcesses/ipfs.js @@ -12,7 +12,7 @@ class IPFSProcess extends ProcessWrapper { } startIPFSDaemon() { - shelljs.exec('ipfs daemon', (err, _stdout, _stderr) => { + shelljs.exec('ipfs daemon', {silent: true}, (err, _stdout, _stderr) => { if (err) { console.error(err); process.exit(1); @@ -23,8 +23,8 @@ class IPFSProcess extends ProcessWrapper { } process.on('message', (msg) => { - if (msg.action === constants.blockchain.init) { + if (msg.action === constants.storage.init) { ipfsProcess = new IPFSProcess(msg.options); - return ipfsProcess.send({result: constants.blockchain.initiated}); + return ipfsProcess.send({result: constants.storage.initiated}); } }); diff --git a/lib/processes/storageProcesses/storageProcessesLauncher.js b/lib/processes/storageProcesses/storageProcessesLauncher.js new file mode 100644 index 000000000..10048e665 --- /dev/null +++ b/lib/processes/storageProcesses/storageProcessesLauncher.js @@ -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;