修复依赖安装流程

This commit is contained in:
whyour 2023-06-18 15:51:35 +08:00
parent 87455cebc0
commit 84828865f7
6 changed files with 35 additions and 27 deletions

View File

@ -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<number> {
return runCronWithLimit(() => {
return runWithCpuLimit(() => {
return new Promise(async (resolve: any) => {
const cron = await this.getDb({ id: cronId });
if (cron.status !== CrontabStatus.queued) {

View File

@ -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<Dependence[]> {
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',
});

View File

@ -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();

View File

@ -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<T>(fn: () => Promise<T>): Promise<T> {
return cronLimit(() => {
export function runWithCpuLimit<T>(fn: () => Promise<T>): Promise<T> {
return cpuLimit(() => {
return fn();
});
}
}
export function runOneByOne<T>(fn: () => Promise<T>): Promise<T> {
return oneLimit(() => {
return fn();
});
}

View File

@ -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<number> {
return runCronWithLimit(() => {
return runWithCpuLimit(() => {
return new Promise(async (resolve: any) => {
Logger.silly('运行命令: ' + cmd);

View File

@ -106,7 +106,7 @@ const Setting = () => {
},
];
const [loading, setLoading] = useState(false);
const [loading, setLoading] = useState(true);
const [dataSource, setDataSource] = useState<any[]>([]);
const [isModalVisible, setIsModalVisible] = useState(false);
const [editedApp, setEditedApp] = useState<any>();
@ -253,6 +253,12 @@ const Setting = () => {
});
};
useEffect(() => {
if (isDemoEnv) {
getApps();
}
}, []);
return (
<PageContainer
className="ql-container-wrapper ql-container-wrapper-has-tab ql-setting-container"