修改并发逻辑,系统设置增加定时任务并发设置

This commit is contained in:
whyour
2023-07-01 15:26:20 +08:00
parent db227e56bf
commit 702c3160ec
18 changed files with 163 additions and 88 deletions
+31 -11
View File
@@ -1,17 +1,37 @@
import pLimit from "p-limit";
import os from 'os';
import { AuthDataType, AuthModel } from "../data/auth";
const cpuLimit = pLimit(os.cpus().length);
const oneLimit = pLimit(1);
class TaskLimit {
private oneLimit = pLimit(1);
private cpuLimit = pLimit(Math.max(os.cpus().length, 4));
export function runWithCpuLimit<T>(fn: () => Promise<T>): Promise<T> {
return cpuLimit(() => {
return fn();
});
constructor() {
this.setCustomLimit();
}
public async setCustomLimit(limit?: number) {
if (limit) {
this.cpuLimit = pLimit(limit);
return;
}
const doc = await AuthModel.findOne({ where: { type: AuthDataType.systemConfig } });
if (doc?.info?.cronConcurrency) {
this.cpuLimit = pLimit(doc?.info?.cronConcurrency);
}
}
public runWithCpuLimit<T>(fn: () => Promise<T>): Promise<T> {
return this.cpuLimit(() => {
return fn();
});
}
public runOneByOne<T>(fn: () => Promise<T>): Promise<T> {
return this.oneLimit(() => {
return fn();
});
}
}
export function runOneByOne<T>(fn: () => Promise<T>): Promise<T> {
return oneLimit(() => {
return fn();
});
}
export default new TaskLimit();