chore(plugin-workflow): add comments (#3959)

This commit is contained in:
Junyi 2024-04-08 12:18:08 +08:00 committed by GitHub
parent a712d27260
commit 9f72b4e1ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 91 additions and 26 deletions

View File

@ -9,6 +9,9 @@
"main": "./dist/server/index.js", "main": "./dist/server/index.js",
"homepage": "https://docs.nocobase.com/handbook/workflow-request", "homepage": "https://docs.nocobase.com/handbook/workflow-request",
"homepage.zh-CN": "https://docs-cn.nocobase.com/handbook/workflow-request", "homepage.zh-CN": "https://docs-cn.nocobase.com/handbook/workflow-request",
"dependencies": {
"axios": "^0.26.1"
},
"devDependencies": { "devDependencies": {
"antd": "5.x", "antd": "5.x",
"react": "18.x", "react": "18.x",

View File

@ -19,7 +19,6 @@
"@formily/react": "2.x", "@formily/react": "2.x",
"@types/ejs": "^3.1.1", "@types/ejs": "^3.1.1",
"antd": "5.x", "antd": "5.x",
"axios": "^0.26.1",
"classnames": "^2.3.1", "classnames": "^2.3.1",
"cron-parser": "4.4.0", "cron-parser": "4.4.0",
"dayjs": "^1.11.8", "dayjs": "^1.11.8",

View File

@ -58,8 +58,20 @@ export default class PluginWorkflowClient extends Plugin {
} }
async load() { async load() {
this.addRoutes(); this.app.router.add('admin.workflow.workflows.id', {
this.addComponents(); path: getWorkflowDetailPath(':id'),
element: <WorkflowPage />,
});
this.app.router.add('admin.workflow.executions.id', {
path: getWorkflowExecutionsPath(':id'),
element: <ExecutionPage />,
});
this.app.addComponents({
WorkflowPage,
ExecutionPage,
});
this.app.pluginSettingsManager.add(NAMESPACE, { this.app.pluginSettingsManager.add(NAMESPACE, {
icon: 'PartitionOutlined', icon: 'PartitionOutlined',
@ -82,24 +94,6 @@ export default class PluginWorkflowClient extends Plugin {
this.registerInstruction('update', UpdateInstruction); this.registerInstruction('update', UpdateInstruction);
this.registerInstruction('destroy', DestroyInstruction); this.registerInstruction('destroy', DestroyInstruction);
} }
addComponents() {
this.app.addComponents({
WorkflowPage,
ExecutionPage,
});
}
addRoutes() {
this.app.router.add('admin.workflow.workflows.id', {
path: getWorkflowDetailPath(':id'),
element: <WorkflowPage />,
});
this.app.router.add('admin.workflow.executions.id', {
path: getWorkflowExecutionsPath(':id'),
element: <ExecutionPage />,
});
}
} }
export * from './Branch'; export * from './Branch';

View File

@ -90,6 +90,9 @@ export function useNodeContext() {
return useContext(NodeContext); return useContext(NodeContext);
} }
/**
* @experimental
*/
export function useAvailableUpstreams(node, filter?) { export function useAvailableUpstreams(node, filter?) {
const stack: any[] = []; const stack: any[] = [];
if (!node) { if (!node) {
@ -104,6 +107,9 @@ export function useAvailableUpstreams(node, filter?) {
return stack; return stack;
} }
/**
* @experimental
*/
export function useUpstreamScopes(node) { export function useUpstreamScopes(node) {
const stack: any[] = []; const stack: any[] = [];
if (!node) { if (!node) {

View File

@ -330,6 +330,9 @@ export const TriggerConfig = () => {
); );
}; };
/**
* @experimental
*/
export function useTrigger() { export function useTrigger() {
const workflowPlugin = usePlugin(WorkflowPlugin); const workflowPlugin = usePlugin(WorkflowPlugin);
const { workflow } = useFlowContext(); const { workflow } = useFlowContext();

View File

@ -260,7 +260,7 @@ export default class PluginWorkflowServer extends Plugin {
}); });
} }
toggle(workflow: WorkflowModel, enable?: boolean) { private toggle(workflow: WorkflowModel, enable?: boolean) {
const type = workflow.get('type'); const type = workflow.get('type');
const trigger = this.triggers.get(type); const trigger = this.triggers.get(type);
if (!trigger) { if (!trigger) {
@ -511,6 +511,13 @@ export default class PluginWorkflowServer extends Plugin {
return processor; return processor;
} }
/**
* @experimental
* @param {string} dataSourceName
* @param {Transaction} transaction
* @param {boolean} create
* @returns {Trasaction}
*/
useDataSourceTransaction(dataSourceName = 'main', transaction, create = false) { useDataSourceTransaction(dataSourceName = 'main', transaction, create = false) {
// @ts-ignore // @ts-ignore
const { db } = this.app.dataSourceManager.dataSources.get(dataSourceName).collectionManager; const { db } = this.app.dataSourceManager.dataSources.get(dataSourceName).collectionManager;

View File

@ -25,11 +25,35 @@ export default class Processor {
}; };
logger: Logger; logger: Logger;
/**
* @experimental
*/
transaction: Transaction; transaction: Transaction;
/**
* @experimental
*/
nodes: FlowNodeModel[] = []; nodes: FlowNodeModel[] = [];
/**
* @experimental
*/
nodesMap = new Map<number, FlowNodeModel>(); nodesMap = new Map<number, FlowNodeModel>();
/**
* @experimental
*/
jobsMap = new Map<number, JobModel>(); jobsMap = new Map<number, JobModel>();
/**
* @experimental
*/
jobsMapByNodeKey: { [key: string]: any } = {}; jobsMapByNodeKey: { [key: string]: any } = {};
/**
* @experimental
*/
lastSavedJob: JobModel | null = null; lastSavedJob: JobModel | null = null;
constructor( constructor(
@ -194,7 +218,7 @@ export default class Processor {
return this.exit(job.status); return this.exit(job.status);
} }
async recall(node, job) { private async recall(node, job) {
const { instructions } = this.options.plugin; const { instructions } = this.options.plugin;
const instruction = instructions.get(node.type); const instruction = instructions.get(node.type);
if (typeof instruction.resume !== 'function') { if (typeof instruction.resume !== 'function') {
@ -206,7 +230,7 @@ export default class Processor {
return this.exec(instruction.resume.bind(instruction), node, job); return this.exec(instruction.resume.bind(instruction), node, job);
} }
async exit(s?: number) { public async exit(s?: number) {
if (typeof s === 'number') { if (typeof s === 'number') {
const status = (<typeof Processor>this.constructor).StatusMap[s] ?? Math.sign(s); const status = (<typeof Processor>this.constructor).StatusMap[s] ?? Math.sign(s);
await this.execution.update({ status }, { transaction: this.transaction }); await this.execution.update({ status }, { transaction: this.transaction });
@ -216,6 +240,11 @@ export default class Processor {
} }
// TODO(optimize) // TODO(optimize)
/**
* @experimental
* @param {JobModel | Record<string, any>} payload
* @returns {JobModel}
*/
async saveJob(payload) { async saveJob(payload) {
const { database } = <typeof ExecutionModel>this.execution.constructor; const { database } = <typeof ExecutionModel>this.execution.constructor;
const { transaction } = this; const { transaction } = this;
@ -243,13 +272,19 @@ export default class Processor {
return job; return job;
} }
/**
* @experimental
*/
getBranches(node: FlowNodeModel): FlowNodeModel[] { getBranches(node: FlowNodeModel): FlowNodeModel[] {
return this.nodes return this.nodes
.filter((item) => item.upstream === node && item.branchIndex !== null) .filter((item) => item.upstream === node && item.branchIndex !== null)
.sort((a, b) => Number(a.branchIndex) - Number(b.branchIndex)); .sort((a, b) => Number(a.branchIndex) - Number(b.branchIndex));
} }
// find the first node in current branch /**
* @experimental
* find the first node in current branch
*/
findBranchStartNode(node: FlowNodeModel, parent?: FlowNodeModel): FlowNodeModel | null { findBranchStartNode(node: FlowNodeModel, parent?: FlowNodeModel): FlowNodeModel | null {
for (let n = node; n; n = n.upstream) { for (let n = node; n; n = n.upstream) {
if (!parent) { if (!parent) {
@ -265,7 +300,10 @@ export default class Processor {
return null; return null;
} }
// find the node start current branch /**
* @experimental
* find the node start current branch
*/
findBranchParentNode(node: FlowNodeModel): FlowNodeModel | null { findBranchParentNode(node: FlowNodeModel): FlowNodeModel | null {
for (let n = node; n; n = n.upstream) { for (let n = node; n; n = n.upstream) {
if (n.branchIndex !== null) { if (n.branchIndex !== null) {
@ -275,6 +313,9 @@ export default class Processor {
return null; return null;
} }
/**
* @experimental
*/
findBranchEndNode(node: FlowNodeModel): FlowNodeModel | null { findBranchEndNode(node: FlowNodeModel): FlowNodeModel | null {
for (let n = node; n; n = n.downstream) { for (let n = node; n; n = n.downstream) {
if (!n.downstream) { if (!n.downstream) {
@ -284,6 +325,9 @@ export default class Processor {
return null; return null;
} }
/**
* @experimental
*/
findBranchParentJob(job: JobModel, node: FlowNodeModel): JobModel | null { findBranchParentJob(job: JobModel, node: FlowNodeModel): JobModel | null {
for (let j: JobModel | undefined = job; j; j = this.jobsMap.get(j.upstreamId)) { for (let j: JobModel | undefined = job; j; j = this.jobsMap.get(j.upstreamId)) {
if (j.nodeId === node.id) { if (j.nodeId === node.id) {
@ -293,6 +337,9 @@ export default class Processor {
return null; return null;
} }
/**
* @experimental
*/
findBranchLastJob(node: FlowNodeModel, job: JobModel): JobModel | null { findBranchLastJob(node: FlowNodeModel, job: JobModel): JobModel | null {
const allJobs = Array.from(this.jobsMap.values()); const allJobs = Array.from(this.jobsMap.values());
const branchJobs = []; const branchJobs = [];
@ -310,6 +357,9 @@ export default class Processor {
return null; return null;
} }
/**
* @experimental
*/
public getScope(sourceNodeId: number) { public getScope(sourceNodeId: number) {
const node = this.nodesMap.get(sourceNodeId); const node = this.nodesMap.get(sourceNodeId);
const systemFns = {}; const systemFns = {};
@ -337,6 +387,9 @@ export default class Processor {
}; };
} }
/**
* @experimental
*/
public getParsedValue(value, sourceNodeId: number, additionalScope?: object) { public getParsedValue(value, sourceNodeId: number, additionalScope?: object) {
const template = parse(value); const template = parse(value);
const scope = Object.assign(this.getScope(sourceNodeId), additionalScope); const scope = Object.assign(this.getScope(sourceNodeId), additionalScope);