Skip to content

Commit 477fafe

Browse files
committed
Merge branch 'cleanup' into pre_prod
2 parents ae8b284 + 7e8571b commit 477fafe

File tree

4 files changed

+53
-30
lines changed

4 files changed

+53
-30
lines changed

bin/cli.js

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,46 @@ var Log = require('../lib/logger'),
2323
logLevel,
2424
tunnel;
2525

26-
function cleanUp(signal) {
27-
try {
28-
server.close();
29-
} catch (e) {
30-
logger.debug("Server already closed");
31-
}
32-
33-
logger.info("Exiting");
34-
35-
for (var key in workers) {
36-
var worker = workers[key];
37-
if (workers.hasOwnProperty(key)) {
38-
client.terminateWorker(worker.id, function () {
39-
if (!workers[key]) {
40-
return;
41-
}
42-
26+
function terminateAllWorkers(callback) {
27+
var cleanWorker = function(id, key) {
28+
client.terminateWorker(id, function() {
29+
var worker = workers[key];
30+
if(worker) {
4331
logger.debug('[%s] Terminated', worker.string);
4432
clearTimeout(worker.activityTimeout);
4533
delete workers[key];
4634
delete workerKeys[worker.id];
47-
});
35+
}
36+
if (utils.objectSize(workers) === 0) {
37+
callback();
38+
}
39+
});
40+
};
41+
42+
if (utils.objectSize(workers) === 0) {
43+
callback();
44+
} else {
45+
for (var key in workers){
46+
var worker = workers[key];
47+
if (worker.id) {
48+
cleanWorker(worker.id, key);
49+
} else {
50+
delete workers[key];
51+
if (utils.objectSize(workers) === 0) {
52+
callback();
53+
}
54+
}
4855
}
4956
}
57+
};
58+
59+
function cleanUpAndExit(signal, status) {
60+
try {
61+
server.close();
62+
} catch (e) {
63+
logger.debug("Server already closed");
64+
}
65+
5066
if (statusPoller) statusPoller.stop();
5167

5268
try {
@@ -59,8 +75,15 @@ function cleanUp(signal) {
5975
} catch (e) {
6076
logger.debug("Non existent pid file.");
6177
}
62-
if (signal) {
63-
process.kill(process.pid, 'SIGTERM');
78+
79+
if (signal == 'SIGTERM') {
80+
logger.info("Exiting");
81+
process.exit(status);
82+
} else {
83+
terminateAllWorkers(function() {
84+
logger.info("Exiting");
85+
process.exit(1);
86+
});
6487
}
6588
}
6689

@@ -187,7 +210,7 @@ var statusPoller = {
187210
config.status = 1;
188211
}
189212

190-
process.exit(config.status);
213+
process.kill(process.pid, 'SIGTERM');
191214
}
192215
}
193216
}, activityTimeout * 1000);
@@ -208,7 +231,7 @@ var statusPoller = {
208231
config.status = 1;
209232
}
210233

211-
process.exit(config.status);
234+
process.kill(process.pid, 'SIGTERM');
212235
}
213236
}
214237
}, (activityTimeout * 1000));
@@ -262,8 +285,8 @@ try {
262285
runTests();
263286
var pid_file = process.cwd() + '/browserstack-run.pid';
264287
fs.writeFileSync(pid_file, process.pid, 'utf-8')
265-
process.on('exit', function() {cleanUp(false)});
266-
process.on('SIGINT', function() {cleanUp(true)});
288+
process.on('SIGINT', function() { cleanUpAndExit('SIGINT', 1) });
289+
process.on('SIGTERM', function() { cleanUpAndExit('SIGTERM', config.status) });
267290
}
268291
} catch (e) {
269292
console.log(e);

lib/local.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ var Tunnel = function Tunnel(key, port, uniqueIdentifier, callback, err) {
2929
logger.debug(stderr);
3030
logger.debug(error);
3131
if (stdout.indexOf('Error') >= 0 || error) {
32-
logger.info("[%s] Tunnel launching failed", new Date());
33-
logger.info(stdout);
34-
process.exit(1);
32+
logger.debug("[%s] Tunnel launching failed", new Date());
33+
logger.debug(stdout);
34+
process.kill(process.pid, 'SIGINT');
3535
}
3636
});
3737

@@ -82,7 +82,7 @@ var Tunnel = function Tunnel(key, port, uniqueIdentifier, callback, err) {
8282
}, 100);
8383
}).on('error', function(e) {
8484
logger.info("Got error while downloading binary: " + e.message);
85-
process.exit(1);
85+
process.kill(process.pid, 'SIGINT');
8686
});
8787
});
8888
});

lib/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ exports.Server = function Server(bsClient, workers) {
208208
config.status = 1;
209209
}
210210

211-
process.exit(config.status);
211+
process.kill(process.pid, 'SIGTERM');
212212
}
213213
});
214214
});

lib/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var alertBrowserStack = function alertBrowserStack(subject, content, params, fn)
4949
if (typeof params === 'function') {
5050
} else {
5151
fn = function() {
52-
process.exit(1);
52+
process.kill(process.pid, 'SIGINT');
5353
};
5454
}
5555
}

0 commit comments

Comments
 (0)