Try to keep up the concurrent downloads

This commit is contained in:
X3F200C 2024-11-09 02:22:38 -05:00
parent 6764bb6e07
commit a748c07a8d

View File

@ -57,12 +57,31 @@ function downloadFile(url, filePath) {
}); });
} }
function processDownloadQueue(baseDirectory, fileEntries) { function processDownloadQueue(baseDirectory, baseURL, fileEntries) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
let runningDownloads = 0; let runningDownloads = 0;
let queue = [...fileEntries];
let finishCallback = function () {
if (queue.length > 0) {
}
};
for (let d = 0; d < module.exports.concurrentDownloads; d++) { 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) { return new Promise(function (resolve, reject) {
processDownloadQueue(baseDirectory, fileEntries.filter(function (entry) { processDownloadQueue(baseDirectory, baseURL, fileEntries.filter(function (entry) {
return !entry.ok; return !entry.ok;
})).then(function () { })).then(function () {
resolve(true); resolve(true);