-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathbatchExecute.js
51 lines (45 loc) · 1.77 KB
/
batchExecute.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import fastq from 'fastq';
/**
* Batch execution function with concurrency control.
* @param {Array} tasks - Array of task objects, each containing func, param, and id.
* @param {Object} listener - Progress listener object containing func and param.
* @param {number} [successCount] - Number of successful tasks to wait for before stopping.
* @returns {Promise<void>} - Resolves when the required tasks are complete or all tasks are processed.
*/
async function batchExecute(tasks, listener, successCount) {
const maxConcurrency = 16; // Maximum number of concurrent tasks
let completedSuccess = 0;
let stopExecution = false;
const queue = fastq.promise(async (task) => {
if (stopExecution) return;
const {func, param, id} = task;
try {
const result = await func(param);
if (listener && typeof listener.func === 'function') {
const listenerResult = listener.func(listener.param, id, null, result);
if (listenerResult === 'break') {
stopExecution = true;
}
}
completedSuccess++;
if (successCount && completedSuccess >= successCount) {
stopExecution = true;
}
} catch (error) {
if (listener && typeof listener.func === 'function') {
listener.func(listener.param, id, error, null);
}
}
}, maxConcurrency);
// Enqueue all tasks
tasks.forEach((task) => {
if (!stopExecution) {
queue.push(task).catch((err) => {
console.error(`Task queue error for task ${task.id}:`, err);
});
}
});
// Wait for all tasks to complete
await queue.drained();
}
export default batchExecute;