Skip to content

Commit

Permalink
Merge pull request #65 from yohan-pereira/remove_pstree
Browse files Browse the repository at this point in the history
Revert "Overriding stop method for fixing stop local issue in docker …
  • Loading branch information
raghuhit authored Nov 26, 2018
2 parents 5fec9d5 + caf72de commit dfea668
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 38 deletions.
77 changes: 41 additions & 36 deletions lib/Local.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ var childProcess = require('child_process'),
path = require('path'),
running = require('is-running'),
LocalBinary = require('./LocalBinary'),
LocalError = require('./LocalError'),
psTree = require('ps-tree');
LocalError = require('./LocalError');


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');
Expand Down Expand Up @@ -58,20 +57,56 @@ 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) && this.isProcessRunning;
return this.pid && running(this.pid);
};

this.stop = function (callback) {
if(!this.pid) return callback();
this.killAllProcesses(function(error){
this.opcode = 'stop';
this.tunnel = childProcess.execFile(this.binaryPath, this.getBinaryArgs(), function(error){
if(error) callback(new LocalError(error.toString()));
callback();
});
Expand Down Expand Up @@ -252,35 +287,5 @@ 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;
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "browserstack-local",
"version": "1.3.5",
"version": "1.3.6",
"description": "Nodejs bindings for BrowserStack Local",
"engine": "^0.10.44",
"main": "index.js",
Expand All @@ -19,7 +19,6 @@
"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"
},
Expand Down

0 comments on commit dfea668

Please sign in to comment.