From 7995d98db5454a54219180b0a156da7368662e3a Mon Sep 17 00:00:00 2001 From: whyour Date: Fri, 24 Dec 2021 23:31:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=89=B9=E9=87=8F=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E4=BB=BB=E5=8A=A1=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/config/util.ts | 37 +++++++++++++++++++++++++++++++++++++ back/loaders/sentry.ts | 2 +- back/services/cron.ts | 10 +++++----- src/layouts/index.tsx | 2 +- 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/back/config/util.ts b/back/config/util.ts index 30f04a1e..174ec9ea 100644 --- a/back/config/util.ts +++ b/back/config/util.ts @@ -231,3 +231,40 @@ export async function fileExist(file: any) { } }); } + +export async function concurrentRun( + fnList: Array<() => Promise> = [], + max = 5, +) { + if (!fnList.length) return; + + const replyList: any[] = []; // 收集任务执行结果 + const startTime = new Date().getTime(); // 记录任务执行开始时间 + + // 任务执行程序 + const schedule = async (index: number) => { + return new Promise(async (resolve) => { + const fn = fnList[index]; + if (!fn) return resolve(null); + + // 执行当前异步任务 + const reply = await fn(); + replyList[index] = reply; + + // 执行完当前任务后,继续执行任务池的剩余任务 + await schedule(index + max); + resolve(null); + }); + }; + + // 任务池执行程序 + const scheduleList = new Array(max) + .fill(0) + .map((_, index) => schedule(index)); + + // 使用 Promise.all 批量执行 + const r = await Promise.all(scheduleList); + const cost = (new Date().getTime() - startTime) / 1000; + + return replyList; +} diff --git a/back/loaders/sentry.ts b/back/loaders/sentry.ts index aa53e600..6ef75224 100644 --- a/back/loaders/sentry.ts +++ b/back/loaders/sentry.ts @@ -5,7 +5,7 @@ import Logger from './logger'; export default ({ expressApp }: { expressApp: Application }) => { Sentry.init({ - dsn: 'https://e14681bce55f4849b11024a7d424b711@o1051273.ingest.sentry.io/6047906', + dsn: 'https://f4b5b55fb3c645b29a5dc2d70a1a4ef4@o1098464.ingest.sentry.io/6122819', integrations: [ new Sentry.Integrations.Http({ tracing: true }), new Tracing.Integrations.Express({ app: expressApp }), diff --git a/back/services/cron.ts b/back/services/cron.ts index a1da0adc..8d747cd1 100644 --- a/back/services/cron.ts +++ b/back/services/cron.ts @@ -6,7 +6,7 @@ import { Crontab, CrontabStatus } from '../data/cron'; import { exec, execSync, spawn } from 'child_process'; import fs from 'fs'; import cron_parser from 'cron-parser'; -import { getFileContentByName } from '../config/util'; +import { getFileContentByName, concurrentRun } from '../config/util'; import PQueue from 'p-queue'; import { promises, existsSync } from 'fs'; import { promisify } from 'util'; @@ -200,10 +200,10 @@ export default class CronService { { $set: { status: CrontabStatus.queued } }, { multi: true }, ); - for (let i = 0; i < ids.length; i++) { - const id = ids[i]; - this.queue.add(() => this.runSingle(id)); - } + concurrentRun( + ids.map((id) => () => this.runSingle(id)), + 10, + ); } public async stop(ids: string[]) { diff --git a/src/layouts/index.tsx b/src/layouts/index.tsx index 8eae7a63..8c68d0af 100644 --- a/src/layouts/index.tsx +++ b/src/layouts/index.tsx @@ -27,7 +27,7 @@ import * as Sentry from '@sentry/react'; import { Integrations } from '@sentry/tracing'; Sentry.init({ - dsn: 'https://ea2fede373244db99c536210b910d9da@o1051273.ingest.sentry.io/6047851', + dsn: 'https://3406424fb1dc4813a62d39e844a9d0ac@o1098464.ingest.sentry.io/6122818', integrations: [new Integrations.BrowserTracing()], release: version, tracesSampleRate: 1.0,