From e1f22d5d5dc8341d59a0e8a2a451daa76572382f Mon Sep 17 00:00:00 2001 From: Punit Mittal Date: Tue, 27 Nov 2018 13:15:32 +0530 Subject: [PATCH 1/3] Revert "Merge pull request #65 from yohan-pereira/remove_pstree" This reverts commit dfea66823e80cef74fb6bee38502942df7be55c3, reversing changes made to 5fec9d58d51eb1dc620fe7d81eba07b197605ecd. --- lib/Local.js | 77 ++++++++++++++++++++++++---------------------------- package.json | 3 +- 2 files changed, 38 insertions(+), 42 deletions(-) diff --git a/lib/Local.js b/lib/Local.js index 57f510d..51e5b8c 100644 --- a/lib/Local.js +++ b/lib/Local.js @@ -3,11 +3,12 @@ var childProcess = require('child_process'), path = require('path'), running = require('is-running'), LocalBinary = require('./LocalBinary'), - LocalError = require('./LocalError'); - + LocalError = require('./LocalError'), + psTree = require('ps-tree'); function Local(){ this.pid = undefined; + this.isProcessRunning = false; this.retriesLeft = 5; this.key = process.env.BROWSERSTACK_ACCESS_KEY; this.logfile = path.join(process.cwd(), 'local.log'); @@ -57,56 +58,20 @@ function Local(){ callback(new LocalError(data['message']['message'])); } else { that.pid = data['pid']; + that.isProcessRunning = true; callback(); } }); - - // that.tunnel = childProcess.spawn(binaryPath, that.getBinaryArgs()); - // that.tunnel.on('exit', function(){ - // that.tunnel = undefined; - // if(that.exitCallback) that.exitCallback(); - // }); - - // that.stdout = fs.openSync(that.logfile, 'r'); - // var chunkSize = 512, - // buffer = new Buffer(81920), - // bytesRead = 0, - // error = undefined; - - // while(true){ - // var bytes = fs.readSync(that.stdout, buffer, bytesRead, chunkSize, bytesRead); - // if(bytes == 0) continue; - - // var buffRead = buffer.slice(bytesRead, bytesRead+bytes); - // bytesRead += bytes; - - // var data = buffRead.toString(); - - // if(data.match(that.errorRegex)){ - // fs.closeSync(that.stdout); - // error = data.match(that.errorRegex)[0].trim(); - // break; - // } - - // if(data.match(that.doneRegex)){ - // fs.closeSync(that.stdout); - // break; - // } - // } - - // if(error) throw new LocalError(error); - // callback(); }); }; this.isRunning = function(){ - return this.pid && running(this.pid); + return this.pid && running(this.pid) && this.isProcessRunning; }; this.stop = function (callback) { if(!this.pid) return callback(); - this.opcode = 'stop'; - this.tunnel = childProcess.execFile(this.binaryPath, this.getBinaryArgs(), function(error){ + this.killAllProcesses(function(error){ if(error) callback(new LocalError(error.toString())); callback(); }); @@ -287,5 +252,35 @@ function Local(){ } return args; }; + + this.killAllProcesses = function(callback){ + psTree(this.pid, (err, children) => { + var childPids = children.map(val => val.PID); + var killChecker = setInterval(() => { + if(childPids.length === 0) { + clearInterval(killChecker); + try { + process.kill(this.pid); + // This gives time to local binary to send kill signal to railsApp. + setTimeout(() => { + this.isProcessRunning = false; + callback(); + }, 2000); + } catch(err) { + this.isProcessRunning = false; + callback(); + } + } + for(var i in childPids) { + try { + process.kill(childPids[i]); + } catch(err) { + childPids.splice(i, 1); + } + } + },500); + }); + }; } + module.exports = Local; diff --git a/package.json b/package.json index 6d663ee..a8b0a2b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "browserstack-local", - "version": "1.3.6", + "version": "1.3.5", "description": "Nodejs bindings for BrowserStack Local", "engine": "^0.10.44", "main": "index.js", @@ -19,6 +19,7 @@ "dependencies": { "https-proxy-agent": "^2.2.1", "is-running": "^2.0.0", + "ps-tree": "^1.1.0", "sinon": "^1.17.6", "temp-fs": "^0.9.9" }, From 0c5043f4528357f81a57cdc46c805167ce88f940 Mon Sep 17 00:00:00 2001 From: Punit Mittal Date: Tue, 27 Nov 2018 16:17:00 +0530 Subject: [PATCH 2/3] Locked ps-tree version to 1.1.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a8b0a2b..9b54f3a 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "dependencies": { "https-proxy-agent": "^2.2.1", "is-running": "^2.0.0", - "ps-tree": "^1.1.0", + "ps-tree": "=1.1.1", "sinon": "^1.17.6", "temp-fs": "^0.9.9" }, From 2c7431079fcd754f6000ee5c1d4fedfcb271021a Mon Sep 17 00:00:00 2001 From: Punit Mittal Date: Tue, 27 Nov 2018 16:40:05 +0530 Subject: [PATCH 3/3] Bumped package version to 1.3.7 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9b54f3a..48b47d4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "browserstack-local", - "version": "1.3.5", + "version": "1.3.7", "description": "Nodejs bindings for BrowserStack Local", "engine": "^0.10.44", "main": "index.js",