update task pool
This commit is contained in:
@@ -6,7 +6,13 @@
|
|||||||
* @param {*} _limit
|
* @param {*} _limit
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export const taskPool = (fn, preFn, _interval = 100, _limit = 100) => {
|
export const taskPool = (
|
||||||
|
fn,
|
||||||
|
preFn,
|
||||||
|
_interval = 100,
|
||||||
|
_limit = 100,
|
||||||
|
_retryInteral = 1000
|
||||||
|
) => {
|
||||||
const pool = [];
|
const pool = [];
|
||||||
const maxRetry = 2; // 最大重试次数
|
const maxRetry = 2; // 最大重试次数
|
||||||
let maxCount = _limit; // 最大数量
|
let maxCount = _limit; // 最大数量
|
||||||
@@ -14,23 +20,6 @@ export const taskPool = (fn, preFn, _interval = 100, _limit = 100) => {
|
|||||||
let interval = _interval; // 间隔时间
|
let interval = _interval; // 间隔时间
|
||||||
let timer = null;
|
let timer = null;
|
||||||
|
|
||||||
const handleTask = async (item, preArgs) => {
|
|
||||||
curCount++;
|
|
||||||
const { args, resolve, reject, retry } = item;
|
|
||||||
try {
|
|
||||||
const res = await fn({ ...args, ...preArgs });
|
|
||||||
resolve(res);
|
|
||||||
} catch (err) {
|
|
||||||
if (retry < maxRetry) {
|
|
||||||
pool.push({ args, resolve, reject, retry: retry + 1 });
|
|
||||||
} else {
|
|
||||||
reject(err);
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
curCount--;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const run = async () => {
|
const run = async () => {
|
||||||
// console.log("timer", timer);
|
// console.log("timer", timer);
|
||||||
timer && clearTimeout(timer);
|
timer && clearTimeout(timer);
|
||||||
@@ -39,12 +28,24 @@ export const taskPool = (fn, preFn, _interval = 100, _limit = 100) => {
|
|||||||
if (curCount < maxCount) {
|
if (curCount < maxCount) {
|
||||||
const item = pool.shift();
|
const item = pool.shift();
|
||||||
if (item) {
|
if (item) {
|
||||||
|
curCount++;
|
||||||
|
const { args, resolve, reject, retry } = item;
|
||||||
try {
|
try {
|
||||||
const preArgs = await preFn(item.args);
|
const preArgs = await preFn(item.args);
|
||||||
handleTask(item, preArgs);
|
const res = await fn({ ...args, ...preArgs });
|
||||||
|
resolve(res);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log("[preFn]", err);
|
console.log("[task]", retry, err);
|
||||||
pool.push(item);
|
if (retry < maxRetry) {
|
||||||
|
const retryTimer = setTimeout(() => {
|
||||||
|
clearTimeout(retryTimer);
|
||||||
|
pool.push({ args, resolve, reject, retry: retry + 1 });
|
||||||
|
}, _retryInteral);
|
||||||
|
} else {
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
curCount--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user