diff --git a/lib/cmds/blockchain/blockchain.js b/lib/cmds/blockchain/blockchain.js index 89a3828f..0fe6a490 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 421703e0..e9d3724f 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 21faad20..908eb9fd 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 b677f81a..ba30dbd5 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 9cbddfbf..226c7864 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 2c40a1ea..49a1ddf4 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) {