From 138aaf39ffab02325e2003a766b4e7daa745e2d5 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 30 May 2018 10:52:15 -0400 Subject: [PATCH] fix processes on ubuntu --- lib/cmds/blockchain/blockchain.js | 18 ++++++++++++------ lib/cmds/blockchain/blockchainProcess.js | 7 +++++++ lib/dashboard/monitor.js | 1 + lib/processes/blockchainProcessLauncher.js | 4 ++++ lib/processes/storageProcesses/ipfs.js | 19 ++++++++++++++----- .../storageProcessesLauncher.js | 6 ++++++ 6 files changed, 44 insertions(+), 11 deletions(-) diff --git a/lib/cmds/blockchain/blockchain.js b/lib/cmds/blockchain/blockchain.js index 89a3828f6..0fe6a4905 100644 --- a/lib/cmds/blockchain/blockchain.js +++ b/lib/cmds/blockchain/blockchain.js @@ -120,9 +120,9 @@ Blockchain.prototype.run = function() { return; } args = _.compact(args); - const child = child_process.spawn(cmd, args, {cwd: process.cwd()}); + self.child = child_process.spawn(cmd, args, {cwd: process.cwd()}); - child.on('error', (err) => { + self.child.on('error', (err) => { err = err.toString(); console.error('Blockchain error: ', err); if (self.env === 'development' && err.indexOf('Failed to unlock') > 0) { @@ -131,19 +131,19 @@ Blockchain.prototype.run = function() { console.error(__('Otherwise, you can change your data directory in blockchain.json (datadir)').yellow); } }); - child.stdout.on('data', (data) => { + self.child.stdout.on('data', (data) => { console.log(`Geth error: ${data}`); }); // Geth logs appear in stderr somehow - child.stderr.on('data', (data) => { + self.child.stderr.on('data', (data) => { data = data.toString(); - if (self.onReadyCallback && !self.readyCalled && data.indexOf('Mapped network port') > -1) { + if (self.onReadyCallback && !self.readyCalled && data.indexOf('WebSocket endpoint opened') > -1) { self.readyCalled = true; self.onReadyCallback(); } console.log('Geth: ' + data); }); - child.on('exit', (code) => { + self.child.on('exit', (code) => { if (code) { console.error('Geth exited with error code ' + code); } @@ -151,6 +151,12 @@ Blockchain.prototype.run = function() { }); }; +Blockchain.prototype.kill = function() { + if (this.child) { + this.child.kill(); + } +}; + Blockchain.prototype.checkPathLength = function() { let dappPath = fs.dappPath(''); if (dappPath.length > 66) { diff --git a/lib/cmds/blockchain/blockchainProcess.js b/lib/cmds/blockchain/blockchainProcess.js index 421703e07..e9d3724fd 100644 --- a/lib/cmds/blockchain/blockchainProcess.js +++ b/lib/cmds/blockchain/blockchainProcess.js @@ -30,9 +30,16 @@ class BlockchainProcess extends ProcessWrapper { blockchainReady() { blockchainProcess.send({result: constants.blockchain.blockchainReady}); } + + kill() { + this.blockchain.kill(); + } } process.on('message', (msg) => { + if (msg === 'exit') { + return blockchainProcess.kill(); + } if (msg.action === constants.blockchain.init) { blockchainProcess = new BlockchainProcess(msg.options); return blockchainProcess.send({result: constants.blockchain.initiated}); diff --git a/lib/dashboard/monitor.js b/lib/dashboard/monitor.js index 21faad20d..908eb9fd7 100644 --- a/lib/dashboard/monitor.js +++ b/lib/dashboard/monitor.js @@ -330,6 +330,7 @@ class Dashboard { let self = this; this.input.key(["C-c"], function () { + self.events.emit('exit'); process.exit(0); }); diff --git a/lib/processes/blockchainProcessLauncher.js b/lib/processes/blockchainProcessLauncher.js index b677f81a9..ba30dbd5f 100644 --- a/lib/processes/blockchainProcessLauncher.js +++ b/lib/processes/blockchainProcessLauncher.js @@ -43,6 +43,10 @@ class BlockchainProcessLauncher { this.logger.info(__('Blockchain node is ready').cyan); this.events.emit(constants.blockchain.blockchainReady); }); + + this.events.on('exit', () => { + this.blockchainProcess.send('exit'); + }); } } diff --git a/lib/processes/storageProcesses/ipfs.js b/lib/processes/storageProcesses/ipfs.js index 9cbddfbfe..226c78644 100644 --- a/lib/processes/storageProcesses/ipfs.js +++ b/lib/processes/storageProcesses/ipfs.js @@ -31,17 +31,17 @@ class IPFSProcess extends ProcessWrapper { startIPFSDaemon() { const self = this; - const child = child_process.spawn('ipfs', ['daemon']); + this.child = child_process.spawn('ipfs', ['daemon']); - child.on('error', (err) => { + this.child.on('error', (err) => { err = err.toString(); console.error('IPFS error: ', err); }); - child.stderr.on('data', (data) => { + this.child.stderr.on('data', (data) => { data = data.toString(); console.log(`IPFS error: ${data}`); }); - child.stdout.on('data', (data) => { + this.child.stdout.on('data', (data) => { data = data.toString(); if (!self.readyCalled && data.indexOf('Daemon is ready') > -1) { self.readyCalled = true; @@ -49,15 +49,24 @@ class IPFSProcess extends ProcessWrapper { } console.log('IPFS: ' + data); }); - child.on('exit', (code) => { + this.child.on('exit', (code) => { if (code) { console.error('IPFS exited with error code ' + code); } }); } + + kill() { + if (this.child) { + this.child.kill(); + } + } } process.on('message', (msg) => { + if (msg === 'exit') { + return ipfsProcess.kill(); + } if (msg.action === constants.storage.init) { ipfsProcess = new IPFSProcess(msg.options); } diff --git a/lib/processes/storageProcesses/storageProcessesLauncher.js b/lib/processes/storageProcesses/storageProcessesLauncher.js index 2c40a1ea6..49a1ddf41 100644 --- a/lib/processes/storageProcesses/storageProcessesLauncher.js +++ b/lib/processes/storageProcesses/storageProcessesLauncher.js @@ -10,6 +10,12 @@ class StorageProcessesLauncher { this.storageConfig = options.storageConfig; this.webServerConfig = options.webServerConfig; this.processes = {}; + + this.events.on('exit', () => { + Object.keys(this.processes).forEach(processName => { + this.processes[processName].send('exit'); + }); + }); } processExited(storageName, code) {