chore: clear file

pull/21/merge
xiaojunnuo 2023-06-28 15:16:19 +08:00
parent f30afac47e
commit 620d1d4092
7 changed files with 46 additions and 4 deletions

View File

@ -8,7 +8,8 @@ services: # 要拉起的服务们
image: registry.cn-shenzhen.aliyuncs.com/handsfree/certd:${TAG}
container_name: certd # 容器名
restart: unless-stopped # 重启
volumes: # 挂载目录
volumes:
# ↓↓↓↓↓ 修改数据库以及证书存储路径
- /data/certd:/app/data
ports: # 端口映射
- "7001:7001"

View File

@ -34,10 +34,17 @@ export class FileStore {
}
private buildFilePath(filename: string) {
const parentDir = path.join(this.rootDir, this.scope + "", dayjs().format("YYYY-MM-DD"), this.parent + "");
const parentDir = path.join(this.rootDir, this.scope + "", this.parent + "", dayjs().format("YYYY-MM-DD"));
if (!fs.existsSync(parentDir)) {
fs.mkdirSync(parentDir, { recursive: true });
}
return path.join(parentDir, filename);
}
deleteByParent(scope: string, parent: string) {
const dir = path.join(this.rootDir, scope, parent);
if (fs.existsSync(dir)) {
fs.unlinkSync(dir);
}
}
}

View File

@ -2,3 +2,4 @@ export * from "./executor";
export * from "./run-history";
export * from "./context";
export * from "./storage";
export * from "./file-store";

View File

@ -78,6 +78,12 @@ export abstract class AbstractTaskPlugin implements ITaskPlugin {
this.ctx = ctx;
}
linkFile(file: FileItem) {
this._result.files!.push({
...file,
id: uuidv4(),
});
}
saveFile(filename: string, file: Buffer) {
const filePath = this.ctx.fileStore.writeFile(filename, file);
this._result.files!.push({

View File

@ -71,6 +71,9 @@ const development = {
expire: 7 * 24 * 60, //单位秒
},
},
certd: {
fileRootDir: '/app/data/files',
},
} as MidwayConfig;
mergeConfig(development, 'development');
export default development;

View File

@ -1,4 +1,4 @@
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/decorator';
import { Config, Inject, Provide, Scope, ScopeEnum } from '@midwayjs/decorator';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { BaseService } from '../../../basic/base-service';
@ -7,6 +7,8 @@ import { PipelineEntity } from '../entity/pipeline';
import { HistoryDetail } from '../entity/vo/history-detail';
import { HistoryLogService } from './history-log-service';
import { FileItem, Pipeline, RunnableCollection } from '@certd/pipeline';
import { FileStore } from '@certd/pipeline';
import { logger } from '../../../utils/logger';
/**
*
@ -18,6 +20,10 @@ export class HistoryService extends BaseService<HistoryEntity> {
repository: Repository<HistoryEntity>;
@Inject()
logService: HistoryLogService;
@Config('certd')
private certdConfig: any;
getRepository() {
return this.repository;
}
@ -60,6 +66,11 @@ export class HistoryService extends BaseService<HistoryEntity> {
}
let shouldDeleteCount = count - keepCount;
const deleteCountBatch = 100;
const fileStore = new FileStore({
rootDir: this.certdConfig.fileRootDir,
scope: pipelineId + '',
parent: '0',
});
while (shouldDeleteCount > 0) {
const list = await this.repository.find({
select: {
@ -76,6 +87,15 @@ export class HistoryService extends BaseService<HistoryEntity> {
});
await this.repository.remove(list);
for (const historyEntity of list) {
const id = historyEntity.id;
try {
fileStore.deleteByParent(pipelineId + '', id + '');
} catch (e) {
logger.error('删除文件失败', e);
}
}
shouldDeleteCount -= deleteCountBatch;
}
}

View File

@ -1,4 +1,4 @@
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/decorator';
import { Config, Inject, Provide, Scope, ScopeEnum } from '@midwayjs/decorator';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { In, Repository } from 'typeorm';
import { BaseService } from '../../../basic/base-service';
@ -38,6 +38,9 @@ export class PipelineService extends BaseService<PipelineEntity> {
@Inject()
cron: Cron;
@Config('certd')
private certdConfig: any;
getRepository() {
return this.repository;
}
@ -213,6 +216,7 @@ export class PipelineService extends BaseService<PipelineEntity> {
accessService: this.accessService,
storage: new DbStorage(userId, this.storageService),
emailService: this.emailService,
fileRootDir: this.certdConfig.fileRootDir,
});
try {
await executor.init();