Skip to content

Commit b02cf14

Browse files
committed
Overriding stop method for fixing stop local issue in docker
1 parent cc6a088 commit b02cf14

File tree

2 files changed

+28
-41
lines changed

2 files changed

+28
-41
lines changed

lib/Local.js

+27-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,6 +252,27 @@ 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+
process.kill(this.pid);
263+
this.isProcessRunning = false;
264+
callback();
265+
}
266+
for(var i in childPids) {
267+
try{
268+
process.kill(childPids[i]);
269+
}catch(err){
270+
childPids.splice(i, 1);
271+
}
272+
}
273+
},100);
274+
});
275+
};
290276
}
291277

292278
module.exports = Local;

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"dependencies": {
2020
"https-proxy-agent": "^2.2.1",
2121
"is-running": "^2.0.0",
22+
"ps-tree": "^1.1.0",
2223
"sinon": "^1.17.6",
2324
"temp-fs": "^0.9.9"
2425
},

0 commit comments

Comments
 (0)