Skip to content

Commit

Permalink
Add automatic retry behavior in the event of a download error
Browse files Browse the repository at this point in the history
This patch adds retry behavior designed to handle intermittent
ECONNRESET and other connection errors during download operations.

The retry will be limited to 3 attempts, and each subsequent retry
will have an increasing delay to give time for the problem to
resolve itself. After the third failure it will give up on the URL
and move on to the next.

For issue: #22
  • Loading branch information
stevenbenner committed Feb 5, 2022
1 parent 0f66835 commit 0357eb2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
9 changes: 8 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,15 @@ module.exports = {
console.log('[' + index + '] Saved: ' + fileName);
index--;
});
dl.on('fail', function(fileName) {
dl.on('retry', function(fileName, retryCount, retryDelay, isFinalRetry) {
console.log('[' + index + '] Connection error while attempting to download file ' + fileName + '!');
console.log(' Waiting ' + retryDelay + ' seconds before ' + (isFinalRetry ? 'final attempt...' : 'retrying download...'));
});
dl.on('fail', function(fileName, err) {
console.log('[' + index + '] Failed: ' + fileName);
if (err) {
console.log(' Error: [' + err.code + '] ' + err.message);
}
index--;
});
dl.on('end', function() {
Expand Down
27 changes: 24 additions & 3 deletions lib/downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var resPaths = require('./respaths.json');

var USERAGENT = 'Mozilla/4.8 [en] (Windows NT 6.0; U)';
var HOST = 'interfacelift.com';
var RETRY_LIMIT = 3;

module.exports = function(uriList, downloadPath, resolution) {
var fileNameRegEx = new RegExp('\\d+_\\w+_' + resolution + '\\.jpg', 'i');
Expand Down Expand Up @@ -57,16 +58,16 @@ module.exports = function(uriList, downloadPath, resolution) {
}
}

function failFile(fileName) {
me.emit('fail', fileName);
function failFile(fileName, err) {
me.emit('fail', fileName, err);
setImmediate(runNextUri);
}

this.start = function() {
setImmediate(runNextUri);
};

this.on('next', function(uri) {
this.on('next', function(uri, retryCount) {
var fileName = fileNameRegEx.exec(uri);
var filePath = path.join(downloadPath, fileName[0]);
var requestConfig = {
Expand All @@ -78,6 +79,8 @@ module.exports = function(uriList, downloadPath, resolution) {
Referer: 'https://' + HOST + resPaths[resolution]
}
};
var retryDelay = 0;
var isFinalRetry = false;

// skip the file if it already exists
if (fs.existsSync(filePath)) {
Expand All @@ -101,6 +104,24 @@ module.exports = function(uriList, downloadPath, resolution) {
// 400 and 500 status codes
failFile(fileName);
}
}).on('error', function(err) {
// retry delay algorithm: 4^c seconds
// delay will be 4 seconds, then 16 seconds, then 64 seconds
retryCount = retryCount || 0;
retryCount++;
retryDelay = Math.pow(4, retryCount);
isFinalRetry = retryCount === RETRY_LIMIT;

if (retryCount <= RETRY_LIMIT) {
// wait for the specified time, then retry
me.emit('retry', fileName, retryCount, retryDelay, isFinalRetry);
setTimeout(function() {
me.emit('next', uri, retryCount);
}, retryDelay * 1000);
} else {
// give up
failFile(fileName, err);
}
});
});
};
Expand Down

0 comments on commit 0357eb2

Please sign in to comment.