diff --git a/lib/constants.json b/lib/constants.json index 16e13e308..bc9144327 100644 --- a/lib/constants.json +++ b/lib/constants.json @@ -17,6 +17,8 @@ "contractConfigChanged": "contractConfigChanged" }, "process": { + "processLaunchRequest": "process:launch-request", + "processLaunchComplete": "process:launch-complete", "log": "log", "events": { "on": "on", diff --git a/lib/core/plugin.js b/lib/core/plugin.js index 17feb99ad..b22e30f74 100644 --- a/lib/core/plugin.js +++ b/lib/core/plugin.js @@ -22,6 +22,7 @@ var Plugin = function(options) { this.serviceChecks = []; this.pluginTypes = []; this.uploadCmds = []; + this.processLaunchCmds = []; this.imports = []; this.embarkjs_code = []; this.embarkjs_init_code = {}; @@ -189,6 +190,11 @@ Plugin.prototype.registerUploadCommand = function(cmd, cb) { this.pluginTypes.push('uploadCmds'); }; +Plugin.prototype.registerProcessLaunchCommand = function(cmd, cb) { + this.processLaunchCmds.push({cmd: cmd, cb: cb}); + this.pluginTypes.push('processLaunchCmds'); +}; + Plugin.prototype.addCodeToEmbarkJS = function(code) { this.embarkjs_code.push(code); this.pluginTypes.push('embarkjsCode'); diff --git a/lib/index.js b/lib/index.js index 9c4414c76..b4a1db00b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -324,7 +324,7 @@ class Embark { callback(); }, function checkStorageService(callback){ - let checkFn; + let checkFn; _.find(engine.servicesMonitor.checkList, (value, key) => { if(key.toLowerCase() === platform.toLowerCase()){ checkFn = value; @@ -336,7 +336,15 @@ class Embark { } checkFn.fn(function (serviceCheckResult) { if (!serviceCheckResult.status || serviceCheckResult.status === 'off') { - 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})}); + engine.events.emit(constants.process.processLaunchRequest, platform.toLowerCase()); + engine.events.on(constants.process.processLaunchComplete, (processName) => { + if (platform.toLowerCase() !== processName) { + return; + } + 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 bccd839cc..69ec9cc15 100644 --- a/lib/modules/ipfs/index.js +++ b/lib/modules/ipfs/index.js @@ -3,6 +3,8 @@ 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 { @@ -15,6 +17,7 @@ class IPFS { this.port = options.port || this.storageConfig.port; this.addCheck = options.addCheck; this.embark = embark; + this.ipfsProcess = null; this.commandlineDeploy(); this.setServiceCheck(); @@ -33,6 +36,30 @@ 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; @@ -58,7 +85,7 @@ class IPFS { } self.addCheck('IPFS', function (cb) { - self.logger.trace("Checking IPFS version..."); + self.logger.info("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/modules/ipfs/ipfsProcess.js new file mode 100644 index 000000000..18fc0ae81 --- /dev/null +++ b/lib/modules/ipfs/ipfsProcess.js @@ -0,0 +1,30 @@ +const shelljs = require('shelljs'); +const ProcessWrapper = require('../../process/processWrapper'); +const constants = require('../../constants'); + +let ipfsProcess; + +class IPFSProcess extends ProcessWrapper { + constructor(_options) { + super(); + + this.startIPFSDaemon(); + } + + startIPFSDaemon() { + shelljs.exec('ipfs daemon', (err, _stdout, _stderr) => { + if (err) { + console.error(err); + process.exit(1); + } + process.exit(); + }); + } +} + +process.on('message', (msg) => { + if (msg.action === constants.blockchain.init) { + ipfsProcess = new IPFSProcess(msg.options); + return ipfsProcess.send({result: constants.blockchain.initiated}); + } +}); diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index 8f6ffd195..db50770e1 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -101,8 +101,7 @@ class Pipeline { const webpackProcess = new ProcessLauncher({ modulePath: utils.joinPath(__dirname, 'webpackProcess.js'), logger: self.logger, - events: self.events, - normalizeInput: utils.normalizeInput + events: self.events }); webpackProcess.send({action: constants.pipeline.init, options: {}}); webpackProcess.send({action: constants.pipeline.build, file, importsList}); diff --git a/lib/process/processLauncher.js b/lib/process/processLauncher.js index 450a36007..35c4e2c56 100644 --- a/lib/process/processLauncher.js +++ b/lib/process/processLauncher.js @@ -1,6 +1,7 @@ const child_process = require('child_process'); const constants = require('../constants'); const path = require('path'); +const utils = require('../utils/utils'); class ProcessLauncher { @@ -9,7 +10,6 @@ class ProcessLauncher { * @param {Object} options Options tp start the process * * modulePath {String} Absolute path to the module to fork * * logger {Object} Logger - * * normalizeInput {Function} Function to normalize logs * * events {Function} Events Emitter instance * @return {ProcessLauncher} The ProcessLauncher instance */ @@ -17,7 +17,6 @@ class ProcessLauncher { this.name = path.basename(options.modulePath); this.process = child_process.fork(options.modulePath); this.logger = options.logger; - this.normalizeInput = options.normalizeInput; this.events = options.events; this.silent = options.silent; this.exitCallback = options.exitCallback; @@ -55,9 +54,9 @@ class ProcessLauncher { return; } if (this.logger[msg.type]) { - return this.logger[msg.type](this.normalizeInput(msg.message)); + return this.logger[msg.type](utils.normalizeInput(msg.message)); } - this.logger.debug(this.normalizeInput(msg.message)); + this.logger.debug(utils.normalizeInput(msg.message)); } // Handle event calls from the child process diff --git a/lib/processes/blockchainProcessLauncher.js b/lib/processes/blockchainProcessLauncher.js index 4c5101f8a..8c2203b42 100644 --- a/lib/processes/blockchainProcessLauncher.js +++ b/lib/processes/blockchainProcessLauncher.js @@ -24,7 +24,6 @@ class BlockchainProcessLauncher { modulePath: utils.joinPath(__dirname, '../cmds/blockchain/blockchainProcess.js'), logger: this.logger, events: this.events, - normalizeInput: this.normalizeInput, silent: this.logger.logLevel !== 'trace', exitCallback: this.processEnded.bind(this) });