2022-10-26 09:02:47 +08:00
|
|
|
import { Registrable } from "../registry";
|
2023-06-25 23:25:56 +08:00
|
|
|
import { FileItem, FormItemProps, Pipeline, Runnable, Step } from "../d.ts";
|
|
|
|
|
import { FileStore } from "../core/file-store";
|
|
|
|
|
import { Logger } from "log4js";
|
|
|
|
|
import { IAccessService } from "../access";
|
|
|
|
|
import { IEmailService } from "../service";
|
|
|
|
|
import { IContext } from "../core";
|
|
|
|
|
import { AxiosInstance } from "axios";
|
2024-05-30 10:12:48 +08:00
|
|
|
import { logger } from "../utils";
|
2023-06-28 22:10:52 +08:00
|
|
|
|
2022-10-26 09:02:47 +08:00
|
|
|
export enum ContextScope {
|
|
|
|
|
global,
|
|
|
|
|
pipeline,
|
|
|
|
|
runtime,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type TaskOutputDefine = {
|
|
|
|
|
title: string;
|
|
|
|
|
value?: any;
|
|
|
|
|
};
|
2023-06-25 23:25:56 +08:00
|
|
|
|
2022-10-26 09:02:47 +08:00
|
|
|
export type TaskInputDefine = FormItemProps;
|
|
|
|
|
|
|
|
|
|
export type PluginDefine = Registrable & {
|
2022-12-27 12:32:09 +08:00
|
|
|
default?: any;
|
2023-01-07 23:22:02 +08:00
|
|
|
input?: {
|
2022-10-26 09:02:47 +08:00
|
|
|
[key: string]: TaskInputDefine;
|
|
|
|
|
};
|
2023-01-07 23:22:02 +08:00
|
|
|
output?: {
|
2022-10-26 09:02:47 +08:00
|
|
|
[key: string]: TaskOutputDefine;
|
|
|
|
|
};
|
2022-12-27 12:32:09 +08:00
|
|
|
|
2023-01-07 23:22:02 +08:00
|
|
|
autowire?: {
|
|
|
|
|
[key: string]: any;
|
|
|
|
|
};
|
2023-06-25 16:25:23 +08:00
|
|
|
|
|
|
|
|
reference?: {
|
|
|
|
|
src: string;
|
|
|
|
|
dest: string;
|
|
|
|
|
type: "computed";
|
|
|
|
|
}[];
|
2022-10-26 09:02:47 +08:00
|
|
|
};
|
|
|
|
|
|
2023-05-24 15:41:35 +08:00
|
|
|
export type ITaskPlugin = {
|
2023-05-09 10:16:49 +08:00
|
|
|
onInstance(): Promise<void>;
|
2022-12-27 12:32:09 +08:00
|
|
|
execute(): Promise<void>;
|
2023-05-24 15:41:35 +08:00
|
|
|
[key: string]: any;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type TaskResult = {
|
|
|
|
|
clearLastStatus?: boolean;
|
2023-06-25 23:25:56 +08:00
|
|
|
files?: FileItem[];
|
2023-05-24 15:41:35 +08:00
|
|
|
};
|
2023-06-25 23:25:56 +08:00
|
|
|
export type TaskInstanceContext = {
|
|
|
|
|
pipeline: Pipeline;
|
|
|
|
|
step: Step;
|
|
|
|
|
logger: Logger;
|
|
|
|
|
accessService: IAccessService;
|
|
|
|
|
emailService: IEmailService;
|
|
|
|
|
pipelineContext: IContext;
|
|
|
|
|
userContext: IContext;
|
|
|
|
|
http: AxiosInstance;
|
|
|
|
|
fileStore: FileStore;
|
|
|
|
|
lastStatus?: Runnable;
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-24 15:41:35 +08:00
|
|
|
export abstract class AbstractTaskPlugin implements ITaskPlugin {
|
2023-06-25 23:25:56 +08:00
|
|
|
_result: TaskResult = { clearLastStatus: false, files: [] };
|
|
|
|
|
ctx!: TaskInstanceContext;
|
2023-05-24 15:41:35 +08:00
|
|
|
clearLastStatus() {
|
2023-06-25 23:25:56 +08:00
|
|
|
this._result.clearLastStatus = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getFiles() {
|
|
|
|
|
return this._result.files;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setCtx(ctx: TaskInstanceContext) {
|
|
|
|
|
this.ctx = ctx;
|
2023-05-24 15:41:35 +08:00
|
|
|
}
|
2023-06-25 23:25:56 +08:00
|
|
|
|
2023-06-28 23:15:37 +08:00
|
|
|
randomFileId() {
|
|
|
|
|
return Math.random().toString(36).substring(2, 9);
|
|
|
|
|
}
|
2023-06-28 15:16:19 +08:00
|
|
|
linkFile(file: FileItem) {
|
|
|
|
|
this._result.files!.push({
|
|
|
|
|
...file,
|
2023-06-28 23:15:37 +08:00
|
|
|
id: this.randomFileId(),
|
2023-06-28 15:16:19 +08:00
|
|
|
});
|
|
|
|
|
}
|
2023-06-25 23:25:56 +08:00
|
|
|
saveFile(filename: string, file: Buffer) {
|
|
|
|
|
const filePath = this.ctx.fileStore.writeFile(filename, file);
|
2024-05-30 10:12:48 +08:00
|
|
|
logger.info(`saveFile:${filePath}`);
|
2023-06-25 23:25:56 +08:00
|
|
|
this._result.files!.push({
|
2023-06-28 23:15:37 +08:00
|
|
|
id: this.randomFileId(),
|
2023-06-25 23:25:56 +08:00
|
|
|
filename,
|
|
|
|
|
path: filePath,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get pipeline() {
|
|
|
|
|
return this.ctx.pipeline;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get step() {
|
|
|
|
|
return this.ctx.step;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-24 15:41:35 +08:00
|
|
|
async onInstance(): Promise<void> {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-06-25 23:25:56 +08:00
|
|
|
|
2023-05-24 15:41:35 +08:00
|
|
|
abstract execute(): Promise<void>;
|
2022-10-26 09:02:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type OutputVO = {
|
|
|
|
|
key: string;
|
|
|
|
|
title: string;
|
|
|
|
|
value: any;
|
|
|
|
|
};
|