Skip to content

Commit ca7920b

Browse files
committed
Add retries on error
1 parent 6bb6399 commit ca7920b

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

lib/Local.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var childProcess = require('child_process'),
2+
fs = require('fs'),
23
path = require('path'),
34
running = require('is-running'),
45
LocalBinary = require('./LocalBinary'),
@@ -7,16 +8,17 @@ var childProcess = require('child_process'),
78

89
function Local(){
910
this.pid = undefined;
11+
this.retriesLeft = 5;
1012
this.key = process.env.BROWSERSTACK_ACCESS_KEY;
1113
this.logfile = path.join(process.cwd(), 'local.log');
1214
this.opcode = 'start';
1315
this.exitCallback;
14-
this.userArgs = [];
1516

1617
this.errorRegex = /\*\*\* Error: [^\r\n]*/i;
1718
this.doneRegex = /Press Ctrl-C to exit/i;
1819

1920
this.start = function(options, callback){
21+
this.userArgs = [];
2022
var that = this;
2123
this.addArgs(options);
2224

@@ -29,7 +31,19 @@ function Local(){
2931

3032
that.opcode = 'start';
3133
that.tunnel = childProcess.execFile(that.binaryPath, that.getBinaryArgs(), function(error, stdout, stderr){
32-
if(error) callback(new LocalError(error.toString()));
34+
if(error) {
35+
console.error('Error while trying to execute binary', error);
36+
if(that.retriesLeft > 0) {
37+
console.log('Retrying Binary Download. Retries Left', that.retriesLeft);
38+
that.retriesLeft -= 1;
39+
fs.unlinkSync(that.binaryPath);
40+
delete(that.binaryPath);
41+
that.start(options, callback);
42+
return;
43+
} else {
44+
callback(new LocalError(error.toString()));
45+
}
46+
}
3347

3448
var data = {};
3549
if(stdout)
@@ -206,6 +220,7 @@ function Local(){
206220
}
207221
this.binary.binaryPath(conf, callback);
208222
} else {
223+
console.log('BINARY PATH IS DEFINED');
209224
callback(this.binaryPath);
210225
}
211226
};

lib/LocalBinary.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,23 @@ function LocalBinary(){
2222
this.httpPath = 'https://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal-linux-ia32';
2323
}
2424

25-
this.download = function(conf, destParentDir, callback){
25+
this.retryBinaryDownload = function(conf, destParentDir, callback, retries, binaryPath) {
26+
var that = this;
27+
if(retries > 0) {
28+
console.log('Retrying Download. Retries left', retries);
29+
fs.stat(binaryPath, function(err) {
30+
if(err == null) {
31+
fs.unlinkSync(binaryPath);
32+
}
33+
that.download(conf, destParentDir, callback, retries - 1);
34+
});
35+
} else {
36+
console.error('Number of retries to download exceeded.');
37+
}
38+
};
39+
40+
this.download = function(conf, destParentDir, callback, retries){
41+
var that = this;
2642
if(!this.checkPath(destParentDir))
2743
fs.mkdirSync(destParentDir);
2844

@@ -31,7 +47,7 @@ function LocalBinary(){
3147
var fileStream = fs.createWriteStream(binaryPath);
3248

3349
var options = url.parse(this.httpPath);
34-
if(conf.proxyHost && conf.proxyPort){
50+
if(conf.proxyHost && conf.proxyPort) {
3551
options.agent = new HttpsProxyAgent({
3652
host: conf.proxyHost,
3753
port: conf.proxyPort
@@ -40,14 +56,22 @@ function LocalBinary(){
4056

4157
https.get(options, function (response) {
4258
response.pipe(fileStream);
59+
response.on('error', function(err) {
60+
console.error('Got Error in binary download response', err);
61+
that.retryBinaryDownload(conf, destParentDir, callback, retries, binaryPath);
62+
});
4363
fileStream.on('error', function (err) {
4464
console.error('Got Error while downloading binary file', err);
65+
that.retryBinaryDownload(conf, destParentDir, callback, retries, binaryPath);
4566
});
4667
fileStream.on('close', function () {
4768
fs.chmod(binaryPath, '0755', function() {
4869
callback(binaryPath);
4970
});
5071
});
72+
}).on('error', function(err) {
73+
console.error('Got Error in binary downloading request', err);
74+
that.retryBinaryDownload(conf, destParentDir, callback, retries, binaryPath);
5175
});
5276
};
5377

@@ -58,7 +82,7 @@ function LocalBinary(){
5882
if(this.checkPath(binaryPath, fs.X_OK)){
5983
callback(binaryPath);
6084
} else {
61-
this.download(conf, destParentDir, callback);
85+
this.download(conf, destParentDir, callback, 5);
6286
}
6387
};
6488

0 commit comments

Comments
 (0)