nocobase/packages/plugins/@nocobase/plugin-workflow-loop/src/server/LoopInstruction.ts

99 lines
2.8 KiB
TypeScript
Raw Normal View History

refactor(plugin-workflow): split workflow features into plugins (#3115) * refactor(plugin-workflow): split manual and dynamic calculation into plugins * refactor(plugin-workflow): move loop to plugin * refactor(plugin-workflow): move parallel to plugin * fix(plugin-dynamic-calculation): fix package title * fix(plugin-workflow): fix plugin name * refactor(plugin-workflow): move delay to plugin * refactor(plugin-workflow): simplify exporting names * refactor(plugin-workflow): move aggregate to plugin * refactor(plugin-workflow): move sql to plugin * refactor(plugin-workflow): move reqeust to plugin * refactor(plugin-workflow): move form trigger to plugin * refactor(plugin-workflow): move locale to plugins * fix(plugin-workflow): fix test cases * fix(plugin-workflow-request): package name typo * fix(plugin-workflow): remove clean db from testkit * fix(plugin-workflow-sql): skip independent case * fix(plugin-workflow-sql): skip independent case * fix(plugin-workflow-delay): fix test cases * test(plugin-workflow-delay): fix test cases * test(plugin-workflow-delay): fix test cases * test(plugin-workflow-delay): fix test cases * test(plugin-workflow-delay): fix test cases * fix(plugin-workflow): fix migration version matching * test(plugin-workflow): fix test case * refactor(plugin-workflow): correct exporting of testkit * fix(plugin-workflow): fix testkit and require module * refactor(plugin-workflow): add workflow-test package for testing * test(plugin-workflow): test weird case * fix(plugin-workflow-test): remove workflow dependency to avoid cycling * fix(plugin-workflow): fix migration version * fix(plugin-workflow): fix migration and packages * fix(plugin-workflow): fix package dependencies * fix(preset): fix builtin list in preset * fix(plugin-workflow): add package entry file * fix(plugin-workflow): fix migrations * refactor(plugin-workflow): remove require * fix(plugin-workflow): fix locale namespace * fix(plugin-workflow): fix merged errors * fix(plugin-workflow): fix import cycling references * refactor(plugin-workflow): change instruction and triggers to classes in client * fix(plugin-workflow): fix migration version
2023-12-07 13:46:58 +00:00
import { Processor, Instruction, JOB_STATUS, FlowNodeModel, JobModel } from '@nocobase/plugin-workflow';
function getTargetLength(target) {
let length = 0;
if (typeof target === 'number') {
if (target < 0) {
throw new Error('Loop target in number type must be greater than 0');
}
length = Math.floor(target);
} else {
const targets = (Array.isArray(target) ? target : [target]).filter((t) => t != null);
length = targets.length;
}
return length;
}
refactor(plugin-workflow): split workflow features into plugins (#3115) * refactor(plugin-workflow): split manual and dynamic calculation into plugins * refactor(plugin-workflow): move loop to plugin * refactor(plugin-workflow): move parallel to plugin * fix(plugin-dynamic-calculation): fix package title * fix(plugin-workflow): fix plugin name * refactor(plugin-workflow): move delay to plugin * refactor(plugin-workflow): simplify exporting names * refactor(plugin-workflow): move aggregate to plugin * refactor(plugin-workflow): move sql to plugin * refactor(plugin-workflow): move reqeust to plugin * refactor(plugin-workflow): move form trigger to plugin * refactor(plugin-workflow): move locale to plugins * fix(plugin-workflow): fix test cases * fix(plugin-workflow-request): package name typo * fix(plugin-workflow): remove clean db from testkit * fix(plugin-workflow-sql): skip independent case * fix(plugin-workflow-sql): skip independent case * fix(plugin-workflow-delay): fix test cases * test(plugin-workflow-delay): fix test cases * test(plugin-workflow-delay): fix test cases * test(plugin-workflow-delay): fix test cases * test(plugin-workflow-delay): fix test cases * fix(plugin-workflow): fix migration version matching * test(plugin-workflow): fix test case * refactor(plugin-workflow): correct exporting of testkit * fix(plugin-workflow): fix testkit and require module * refactor(plugin-workflow): add workflow-test package for testing * test(plugin-workflow): test weird case * fix(plugin-workflow-test): remove workflow dependency to avoid cycling * fix(plugin-workflow): fix migration version * fix(plugin-workflow): fix migration and packages * fix(plugin-workflow): fix package dependencies * fix(preset): fix builtin list in preset * fix(plugin-workflow): add package entry file * fix(plugin-workflow): fix migrations * refactor(plugin-workflow): remove require * fix(plugin-workflow): fix locale namespace * fix(plugin-workflow): fix merged errors * fix(plugin-workflow): fix import cycling references * refactor(plugin-workflow): change instruction and triggers to classes in client * fix(plugin-workflow): fix migration version
2023-12-07 13:46:58 +00:00
export default class extends Instruction {
async run(node: FlowNodeModel, prevJob: JobModel, processor: Processor) {
const [branch] = processor.getBranches(node);
const target = processor.getParsedValue(node.config.target, node.id);
const length = getTargetLength(target);
if (!branch || !length) {
return {
status: JOB_STATUS.RESOLVED,
result: 0,
};
}
const job = await processor.saveJob({
status: JOB_STATUS.PENDING,
// save loop index
result: 0,
nodeId: node.id,
nodeKey: node.key,
upstreamId: prevJob?.id ?? null,
});
// TODO: add loop scope to stack
// processor.stack.push({
// label: node.title,
// value: node.id
// });
await processor.run(branch, job);
return null;
refactor(plugin-workflow): split workflow features into plugins (#3115) * refactor(plugin-workflow): split manual and dynamic calculation into plugins * refactor(plugin-workflow): move loop to plugin * refactor(plugin-workflow): move parallel to plugin * fix(plugin-dynamic-calculation): fix package title * fix(plugin-workflow): fix plugin name * refactor(plugin-workflow): move delay to plugin * refactor(plugin-workflow): simplify exporting names * refactor(plugin-workflow): move aggregate to plugin * refactor(plugin-workflow): move sql to plugin * refactor(plugin-workflow): move reqeust to plugin * refactor(plugin-workflow): move form trigger to plugin * refactor(plugin-workflow): move locale to plugins * fix(plugin-workflow): fix test cases * fix(plugin-workflow-request): package name typo * fix(plugin-workflow): remove clean db from testkit * fix(plugin-workflow-sql): skip independent case * fix(plugin-workflow-sql): skip independent case * fix(plugin-workflow-delay): fix test cases * test(plugin-workflow-delay): fix test cases * test(plugin-workflow-delay): fix test cases * test(plugin-workflow-delay): fix test cases * test(plugin-workflow-delay): fix test cases * fix(plugin-workflow): fix migration version matching * test(plugin-workflow): fix test case * refactor(plugin-workflow): correct exporting of testkit * fix(plugin-workflow): fix testkit and require module * refactor(plugin-workflow): add workflow-test package for testing * test(plugin-workflow): test weird case * fix(plugin-workflow-test): remove workflow dependency to avoid cycling * fix(plugin-workflow): fix migration version * fix(plugin-workflow): fix migration and packages * fix(plugin-workflow): fix package dependencies * fix(preset): fix builtin list in preset * fix(plugin-workflow): add package entry file * fix(plugin-workflow): fix migrations * refactor(plugin-workflow): remove require * fix(plugin-workflow): fix locale namespace * fix(plugin-workflow): fix merged errors * fix(plugin-workflow): fix import cycling references * refactor(plugin-workflow): change instruction and triggers to classes in client * fix(plugin-workflow): fix migration version
2023-12-07 13:46:58 +00:00
}
async resume(node: FlowNodeModel, branchJob, processor: Processor) {
const job = processor.findBranchParentJob(branchJob, node) as JobModel;
const loop = processor.nodesMap.get(job.nodeId);
const [branch] = processor.getBranches(node);
const { result, status } = job;
// if loop has been done (resolved / rejected), do not care newly executed branch jobs.
if (status !== JOB_STATUS.PENDING) {
return processor.exit();
}
const nextIndex = result + 1;
const target = processor.getParsedValue(loop.config.target, node.id);
// branchJob.status === JOB_STATUS.RESOLVED means branchJob is done, try next loop or exit as resolved
if (branchJob.status > JOB_STATUS.PENDING) {
job.set({ result: nextIndex });
const length = getTargetLength(target);
if (nextIndex < length) {
await processor.saveJob(job);
await processor.run(branch, job);
return null;
}
}
// branchJob.status < JOB_STATUS.PENDING means branchJob is rejected, any rejection should cause loop rejected
job.set({
status: branchJob.status,
});
return job;
refactor(plugin-workflow): split workflow features into plugins (#3115) * refactor(plugin-workflow): split manual and dynamic calculation into plugins * refactor(plugin-workflow): move loop to plugin * refactor(plugin-workflow): move parallel to plugin * fix(plugin-dynamic-calculation): fix package title * fix(plugin-workflow): fix plugin name * refactor(plugin-workflow): move delay to plugin * refactor(plugin-workflow): simplify exporting names * refactor(plugin-workflow): move aggregate to plugin * refactor(plugin-workflow): move sql to plugin * refactor(plugin-workflow): move reqeust to plugin * refactor(plugin-workflow): move form trigger to plugin * refactor(plugin-workflow): move locale to plugins * fix(plugin-workflow): fix test cases * fix(plugin-workflow-request): package name typo * fix(plugin-workflow): remove clean db from testkit * fix(plugin-workflow-sql): skip independent case * fix(plugin-workflow-sql): skip independent case * fix(plugin-workflow-delay): fix test cases * test(plugin-workflow-delay): fix test cases * test(plugin-workflow-delay): fix test cases * test(plugin-workflow-delay): fix test cases * test(plugin-workflow-delay): fix test cases * fix(plugin-workflow): fix migration version matching * test(plugin-workflow): fix test case * refactor(plugin-workflow): correct exporting of testkit * fix(plugin-workflow): fix testkit and require module * refactor(plugin-workflow): add workflow-test package for testing * test(plugin-workflow): test weird case * fix(plugin-workflow-test): remove workflow dependency to avoid cycling * fix(plugin-workflow): fix migration version * fix(plugin-workflow): fix migration and packages * fix(plugin-workflow): fix package dependencies * fix(preset): fix builtin list in preset * fix(plugin-workflow): add package entry file * fix(plugin-workflow): fix migrations * refactor(plugin-workflow): remove require * fix(plugin-workflow): fix locale namespace * fix(plugin-workflow): fix merged errors * fix(plugin-workflow): fix import cycling references * refactor(plugin-workflow): change instruction and triggers to classes in client * fix(plugin-workflow): fix migration version
2023-12-07 13:46:58 +00:00
}
getScope(node, index, processor) {
const target = processor.getParsedValue(node.config.target, node.id);
const targets = (Array.isArray(target) ? target : [target]).filter((t) => t != null);
const length = getTargetLength(target);
const item = typeof target === 'number' ? index : targets[index];
const result = {
item,
index,
length,
};
return result;
refactor(plugin-workflow): split workflow features into plugins (#3115) * refactor(plugin-workflow): split manual and dynamic calculation into plugins * refactor(plugin-workflow): move loop to plugin * refactor(plugin-workflow): move parallel to plugin * fix(plugin-dynamic-calculation): fix package title * fix(plugin-workflow): fix plugin name * refactor(plugin-workflow): move delay to plugin * refactor(plugin-workflow): simplify exporting names * refactor(plugin-workflow): move aggregate to plugin * refactor(plugin-workflow): move sql to plugin * refactor(plugin-workflow): move reqeust to plugin * refactor(plugin-workflow): move form trigger to plugin * refactor(plugin-workflow): move locale to plugins * fix(plugin-workflow): fix test cases * fix(plugin-workflow-request): package name typo * fix(plugin-workflow): remove clean db from testkit * fix(plugin-workflow-sql): skip independent case * fix(plugin-workflow-sql): skip independent case * fix(plugin-workflow-delay): fix test cases * test(plugin-workflow-delay): fix test cases * test(plugin-workflow-delay): fix test cases * test(plugin-workflow-delay): fix test cases * test(plugin-workflow-delay): fix test cases * fix(plugin-workflow): fix migration version matching * test(plugin-workflow): fix test case * refactor(plugin-workflow): correct exporting of testkit * fix(plugin-workflow): fix testkit and require module * refactor(plugin-workflow): add workflow-test package for testing * test(plugin-workflow): test weird case * fix(plugin-workflow-test): remove workflow dependency to avoid cycling * fix(plugin-workflow): fix migration version * fix(plugin-workflow): fix migration and packages * fix(plugin-workflow): fix package dependencies * fix(preset): fix builtin list in preset * fix(plugin-workflow): add package entry file * fix(plugin-workflow): fix migrations * refactor(plugin-workflow): remove require * fix(plugin-workflow): fix locale namespace * fix(plugin-workflow): fix merged errors * fix(plugin-workflow): fix import cycling references * refactor(plugin-workflow): change instruction and triggers to classes in client * fix(plugin-workflow): fix migration version
2023-12-07 13:46:58 +00:00
}
}