From 84828865f7d23d06cdb416c4988bd49992d93fb5 Mon Sep 17 00:00:00 2001 From: whyour Date: Sun, 18 Jun 2023 15:51:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BE=9D=E8=B5=96=E5=AE=89?= =?UTF-8?q?=E8=A3=85=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/services/cron.ts | 4 ++-- back/services/dependence.ts | 27 +++++++++++---------------- back/services/schedule.ts | 4 ++-- back/shared/pLimit.ts | 15 +++++++++++---- back/shared/runCron.ts | 4 ++-- src/pages/setting/index.tsx | 8 +++++++- 6 files changed, 35 insertions(+), 27 deletions(-) diff --git a/back/services/cron.ts b/back/services/cron.ts index 36853e29..1e3559f5 100644 --- a/back/services/cron.ts +++ b/back/services/cron.ts @@ -16,7 +16,7 @@ import { Op, where, col as colFn, FindOptions } from 'sequelize'; import path from 'path'; import { TASK_PREFIX, QL_PREFIX } from '../config/const'; import cronClient from '../schedule/client'; -import { runCronWithLimit } from '../shared/pLimit'; +import { runWithCpuLimit } from '../shared/pLimit'; import { spawn } from 'cross-spawn'; @Service() @@ -387,7 +387,7 @@ export default class CronService { } private async runSingle(cronId: number): Promise { - return runCronWithLimit(() => { + return runWithCpuLimit(() => { return new Promise(async (resolve: any) => { const cron = await this.getDb({ id: cronId }); if (cron.status !== CrontabStatus.queued) { diff --git a/back/services/dependence.ts b/back/services/dependence.ts index b0c05c1d..5323882f 100644 --- a/back/services/dependence.ts +++ b/back/services/dependence.ts @@ -14,14 +14,14 @@ import SockService from './sock'; import { FindOptions, Op } from 'sequelize'; import { concurrentRun } from '../config/util'; import dayjs from 'dayjs'; -import { runCronWithLimit } from '../shared/pLimit'; +import { runOneByOne, runWithCpuLimit } from '../shared/pLimit'; @Service() export default class DependenceService { constructor( @Inject('logger') private logger: winston.Logger, private sockService: SockService, - ) {} + ) { } public async create(payloads: Dependence[]): Promise { const tabs = payloads.map((x) => { @@ -106,7 +106,7 @@ export default class DependenceService { force: boolean = false, ) { docs.forEach((dep) => { - this.installOrUninstallDependencies([dep], isInstall, force); + this.installOrUninstallDependency(dep, isInstall, force); }); } @@ -142,19 +142,14 @@ export default class DependenceService { await DependenceModel.update({ log: newLog }, { where: { id: ids } }); } - public installOrUninstallDependencies( - dependencies: Dependence[], + public installOrUninstallDependency( + dependency: Dependence, isInstall: boolean = true, force: boolean = false, ) { - return runCronWithLimit(() => { + return runOneByOne(() => { return new Promise(async (resolve) => { - if (dependencies.length === 0) { - resolve(null); - return; - } - - const depIds = dependencies.map((x) => x.id) as number[]; + const depIds = [dependency.id!]; const status = isInstall ? DependenceStatus.installing : DependenceStatus.removing; @@ -163,16 +158,16 @@ export default class DependenceService { const socketMessageType = !force ? 'installDependence' : 'uninstallDependence'; - const depNames = dependencies.map((x) => x.name).join(' '); + const depName = dependency.name; const depRunCommand = ( isInstall ? InstallDependenceCommandTypes : unInstallDependenceCommandTypes - )[dependencies[0].type as any]; + )[dependency.type as any]; const actionText = isInstall ? '安装' : '删除'; const startTime = dayjs(); - const message = `开始${actionText}依赖 ${depNames},开始时间 ${startTime.format( + const message = `开始${actionText}依赖 ${depName},开始时间 ${startTime.format( 'YYYY-MM-DD HH:mm:ss', )}\n\n`; this.sockService.sendMessage({ @@ -182,7 +177,7 @@ export default class DependenceService { }); await this.updateLog(depIds, message); - const cp = spawn(`${depRunCommand} ${depNames}`, { + const cp = spawn(`${depRunCommand} ${depName}`, { shell: '/bin/bash', }); diff --git a/back/services/schedule.ts b/back/services/schedule.ts index b0723981..4eac4fc1 100644 --- a/back/services/schedule.ts +++ b/back/services/schedule.ts @@ -9,7 +9,7 @@ import { Task, } from 'toad-scheduler'; import dayjs from 'dayjs'; -import { runCronWithLimit } from '../shared/pLimit'; +import { runWithCpuLimit } from '../shared/pLimit'; import { spawn } from 'cross-spawn'; interface ScheduleTaskType { @@ -49,7 +49,7 @@ export default class ScheduleService { callbacks: TaskCallbacks = {}, completionTime: 'start' | 'end' = 'end', ) { - return runCronWithLimit(() => { + return runWithCpuLimit(() => { return new Promise(async (resolve, reject) => { try { const startTime = dayjs(); diff --git a/back/shared/pLimit.ts b/back/shared/pLimit.ts index b1e4730e..a582c72a 100644 --- a/back/shared/pLimit.ts +++ b/back/shared/pLimit.ts @@ -1,10 +1,17 @@ import pLimit from "p-limit"; import os from 'os'; -const cronLimit = pLimit(os.cpus().length); +const cpuLimit = pLimit(os.cpus().length); +const oneLimit = pLimit(1); -export function runCronWithLimit(fn: () => Promise): Promise { - return cronLimit(() => { +export function runWithCpuLimit(fn: () => Promise): Promise { + return cpuLimit(() => { return fn(); }); -} \ No newline at end of file +} + +export function runOneByOne(fn: () => Promise): Promise { + return oneLimit(() => { + return fn(); + }); +} diff --git a/back/shared/runCron.ts b/back/shared/runCron.ts index 771398e1..cd11bbff 100644 --- a/back/shared/runCron.ts +++ b/back/shared/runCron.ts @@ -1,9 +1,9 @@ import { spawn } from 'cross-spawn'; -import { runCronWithLimit } from "./pLimit"; +import { runWithCpuLimit } from "./pLimit"; import Logger from '../loaders/logger'; export function runCron(cmd: string): Promise { - return runCronWithLimit(() => { + return runWithCpuLimit(() => { return new Promise(async (resolve: any) => { Logger.silly('运行命令: ' + cmd); diff --git a/src/pages/setting/index.tsx b/src/pages/setting/index.tsx index 929a7d9f..4b8408c0 100644 --- a/src/pages/setting/index.tsx +++ b/src/pages/setting/index.tsx @@ -106,7 +106,7 @@ const Setting = () => { }, ]; - const [loading, setLoading] = useState(false); + const [loading, setLoading] = useState(true); const [dataSource, setDataSource] = useState([]); const [isModalVisible, setIsModalVisible] = useState(false); const [editedApp, setEditedApp] = useState(); @@ -253,6 +253,12 @@ const Setting = () => { }); }; + useEffect(() => { + if (isDemoEnv) { + getApps(); + } + }, []); + return (