2023-12-07 21:46:58 +08:00
|
|
|
import { Processor, Instruction, JOB_STATUS, FlowNodeModel, JobModel } from '@nocobase/plugin-workflow';
|
2022-02-01 12:04:08 +08:00
|
|
|
|
|
|
|
export const PARALLEL_MODE = {
|
|
|
|
ALL: 'all',
|
|
|
|
ANY: 'any',
|
2023-04-25 13:12:14 +08:00
|
|
|
RACE: 'race',
|
2022-02-01 12:04:08 +08:00
|
|
|
} as const;
|
|
|
|
|
2022-06-28 17:00:19 +08:00
|
|
|
const Modes = {
|
|
|
|
[PARALLEL_MODE.ALL]: {
|
|
|
|
next(previous) {
|
2023-02-20 11:52:06 +08:00
|
|
|
return previous.status >= JOB_STATUS.PENDING;
|
2022-06-28 17:00:19 +08:00
|
|
|
},
|
|
|
|
getStatus(result) {
|
2023-04-25 13:12:14 +08:00
|
|
|
const failedStatus = result.find((status) => status != null && status < JOB_STATUS.PENDING);
|
2023-02-20 11:52:06 +08:00
|
|
|
if (typeof failedStatus !== 'undefined') {
|
|
|
|
return failedStatus;
|
2022-06-28 17:00:19 +08:00
|
|
|
}
|
2023-04-25 13:12:14 +08:00
|
|
|
if (result.every((status) => status != null && status === JOB_STATUS.RESOLVED)) {
|
2022-06-28 17:00:19 +08:00
|
|
|
return JOB_STATUS.RESOLVED;
|
|
|
|
}
|
|
|
|
return JOB_STATUS.PENDING;
|
2023-04-25 13:12:14 +08:00
|
|
|
},
|
2022-02-01 12:04:08 +08:00
|
|
|
},
|
2022-06-28 17:00:19 +08:00
|
|
|
[PARALLEL_MODE.ANY]: {
|
|
|
|
next(previous) {
|
2023-02-20 11:52:06 +08:00
|
|
|
return previous.status <= JOB_STATUS.PENDING;
|
2022-06-28 17:00:19 +08:00
|
|
|
},
|
|
|
|
getStatus(result) {
|
2023-04-25 13:12:14 +08:00
|
|
|
if (result.some((status) => status != null && status === JOB_STATUS.RESOLVED)) {
|
2022-06-28 17:00:19 +08:00
|
|
|
return JOB_STATUS.RESOLVED;
|
|
|
|
}
|
2023-04-25 13:12:14 +08:00
|
|
|
if (result.some((status) => (status != null ? status === JOB_STATUS.PENDING : true))) {
|
2022-06-28 17:00:19 +08:00
|
|
|
return JOB_STATUS.PENDING;
|
|
|
|
}
|
2023-02-20 11:52:06 +08:00
|
|
|
return JOB_STATUS.FAILED;
|
2023-04-25 13:12:14 +08:00
|
|
|
},
|
2022-02-01 12:04:08 +08:00
|
|
|
},
|
2022-06-28 17:00:19 +08:00
|
|
|
[PARALLEL_MODE.RACE]: {
|
|
|
|
next(previous) {
|
|
|
|
return previous.status === JOB_STATUS.PENDING;
|
|
|
|
},
|
|
|
|
getStatus(result) {
|
2023-04-25 13:12:14 +08:00
|
|
|
if (result.some((status) => status != null && status === JOB_STATUS.RESOLVED)) {
|
2022-06-28 17:00:19 +08:00
|
|
|
return JOB_STATUS.RESOLVED;
|
|
|
|
}
|
2023-04-25 13:12:14 +08:00
|
|
|
const failedStatus = result.find((status) => status != null && status < JOB_STATUS.PENDING);
|
2023-02-20 11:52:06 +08:00
|
|
|
if (typeof failedStatus !== 'undefined') {
|
|
|
|
return failedStatus;
|
2022-06-28 17:00:19 +08:00
|
|
|
}
|
|
|
|
return JOB_STATUS.PENDING;
|
2023-04-25 13:12:14 +08:00
|
|
|
},
|
|
|
|
},
|
2022-02-01 12:04:08 +08:00
|
|
|
};
|
|
|
|
|
2023-12-07 21:46:58 +08:00
|
|
|
export default class extends Instruction {
|
2022-06-24 23:28:49 +08:00
|
|
|
async run(node: FlowNodeModel, prevJob: JobModel, processor: Processor) {
|
2022-06-28 17:00:19 +08:00
|
|
|
const branches = processor.getBranches(node);
|
2022-02-01 12:04:08 +08:00
|
|
|
|
2022-06-20 23:29:21 +08:00
|
|
|
const job = await processor.saveJob({
|
2022-02-01 12:04:08 +08:00
|
|
|
status: JOB_STATUS.PENDING,
|
|
|
|
result: Array(branches.length).fill(null),
|
2022-06-24 23:28:49 +08:00
|
|
|
nodeId: node.id,
|
2023-04-25 13:12:14 +08:00
|
|
|
upstreamId: prevJob?.id ?? null,
|
2022-02-01 12:04:08 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// NOTE:
|
|
|
|
// use `reduce` but not `Promise.all` here to avoid racing manupulating db.
|
|
|
|
// for users, this is almost equivalent to `Promise.all`,
|
|
|
|
// because of the delay is not significant sensible.
|
2022-06-20 23:29:21 +08:00
|
|
|
// another benifit of this is, it could handle sequenced branches in future.
|
2022-06-28 17:00:19 +08:00
|
|
|
const { mode = PARALLEL_MODE.ALL } = node.config;
|
2023-04-25 13:12:14 +08:00
|
|
|
await branches.reduce(
|
|
|
|
(promise: Promise<any>, branch, i) =>
|
2023-05-16 09:45:45 +08:00
|
|
|
promise.then(async (previous) => {
|
2023-04-25 13:12:14 +08:00
|
|
|
if (i && !Modes[mode].next(previous)) {
|
2023-05-16 09:45:45 +08:00
|
|
|
return previous;
|
2023-04-25 13:12:14 +08:00
|
|
|
}
|
2023-05-16 09:45:45 +08:00
|
|
|
await processor.run(branch, job);
|
|
|
|
|
|
|
|
// find last job of the branch
|
2023-09-22 17:59:36 +08:00
|
|
|
return processor.findBranchLastJob(branch, job);
|
2023-04-25 13:12:14 +08:00
|
|
|
}),
|
|
|
|
Promise.resolve(),
|
|
|
|
);
|
2022-02-01 12:04:08 +08:00
|
|
|
|
2023-05-16 09:45:45 +08:00
|
|
|
return null;
|
2023-12-07 21:46:58 +08:00
|
|
|
}
|
2022-02-01 12:04:08 +08:00
|
|
|
|
2022-06-24 23:28:49 +08:00
|
|
|
async resume(node: FlowNodeModel, branchJob, processor: Processor) {
|
2023-02-20 11:52:06 +08:00
|
|
|
const job = processor.findBranchParentJob(branchJob, node) as JobModel;
|
2022-02-01 12:04:08 +08:00
|
|
|
|
|
|
|
const { result, status } = job;
|
|
|
|
// if parallel has been done (resolved / rejected), do not care newly executed branch jobs.
|
|
|
|
if (status !== JOB_STATUS.PENDING) {
|
2023-06-23 23:21:19 +08:00
|
|
|
return processor.exit();
|
2022-02-01 12:04:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// find the index of the node which start the branch
|
2023-02-20 11:52:06 +08:00
|
|
|
const jobNode = processor.nodesMap.get(branchJob.nodeId) as FlowNodeModel;
|
|
|
|
const branchStartNode = processor.findBranchStartNode(jobNode, node) as FlowNodeModel;
|
2022-06-28 17:00:19 +08:00
|
|
|
const branches = processor.getBranches(node);
|
|
|
|
const branchIndex = branches.indexOf(branchStartNode);
|
2022-06-24 23:28:49 +08:00
|
|
|
const { mode = PARALLEL_MODE.ALL } = node.config || {};
|
2022-02-01 12:04:08 +08:00
|
|
|
|
2022-06-28 17:00:19 +08:00
|
|
|
const newResult = [...result.slice(0, branchIndex), branchJob.status, ...result.slice(branchIndex + 1)];
|
2022-02-01 12:04:08 +08:00
|
|
|
job.set({
|
|
|
|
result: newResult,
|
2023-04-25 13:12:14 +08:00
|
|
|
status: Modes[mode].getStatus(newResult),
|
2022-02-01 12:04:08 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
if (job.status === JOB_STATUS.PENDING) {
|
2023-12-27 13:55:48 +08:00
|
|
|
await job.save();
|
2023-06-23 23:21:19 +08:00
|
|
|
return processor.exit();
|
2022-02-01 12:04:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return job;
|
2023-12-07 21:46:58 +08:00
|
|
|
}
|
|
|
|
}
|