依赖管理支持取消安装和状态筛选

This commit is contained in:
whyour
2024-02-13 22:42:22 +08:00
parent 892d91edc4
commit 14cb1f7788
9 changed files with 209 additions and 51 deletions
+39 -6
View File
@@ -14,7 +14,12 @@ import {
import { spawn } from 'cross-spawn';
import SockService from './sock';
import { FindOptions, Op } from 'sequelize';
import { fileExist, promiseExecSuccess } from '../config/util';
import {
fileExist,
getPid,
killTask,
promiseExecSuccess,
} from '../config/util';
import dayjs from 'dayjs';
import taskLimit from '../shared/pLimit';
@@ -86,11 +91,21 @@ export default class DependenceService {
}
public async dependencies(
{ searchValue, type }: { searchValue: string; type: string },
sort: any = { position: -1 },
{
searchValue,
type,
status,
}: { searchValue: string; type: string; status: string },
sort: any = [],
query: any = {},
): Promise<Dependence[]> {
let condition = { ...query, type: DependenceTypes[type as any] };
let condition = {
...query,
type: DependenceTypes[type as any],
};
if (status) {
condition.status = status.split(',').map(Number);
}
if (searchValue) {
const encodeText = encodeURI(searchValue);
const reg = {
@@ -106,7 +121,7 @@ export default class DependenceService {
};
}
try {
const result = await this.find(condition);
const result = await this.find(condition, sort);
return result as any;
} catch (error) {
throw error;
@@ -134,6 +149,18 @@ export default class DependenceService {
return docs;
}
public async cancel(ids: number[]) {
const docs = await DependenceModel.findAll({ where: { id: ids } });
for (const doc of docs) {
taskLimit.removeQueuedDependency(doc);
const depRunCommand = InstallDependenceCommandTypes[doc.type];
const cmd = `${depRunCommand} ${doc.name.trim()}`;
const pid = await getPid(cmd);
pid && (await killTask(pid));
}
await this.removeDb(ids);
}
private async find(query: any, sort: any = []): Promise<Dependence[]> {
const docs = await DependenceModel.findAll({
where: { ...query },
@@ -168,8 +195,14 @@ export default class DependenceService {
isInstall: boolean = true,
force: boolean = false,
) {
return taskLimit.runOneByOne(() => {
return taskLimit.runDependeny(dependency, () => {
return new Promise(async (resolve) => {
if (taskLimit.firstDependencyId !== dependency.id) {
return resolve(null);
}
taskLimit.removeQueuedDependency(dependency);
const depIds = [dependency.id!];
const status = isInstall
? DependenceStatus.installing