@nocobase/plugin-workflow 0.7.0-alpha.9 → 0.7.1-alpha.6

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.
Files changed (159) hide show
  1. package/lib/Plugin.d.ts +18 -0
  2. package/lib/Plugin.js +285 -0
  3. package/lib/Processor.d.ts +40 -0
  4. package/lib/Processor.js +440 -0
  5. package/lib/actions/index.d.ts +3 -1
  6. package/lib/actions/index.js +33 -26
  7. package/{esm/actions/flow_nodes.d.ts → lib/actions/nodes.d.ts} +1 -0
  8. package/lib/actions/nodes.js +321 -0
  9. package/lib/actions/workflows.d.ts +2 -0
  10. package/lib/actions/workflows.js +197 -0
  11. package/lib/calculators/index.d.ts +2 -2
  12. package/lib/calculators/index.js +144 -92
  13. package/lib/collections/executions.js +32 -38
  14. package/lib/collections/flow_nodes.js +60 -72
  15. package/lib/collections/jobs.js +26 -47
  16. package/lib/collections/workflows.d.ts +1 -2
  17. package/lib/collections/workflows.js +70 -63
  18. package/lib/constants.js +22 -17
  19. package/lib/index.d.ts +4 -3
  20. package/lib/index.js +85 -22
  21. package/lib/instructions/calculation.d.ts +1 -1
  22. package/lib/instructions/calculation.js +32 -29
  23. package/lib/instructions/condition.d.ts +2 -2
  24. package/lib/instructions/condition.js +91 -88
  25. package/lib/instructions/create.d.ts +2 -2
  26. package/lib/instructions/create.js +39 -25
  27. package/lib/instructions/delay.d.ts +14 -0
  28. package/lib/instructions/delay.js +138 -0
  29. package/lib/instructions/destroy.d.ts +1 -1
  30. package/lib/instructions/destroy.js +38 -25
  31. package/lib/instructions/index.d.ts +11 -8
  32. package/lib/instructions/index.js +76 -25
  33. package/lib/instructions/parallel.d.ts +3 -3
  34. package/lib/instructions/parallel.js +95 -84
  35. package/lib/instructions/prompt.d.ts +2 -2
  36. package/lib/instructions/prompt.js +21 -13
  37. package/lib/instructions/query.d.ts +2 -1
  38. package/lib/instructions/query.js +42 -25
  39. package/lib/instructions/update.d.ts +2 -1
  40. package/lib/instructions/update.js +40 -25
  41. package/lib/models/Execution.d.ts +3 -33
  42. package/lib/models/Execution.js +18 -254
  43. package/lib/models/FlowNode.js +18 -5
  44. package/lib/models/Job.js +18 -5
  45. package/lib/models/Workflow.d.ts +5 -6
  46. package/lib/models/Workflow.js +18 -76
  47. package/lib/triggers/collection.d.ts +12 -0
  48. package/lib/triggers/collection.js +184 -0
  49. package/lib/triggers/index.d.ts +11 -7
  50. package/lib/triggers/index.js +64 -11
  51. package/lib/triggers/schedule.d.ts +38 -0
  52. package/lib/triggers/schedule.js +672 -0
  53. package/package.json +8 -12
  54. package/esm/actions/flow_nodes.js +0 -139
  55. package/esm/actions/flow_nodes.js.map +0 -1
  56. package/esm/actions/index.d.ts +0 -1
  57. package/esm/actions/index.js +0 -8
  58. package/esm/actions/index.js.map +0 -1
  59. package/esm/calculators/index.d.ts +0 -38
  60. package/esm/calculators/index.js +0 -128
  61. package/esm/calculators/index.js.map +0 -1
  62. package/esm/collections/executions.d.ts +0 -3
  63. package/esm/collections/executions.js +0 -38
  64. package/esm/collections/executions.js.map +0 -1
  65. package/esm/collections/flow_nodes.d.ts +0 -3
  66. package/esm/collections/flow_nodes.js +0 -72
  67. package/esm/collections/flow_nodes.js.map +0 -1
  68. package/esm/collections/jobs.d.ts +0 -3
  69. package/esm/collections/jobs.js +0 -47
  70. package/esm/collections/jobs.js.map +0 -1
  71. package/esm/collections/workflows.d.ts +0 -3
  72. package/esm/collections/workflows.js +0 -63
  73. package/esm/collections/workflows.js.map +0 -1
  74. package/esm/constants.d.ts +0 -17
  75. package/esm/constants.js +0 -18
  76. package/esm/constants.js.map +0 -1
  77. package/esm/index.d.ts +0 -5
  78. package/esm/index.js +0 -6
  79. package/esm/index.js.map +0 -1
  80. package/esm/instructions/calculation.d.ts +0 -8
  81. package/esm/instructions/calculation.js +0 -55
  82. package/esm/instructions/calculation.js.map +0 -1
  83. package/esm/instructions/condition.d.ts +0 -5
  84. package/esm/instructions/condition.js +0 -99
  85. package/esm/instructions/condition.js.map +0 -1
  86. package/esm/instructions/create.d.ts +0 -8
  87. package/esm/instructions/create.js +0 -25
  88. package/esm/instructions/create.js.map +0 -1
  89. package/esm/instructions/destroy.d.ts +0 -8
  90. package/esm/instructions/destroy.js +0 -25
  91. package/esm/instructions/destroy.js.map +0 -1
  92. package/esm/instructions/index.d.ts +0 -15
  93. package/esm/instructions/index.js +0 -20
  94. package/esm/instructions/index.js.map +0 -1
  95. package/esm/instructions/parallel.d.ts +0 -13
  96. package/esm/instructions/parallel.js +0 -88
  97. package/esm/instructions/parallel.js.map +0 -1
  98. package/esm/instructions/prompt.d.ts +0 -7
  99. package/esm/instructions/prompt.js +0 -13
  100. package/esm/instructions/prompt.js.map +0 -1
  101. package/esm/instructions/query.d.ts +0 -8
  102. package/esm/instructions/query.js +0 -25
  103. package/esm/instructions/query.js.map +0 -1
  104. package/esm/instructions/update.d.ts +0 -8
  105. package/esm/instructions/update.js +0 -25
  106. package/esm/instructions/update.js.map +0 -1
  107. package/esm/models/Execution.d.ts +0 -50
  108. package/esm/models/Execution.js +0 -250
  109. package/esm/models/Execution.js.map +0 -1
  110. package/esm/models/FlowNode.d.ts +0 -17
  111. package/esm/models/FlowNode.js +0 -4
  112. package/esm/models/FlowNode.js.map +0 -1
  113. package/esm/models/Job.d.ts +0 -15
  114. package/esm/models/Job.js +0 -4
  115. package/esm/models/Job.js.map +0 -1
  116. package/esm/models/Workflow.d.ts +0 -27
  117. package/esm/models/Workflow.js +0 -72
  118. package/esm/models/Workflow.js.map +0 -1
  119. package/esm/server.d.ts +0 -5
  120. package/esm/server.js +0 -62
  121. package/esm/server.js.map +0 -1
  122. package/esm/triggers/index.d.ts +0 -9
  123. package/esm/triggers/index.js +0 -6
  124. package/esm/triggers/index.js.map +0 -1
  125. package/esm/triggers/model.d.ts +0 -12
  126. package/esm/triggers/model.js +0 -47
  127. package/esm/triggers/model.js.map +0 -1
  128. package/lib/actions/flow_nodes.d.ts +0 -3
  129. package/lib/actions/flow_nodes.js +0 -163
  130. package/lib/actions/flow_nodes.js.map +0 -1
  131. package/lib/actions/index.js.map +0 -1
  132. package/lib/calculators/index.js.map +0 -1
  133. package/lib/collections/executions.js.map +0 -1
  134. package/lib/collections/flow_nodes.js.map +0 -1
  135. package/lib/collections/jobs.js.map +0 -1
  136. package/lib/collections/workflows.js.map +0 -1
  137. package/lib/constants.js.map +0 -1
  138. package/lib/index.js.map +0 -1
  139. package/lib/instructions/calculation.js.map +0 -1
  140. package/lib/instructions/condition.js.map +0 -1
  141. package/lib/instructions/create.js.map +0 -1
  142. package/lib/instructions/destroy.js.map +0 -1
  143. package/lib/instructions/index.js.map +0 -1
  144. package/lib/instructions/parallel.js.map +0 -1
  145. package/lib/instructions/prompt.js.map +0 -1
  146. package/lib/instructions/query.js.map +0 -1
  147. package/lib/instructions/update.js.map +0 -1
  148. package/lib/models/Execution.js.map +0 -1
  149. package/lib/models/FlowNode.js.map +0 -1
  150. package/lib/models/Job.js.map +0 -1
  151. package/lib/models/Workflow.js.map +0 -1
  152. package/lib/server.d.ts +0 -5
  153. package/lib/server.js +0 -68
  154. package/lib/server.js.map +0 -1
  155. package/lib/triggers/index.js.map +0 -1
  156. package/lib/triggers/model.d.ts +0 -12
  157. package/lib/triggers/model.js +0 -49
  158. package/lib/triggers/model.js.map +0 -1
  159. package/tsconfig.build.json +0 -9
@@ -0,0 +1,18 @@
1
+ import { Plugin } from '@nocobase/server';
2
+ import { Transactionable } from '@nocobase/database';
3
+ import { Registry } from '@nocobase/utils';
4
+ import { Trigger } from './triggers';
5
+ import { Instruction } from './instructions';
6
+ import Processor from './Processor';
7
+ import WorkflowModel from './models/Workflow';
8
+ import ExecutionModel from './models/Execution';
9
+ export default class WorkflowPlugin extends Plugin {
10
+ instructions: Registry<Instruction>;
11
+ triggers: Registry<Trigger>;
12
+ onBeforeSave: (instance: WorkflowModel, options: any) => Promise<void>;
13
+ getName(): string;
14
+ load(): Promise<void>;
15
+ toggle(workflow: WorkflowModel, enable?: boolean): void;
16
+ trigger(workflow: any, context: Object, options?: Transactionable): Promise<any>;
17
+ createProcessor(execution: ExecutionModel, options?: {}): Processor;
18
+ }
package/lib/Plugin.js ADDED
@@ -0,0 +1,285 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ function _path() {
9
+ const data = _interopRequireDefault(require("path"));
10
+
11
+ _path = function _path() {
12
+ return data;
13
+ };
14
+
15
+ return data;
16
+ }
17
+
18
+ function _server() {
19
+ const data = require("@nocobase/server");
20
+
21
+ _server = function _server() {
22
+ return data;
23
+ };
24
+
25
+ return data;
26
+ }
27
+
28
+ function _database() {
29
+ const data = require("@nocobase/database");
30
+
31
+ _database = function _database() {
32
+ return data;
33
+ };
34
+
35
+ return data;
36
+ }
37
+
38
+ function _utils() {
39
+ const data = require("@nocobase/utils");
40
+
41
+ _utils = function _utils() {
42
+ return data;
43
+ };
44
+
45
+ return data;
46
+ }
47
+
48
+ var _actions = _interopRequireDefault(require("./actions"));
49
+
50
+ var _triggers = _interopRequireDefault(require("./triggers"));
51
+
52
+ var _instructions = _interopRequireDefault(require("./instructions"));
53
+
54
+ var _Processor = _interopRequireDefault(require("./Processor"));
55
+
56
+ var _constants = require("./constants");
57
+
58
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
59
+
60
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
61
+
62
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
63
+
64
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
65
+
66
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
67
+
68
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
69
+
70
+ class WorkflowPlugin extends _server().Plugin {
71
+ constructor(...args) {
72
+ var _this;
73
+
74
+ super(...args);
75
+ _this = this;
76
+ this.instructions = new (_utils().Registry)();
77
+ this.triggers = new (_utils().Registry)();
78
+
79
+ this.onBeforeSave = /*#__PURE__*/function () {
80
+ var _ref = _asyncToGenerator(function* (instance, options) {
81
+ const Model = instance.constructor;
82
+
83
+ if (instance.enabled) {
84
+ instance.set('current', true);
85
+ } else if (!instance.current) {
86
+ const count = yield Model.count({
87
+ where: {
88
+ key: instance.key
89
+ },
90
+ transaction: options.transaction
91
+ });
92
+
93
+ if (!count) {
94
+ instance.set('current', true);
95
+ }
96
+ }
97
+
98
+ if (!instance.changed('enabled') || !instance.enabled) {
99
+ return;
100
+ }
101
+
102
+ const previous = yield Model.findOne({
103
+ where: {
104
+ key: instance.key,
105
+ current: true,
106
+ id: {
107
+ [_database().Op.ne]: instance.id
108
+ }
109
+ },
110
+ transaction: options.transaction
111
+ });
112
+
113
+ if (previous) {
114
+ // NOTE: set to `null` but not `false` will not violate the unique index
115
+ yield previous.update({
116
+ enabled: false,
117
+ current: null
118
+ }, {
119
+ transaction: options.transaction,
120
+ hooks: false
121
+ });
122
+
123
+ _this.toggle(previous, false);
124
+ }
125
+ });
126
+
127
+ return function (_x, _x2) {
128
+ return _ref.apply(this, arguments);
129
+ };
130
+ }();
131
+ }
132
+
133
+ getName() {
134
+ return this.getPackageName(__dirname);
135
+ }
136
+
137
+ load() {
138
+ var _this2 = this;
139
+
140
+ return _asyncToGenerator(function* () {
141
+ const db = _this2.db,
142
+ options = _this2.options;
143
+ yield db.import({
144
+ directory: _path().default.resolve(__dirname, 'collections')
145
+ });
146
+ (0, _actions.default)(_this2);
147
+ (0, _triggers.default)(_this2, options.triggers);
148
+ (0, _instructions.default)(_this2, options.instructions);
149
+ db.on('workflows.beforeSave', _this2.onBeforeSave);
150
+ db.on('workflows.afterSave', model => _this2.toggle(model));
151
+ db.on('workflows.afterDestroy', model => _this2.toggle(model, false)); // [Life Cycle]:
152
+ // * load all workflows in db
153
+ // * add all hooks for enabled workflows
154
+ // * add hooks for create/update[enabled]/delete workflow to add/remove specific hooks
155
+
156
+ _this2.app.on('beforeStart', /*#__PURE__*/_asyncToGenerator(function* () {
157
+ const collection = db.getCollection('workflows');
158
+ const workflows = yield collection.repository.find({
159
+ filter: {
160
+ enabled: true
161
+ }
162
+ });
163
+ workflows.forEach(workflow => {
164
+ _this2.toggle(workflow);
165
+ });
166
+ }));
167
+
168
+ _this2.app.on('beforeStop', /*#__PURE__*/_asyncToGenerator(function* () {
169
+ const collection = db.getCollection('workflows');
170
+ const workflows = yield collection.repository.find({
171
+ filter: {
172
+ enabled: true
173
+ }
174
+ });
175
+ workflows.forEach(workflow => {
176
+ _this2.toggle(workflow, false);
177
+ });
178
+ }));
179
+ })();
180
+ }
181
+
182
+ toggle(workflow, enable) {
183
+ const type = workflow.get('type');
184
+ const trigger = this.triggers.get(type);
185
+
186
+ if (typeof enable !== 'undefined' ? enable : workflow.get('enabled')) {
187
+ // NOTE: remove previous listener if config updated
188
+ const prev = workflow.previous();
189
+
190
+ if (prev.config) {
191
+ trigger.off(_objectSpread(_objectSpread({}, workflow.get()), prev));
192
+ }
193
+
194
+ trigger.on(workflow);
195
+ } else {
196
+ trigger.off(workflow);
197
+ }
198
+ }
199
+
200
+ trigger(workflow, context, options = {}) {
201
+ var _this3 = this;
202
+
203
+ return _asyncToGenerator(function* () {
204
+ // `null` means not to trigger
205
+ if (context === null) {
206
+ return;
207
+ }
208
+
209
+ let transaction = null;
210
+
211
+ if (workflow.useTransaction) {
212
+ // @ts-ignore
213
+ transaction = options.transaction && !options.transaction.finished ? options.transaction : yield workflow.constructor.database.sequelize.transaction();
214
+ const existed = yield workflow.countExecutions({
215
+ where: {
216
+ transaction: transaction.id
217
+ },
218
+ transaction
219
+ });
220
+
221
+ if (existed) {
222
+ console.warn(`workflow ${workflow.id} has already been triggered in same execution (${transaction.id}), and newly triggering will be skipped.`);
223
+ return;
224
+ }
225
+ }
226
+
227
+ const execution = yield workflow.createExecution({
228
+ context,
229
+ key: workflow.key,
230
+ status: _constants.EXECUTION_STATUS.STARTED,
231
+ useTransaction: workflow.useTransaction,
232
+ transaction: transaction.id
233
+ }, {
234
+ transaction
235
+ });
236
+ const executed = yield workflow.countExecutions({
237
+ transaction
238
+ }); // NOTE: not to trigger afterUpdate hook here
239
+
240
+ yield workflow.update({
241
+ executed
242
+ }, {
243
+ transaction,
244
+ hooks: false
245
+ });
246
+ const allExecuted = yield execution.constructor.count({
247
+ where: {
248
+ key: workflow.key
249
+ },
250
+ transaction
251
+ });
252
+ yield workflow.constructor.update({
253
+ allExecuted
254
+ }, {
255
+ where: {
256
+ key: workflow.key
257
+ },
258
+ individualHooks: true,
259
+ transaction
260
+ });
261
+ execution.workflow = workflow;
262
+
263
+ const processor = _this3.createProcessor(execution, {
264
+ transaction
265
+ });
266
+
267
+ yield processor.start(); // @ts-ignore
268
+
269
+ if (transaction && (!options.transaction || options.transaction.finished)) {
270
+ yield transaction.commit();
271
+ }
272
+
273
+ return execution;
274
+ })();
275
+ }
276
+
277
+ createProcessor(execution, options = {}) {
278
+ return new _Processor.default(execution, _objectSpread(_objectSpread({}, options), {}, {
279
+ plugin: this
280
+ }));
281
+ }
282
+
283
+ }
284
+
285
+ exports.default = WorkflowPlugin;
@@ -0,0 +1,40 @@
1
+ import { Transaction, Transactionable } from 'sequelize';
2
+ import Plugin from '.';
3
+ import ExecutionModel from './models/Execution';
4
+ import JobModel from './models/Job';
5
+ import FlowNodeModel from './models/FlowNode';
6
+ export interface ProcessorOptions extends Transactionable {
7
+ plugin: Plugin;
8
+ }
9
+ export default class Processor {
10
+ execution: ExecutionModel;
11
+ private options;
12
+ static StatusMap: {
13
+ [x: number]: number;
14
+ };
15
+ transaction: Transaction;
16
+ nodes: FlowNodeModel[];
17
+ nodesMap: Map<number, FlowNodeModel>;
18
+ jobsMap: Map<number, JobModel>;
19
+ jobsMapByNodeId: {
20
+ [key: number]: any;
21
+ };
22
+ constructor(execution: ExecutionModel, options: ProcessorOptions);
23
+ private makeNodes;
24
+ private makeJobs;
25
+ private getTransaction;
26
+ prepare(commit?: boolean): Promise<void>;
27
+ start(): Promise<void>;
28
+ resume(job: JobModel): Promise<void>;
29
+ private commit;
30
+ private exec;
31
+ run(node: any, input?: any): any;
32
+ end(node: any, job: any): any;
33
+ recall(node: any, job: any): any;
34
+ exit(job: JobModel | null): Promise<any>;
35
+ saveJob(payload: any): Promise<any>;
36
+ findBranchStartNode(node: FlowNodeModel): FlowNodeModel | null;
37
+ findBranchParentNode(node: FlowNodeModel): FlowNodeModel | null;
38
+ findBranchParentJob(job: JobModel, node: FlowNodeModel): JobModel | null;
39
+ getParsedValue(value: any, node?: any): any;
40
+ }