@nocobase/plugin-workflow-loop 1.9.0-beta.8 → 2.0.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.env.example ADDED
@@ -0,0 +1,3 @@
1
+ # Limit of workflow loops executed in one loop node
2
+ # By default, no limit
3
+ # WORKFLOW_LOOP_LIMIT=100
@@ -12,10 +12,10 @@ module.exports = {
12
12
  "@ant-design/icons": "5.6.1",
13
13
  "antd": "5.24.2",
14
14
  "@formily/antd-v5": "1.2.3",
15
- "@formily/react": "2.3.0",
16
- "@nocobase/client": "1.9.0-beta.8",
17
- "@nocobase/plugin-workflow": "1.9.0-beta.8",
15
+ "@formily/react": "2.3.7",
16
+ "@nocobase/client": "2.0.0-alpha.10",
17
+ "@nocobase/plugin-workflow": "2.0.0-alpha.10",
18
18
  "react-i18next": "11.18.6",
19
- "@nocobase/evaluators": "1.9.0-beta.8",
20
- "@nocobase/server": "1.9.0-beta.8"
19
+ "@nocobase/evaluators": "2.0.0-alpha.10",
20
+ "@nocobase/server": "2.0.0-alpha.10"
21
21
  };
@@ -22,6 +22,7 @@ export default class extends Instruction {
22
22
  status: 1;
23
23
  result: {
24
24
  looped: number;
25
+ done: number;
25
26
  };
26
27
  }>;
27
28
  resume(node: FlowNodeModel, branchJob: any, processor: Processor): Promise<JobModel>;
@@ -42,6 +42,10 @@ module.exports = __toCommonJS(LoopInstruction_exports);
42
42
  var import_evaluators = __toESM(require("@nocobase/evaluators"));
43
43
  var import_plugin_workflow = require("@nocobase/plugin-workflow");
44
44
  var import_constants = require("../constants");
45
+ function getLoopLimit() {
46
+ const limit = process.env.WORKFLOW_LOOP_LIMIT;
47
+ return limit ? parseInt(limit, 10) : null;
48
+ }
45
49
  function getTargetLength(target) {
46
50
  let length = 0;
47
51
  if (typeof target === "number") {
@@ -65,16 +69,16 @@ class LoopInstruction_default extends import_plugin_workflow.Instruction {
65
69
  const [branch] = processor.getBranches(node);
66
70
  const target = processor.getParsedValue(node.config.target, node.id);
67
71
  const length = getTargetLength(target);
68
- const looped = 0;
72
+ const result = { looped: 0, done: 0 };
69
73
  if (!branch || !length) {
70
74
  return {
71
75
  status: import_plugin_workflow.JOB_STATUS.RESOLVED,
72
- result: { looped }
76
+ result
73
77
  };
74
78
  }
75
79
  const job = processor.saveJob({
76
80
  status: import_plugin_workflow.JOB_STATUS.PENDING,
77
- result: { looped },
81
+ result,
78
82
  nodeId: node.id,
79
83
  nodeKey: node.key,
80
84
  upstreamId: (prevJob == null ? void 0 : prevJob.id) ?? null
@@ -82,18 +86,29 @@ class LoopInstruction_default extends import_plugin_workflow.Instruction {
82
86
  if (node.config.condition) {
83
87
  const { checkpoint, calculation, expression, continueOnFalse } = node.config.condition ?? {};
84
88
  if ((calculation || expression) && !checkpoint) {
85
- const condition = calculateCondition(node, processor);
86
- if (!condition) {
89
+ while (result.looped < length) {
90
+ const condition = calculateCondition(node, processor);
91
+ if (condition) {
92
+ break;
93
+ }
94
+ result.looped += 1;
95
+ job.set("result", result);
87
96
  if (continueOnFalse) {
88
- job.set("result", { looped: 1 });
89
97
  processor.saveJob(job);
90
- } else {
91
- job.set({
92
- status: import_plugin_workflow.JOB_STATUS.RESOLVED,
93
- result: { looped, broken: true }
94
- });
95
- return job;
98
+ continue;
96
99
  }
100
+ job.set({
101
+ status: import_plugin_workflow.JOB_STATUS.RESOLVED,
102
+ result: { ...result, broken: true }
103
+ });
104
+ return job;
105
+ }
106
+ if (result.looped >= length) {
107
+ job.set({
108
+ status: import_plugin_workflow.JOB_STATUS.RESOLVED,
109
+ result
110
+ });
111
+ return job;
97
112
  }
98
113
  }
99
114
  }
@@ -111,28 +126,46 @@ class LoopInstruction_default extends import_plugin_workflow.Instruction {
111
126
  if (branchJob.id !== job.id && branchJob.status === import_plugin_workflow.JOB_STATUS.PENDING) {
112
127
  return null;
113
128
  }
114
- const nextIndex = result.looped + 1;
129
+ result.looped += 1;
115
130
  const target = processor.getParsedValue(loop.config.target, node.id);
116
131
  if (branchJob.status === import_plugin_workflow.JOB_STATUS.RESOLVED || branchJob.status < import_plugin_workflow.JOB_STATUS.PENDING && loop.config.exit === import_constants.EXIT.CONTINUE) {
117
- job.set({ result: { looped: nextIndex } });
118
- processor.saveJob(job);
132
+ result.done += 1;
119
133
  const length = getTargetLength(target);
120
- if (nextIndex < length) {
121
- if (loop.config.condition) {
122
- const { calculation, expression, continueOnFalse } = loop.config.condition ?? {};
123
- if (calculation || expression) {
134
+ if (loop.config.condition) {
135
+ const { calculation, expression, continueOnFalse } = loop.config.condition ?? {};
136
+ if (calculation || expression) {
137
+ while (result.looped < length) {
124
138
  const condition = calculateCondition(loop, processor);
125
- if (!condition && !continueOnFalse) {
139
+ if (condition) {
140
+ processor.logger.debug(`loop condition matched, continue inner branch (looped: ${result.looped})`);
141
+ break;
142
+ }
143
+ if (!continueOnFalse) {
144
+ processor.logger.debug(`loop condition not matches, break (looped: ${result.looped})`);
126
145
  job.set({
127
146
  status: import_plugin_workflow.JOB_STATUS.RESOLVED,
128
- result: { looped: nextIndex, broken: true }
147
+ result: { ...result, broken: true }
129
148
  });
130
149
  return job;
131
150
  }
151
+ result.looped += 1;
152
+ processor.logger.debug(`loop condition not matches, try next loop item (looped: ${result.looped})`);
132
153
  }
133
154
  }
134
- await processor.run(branch, job);
135
- return;
155
+ }
156
+ job.set({ result });
157
+ job.changed("result", true);
158
+ processor.saveJob(job);
159
+ if (result.looped < length) {
160
+ const loopLimit = getLoopLimit();
161
+ if (!loopLimit || result.done < loopLimit) {
162
+ await processor.run(branch, job);
163
+ return;
164
+ }
165
+ job.set({
166
+ status: import_plugin_workflow.JOB_STATUS.ERROR,
167
+ result: { ...result, exceeded: true }
168
+ });
136
169
  } else {
137
170
  job.set({
138
171
  status: import_plugin_workflow.JOB_STATUS.RESOLVED
@@ -141,7 +174,7 @@ class LoopInstruction_default extends import_plugin_workflow.Instruction {
141
174
  } else {
142
175
  job.set(
143
176
  loop.config.exit ? {
144
- result: { looped: result.looped, broken: true },
177
+ result: { ...result, broken: true },
145
178
  status: import_plugin_workflow.JOB_STATUS.RESOLVED
146
179
  } : {
147
180
  status: branchJob.status
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "displayName.zh-CN": "工作流:循环节点",
5
5
  "description": "Used to repeat the sub-process processing of each value in an array, and can also be used for fixed times of sub-process processing.",
6
6
  "description.zh-CN": "用于对一个数组中的每个值进行重复的子流程处理,也可用于固定次数的重复子流程处理。",
7
- "version": "1.9.0-beta.8",
7
+ "version": "2.0.0-alpha.10",
8
8
  "license": "AGPL-3.0",
9
9
  "main": "./dist/server/index.js",
10
10
  "homepage": "https://docs.nocobase.com/handbook/workflow-loop",
@@ -15,14 +15,14 @@
15
15
  "react-i18next": "^11.15.1"
16
16
  },
17
17
  "peerDependencies": {
18
- "@nocobase/client": "1.x",
19
- "@nocobase/database": "1.x",
20
- "@nocobase/evaluators": "1.x",
18
+ "@nocobase/client": "2.x",
19
+ "@nocobase/database": "2.x",
20
+ "@nocobase/evaluators": "2.x",
21
21
  "@nocobase/plugin-workflow": ">=0.17.0-alpha.3",
22
- "@nocobase/server": "1.x",
23
- "@nocobase/test": "1.x"
22
+ "@nocobase/server": "2.x",
23
+ "@nocobase/test": "2.x"
24
24
  },
25
- "gitHead": "f3d4f3d1ba7adbf4d4c60e656c23da45565769c8",
25
+ "gitHead": "6422cfb6fa6c8450de2efa3294fd29a28b228990",
26
26
  "keywords": [
27
27
  "Workflow"
28
28
  ]