Skip to content

Commit 230ec93

Browse files
authored
Merge pull request #66 from punitx/lock_ps_tree_version
Locked ps tree version to 1.1.1
2 parents dfea668 + 2c74310 commit 230ec93

File tree

2 files changed

+38
-42
lines changed

2 files changed

+38
-42
lines changed

Diff for: lib/Local.js

+36-41
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ var childProcess = require('child_process'),
33
path = require('path'),
44
running = require('is-running'),
55
LocalBinary = require('./LocalBinary'),
6-
LocalError = require('./LocalError');
7-
6+
LocalError = require('./LocalError'),
7+
psTree = require('ps-tree');
88

99
function Local(){
1010
this.pid = undefined;
11+
this.isProcessRunning = false;
1112
this.retriesLeft = 5;
1213
this.key = process.env.BROWSERSTACK_ACCESS_KEY;
1314
this.logfile = path.join(process.cwd(), 'local.log');
@@ -57,56 +58,20 @@ function Local(){
5758
callback(new LocalError(data['message']['message']));
5859
} else {
5960
that.pid = data['pid'];
61+
that.isProcessRunning = true;
6062
callback();
6163
}
6264
});
63-
64-
// that.tunnel = childProcess.spawn(binaryPath, that.getBinaryArgs());
65-
// that.tunnel.on('exit', function(){
66-
// that.tunnel = undefined;
67-
// if(that.exitCallback) that.exitCallback();
68-
// });
69-
70-
// that.stdout = fs.openSync(that.logfile, 'r');
71-
// var chunkSize = 512,
72-
// buffer = new Buffer(81920),
73-
// bytesRead = 0,
74-
// error = undefined;
75-
76-
// while(true){
77-
// var bytes = fs.readSync(that.stdout, buffer, bytesRead, chunkSize, bytesRead);
78-
// if(bytes == 0) continue;
79-
80-
// var buffRead = buffer.slice(bytesRead, bytesRead+bytes);
81-
// bytesRead += bytes;
82-
83-
// var data = buffRead.toString();
84-
85-
// if(data.match(that.errorRegex)){
86-
// fs.closeSync(that.stdout);
87-
// error = data.match(that.errorRegex)[0].trim();
88-
// break;
89-
// }
90-
91-
// if(data.match(that.doneRegex)){
92-
// fs.closeSync(that.stdout);
93-
// break;
94-
// }
95-
// }
96-
97-
// if(error) throw new LocalError(error);
98-
// callback();
9965
});
10066
};
10167

10268
this.isRunning = function(){
103-
return this.pid && running(this.pid);
69+
return this.pid && running(this.pid) && this.isProcessRunning;
10470
};
10571

10672
this.stop = function (callback) {
10773
if(!this.pid) return callback();
108-
this.opcode = 'stop';
109-
this.tunnel = childProcess.execFile(this.binaryPath, this.getBinaryArgs(), function(error){
74+
this.killAllProcesses(function(error){
11075
if(error) callback(new LocalError(error.toString()));
11176
callback();
11277
});
@@ -287,5 +252,35 @@ function Local(){
287252
}
288253
return args;
289254
};
255+
256+
this.killAllProcesses = function(callback){
257+
psTree(this.pid, (err, children) => {
258+
var childPids = children.map(val => val.PID);
259+
var killChecker = setInterval(() => {
260+
if(childPids.length === 0) {
261+
clearInterval(killChecker);
262+
try {
263+
process.kill(this.pid);
264+
// This gives time to local binary to send kill signal to railsApp.
265+
setTimeout(() => {
266+
this.isProcessRunning = false;
267+
callback();
268+
}, 2000);
269+
} catch(err) {
270+
this.isProcessRunning = false;
271+
callback();
272+
}
273+
}
274+
for(var i in childPids) {
275+
try {
276+
process.kill(childPids[i]);
277+
} catch(err) {
278+
childPids.splice(i, 1);
279+
}
280+
}
281+
},500);
282+
});
283+
};
290284
}
285+
291286
module.exports = Local;

Diff for: package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "browserstack-local",
3-
"version": "1.3.6",
3+
"version": "1.3.7",
44
"description": "Nodejs bindings for BrowserStack Local",
55
"engine": "^0.10.44",
66
"main": "index.js",
@@ -19,6 +19,7 @@
1919
"dependencies": {
2020
"https-proxy-agent": "^2.2.1",
2121
"is-running": "^2.0.0",
22+
"ps-tree": "=1.1.1",
2223
"sinon": "^1.17.6",
2324
"temp-fs": "^0.9.9"
2425
},

0 commit comments

Comments
 (0)