Try to keep up the concurrent downloads
This commit is contained in:
parent
6764bb6e07
commit
a748c07a8d
@ -57,12 +57,31 @@ function downloadFile(url, filePath) {
|
||||
});
|
||||
}
|
||||
|
||||
function processDownloadQueue(baseDirectory, fileEntries) {
|
||||
function processDownloadQueue(baseDirectory, baseURL, fileEntries) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
let runningDownloads = 0;
|
||||
let queue = [...fileEntries];
|
||||
|
||||
let finishCallback = function () {
|
||||
if (queue.length > 0) {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
for (let d = 0; d < module.exports.concurrentDownloads; d++) {
|
||||
if (queue.length < 1) break;
|
||||
|
||||
let currentEntry = queue.shift();
|
||||
|
||||
downloadFile(baseURL + currentEntry.hash, path.join(baseDirectory, currentEntry.name)).then(finishCallback).catch((function (error) {
|
||||
if (!this.retries) {
|
||||
this.retries = 0;
|
||||
}
|
||||
if (this.retries > 3) return reject(error);
|
||||
|
||||
this.retries++;
|
||||
queue.push(this);
|
||||
}).bind(currentEntry));
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -86,9 +105,9 @@ module.exports = {
|
||||
});
|
||||
});
|
||||
},
|
||||
"downloadFiles": function (baseDirectory, fileEntries) {
|
||||
"downloadFiles": function (baseDirectory, baseURL, fileEntries) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
processDownloadQueue(baseDirectory, fileEntries.filter(function (entry) {
|
||||
processDownloadQueue(baseDirectory, baseURL, fileEntries.filter(function (entry) {
|
||||
return !entry.ok;
|
||||
})).then(function () {
|
||||
resolve(true);
|
||||
|
Loading…
Reference in New Issue
Block a user