mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
refactor: dont sleep if iteration is done on first one
This commit is contained in:
23
src/batch.js
23
src/batch.js
@@ -40,20 +40,20 @@ exports.processSortedSet = async function (setKey, process, options) {
|
|||||||
process = util.promisify(process);
|
process = util.promisify(process);
|
||||||
}
|
}
|
||||||
const method = options.reverse ? 'getSortedSetRevRange' : 'getSortedSetRange';
|
const method = options.reverse ? 'getSortedSetRevRange' : 'getSortedSetRange';
|
||||||
|
let iteration = 1;
|
||||||
while (true) {
|
while (true) {
|
||||||
/* eslint-disable no-await-in-loop */
|
/* eslint-disable no-await-in-loop */
|
||||||
const ids = await db[`${method}${options.withScores ? 'WithScores' : ''}`](setKey, start, stop);
|
const ids = await db[`${method}${options.withScores ? 'WithScores' : ''}`](setKey, start, stop);
|
||||||
if (!ids.length || options.doneIf(start, stop, ids)) {
|
if (!ids.length || options.doneIf(start, stop, ids)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await process(ids);
|
if (iteration > 1 && options.interval) {
|
||||||
|
|
||||||
start += utils.isNumber(options.alwaysStartAt) ? options.alwaysStartAt : options.batch;
|
|
||||||
stop = start + options.batch - 1;
|
|
||||||
|
|
||||||
if (options.interval) {
|
|
||||||
await sleep(options.interval);
|
await sleep(options.interval);
|
||||||
}
|
}
|
||||||
|
await process(ids);
|
||||||
|
iteration += 1;
|
||||||
|
start += utils.isNumber(options.alwaysStartAt) ? options.alwaysStartAt : options.batch;
|
||||||
|
stop = start + options.batch - 1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -72,21 +72,20 @@ exports.processArray = async function (array, process, options) {
|
|||||||
if (process && process.constructor && process.constructor.name !== 'AsyncFunction') {
|
if (process && process.constructor && process.constructor.name !== 'AsyncFunction') {
|
||||||
process = util.promisify(process);
|
process = util.promisify(process);
|
||||||
}
|
}
|
||||||
|
let iteration = 1;
|
||||||
while (true) {
|
while (true) {
|
||||||
const currentBatch = array.slice(start, start + batch);
|
const currentBatch = array.slice(start, start + batch);
|
||||||
|
|
||||||
if (!currentBatch.length) {
|
if (!currentBatch.length) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (iteration > 1 && options.interval) {
|
||||||
|
await sleep(options.interval);
|
||||||
|
}
|
||||||
await process(currentBatch);
|
await process(currentBatch);
|
||||||
|
|
||||||
start += batch;
|
start += batch;
|
||||||
|
iteration += 1;
|
||||||
if (options.interval) {
|
|
||||||
await sleep(options.interval);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user