@nocobase/plugin-workflow 0.7.0-alpha.34 → 0.7.0-alpha.57
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/lib/actions/index.js +31 -32
- package/lib/actions/nodes.js +310 -182
- package/lib/actions/workflows.js +260 -157
- package/lib/calculators/index.js +142 -90
- package/lib/collections/executions.js +40 -43
- package/lib/collections/flow_nodes.js +60 -65
- package/lib/collections/jobs.js +45 -47
- package/lib/collections/workflows.js +78 -87
- package/lib/constants.js +22 -17
- package/lib/index.js +71 -22
- package/lib/instructions/calculation.js +34 -29
- package/lib/instructions/condition.js +94 -87
- package/lib/instructions/create.js +43 -26
- package/lib/instructions/destroy.js +42 -25
- package/lib/instructions/index.js +46 -25
- package/lib/instructions/parallel.js +99 -84
- package/lib/instructions/prompt.js +21 -13
- package/lib/instructions/query.js +47 -29
- package/lib/instructions/update.js +44 -25
- package/lib/models/Execution.js +401 -248
- package/lib/models/FlowNode.js +18 -5
- package/lib/models/Job.js +18 -5
- package/lib/models/Workflow.js +132 -88
- package/lib/server.js +93 -66
- package/lib/triggers/collection.js +113 -51
- package/lib/triggers/index.js +25 -11
- package/package.json +7 -12
- package/esm/actions/index.d.ts +0 -1
- package/esm/actions/index.js +0 -14
- package/esm/actions/index.js.map +0 -1
- package/esm/actions/nodes.d.ts +0 -4
- package/esm/actions/nodes.js +0 -187
- package/esm/actions/nodes.js.map +0 -1
- package/esm/actions/workflows.d.ts +0 -3
- package/esm/actions/workflows.js +0 -163
- package/esm/actions/workflows.js.map +0 -1
- package/esm/calculators/index.d.ts +0 -38
- package/esm/calculators/index.js +0 -128
- package/esm/calculators/index.js.map +0 -1
- package/esm/collections/executions.d.ts +0 -3
- package/esm/collections/executions.js +0 -43
- package/esm/collections/executions.js.map +0 -1
- package/esm/collections/flow_nodes.d.ts +0 -3
- package/esm/collections/flow_nodes.js +0 -65
- package/esm/collections/flow_nodes.js.map +0 -1
- package/esm/collections/jobs.d.ts +0 -3
- package/esm/collections/jobs.js +0 -47
- package/esm/collections/jobs.js.map +0 -1
- package/esm/collections/workflows.d.ts +0 -3
- package/esm/collections/workflows.js +0 -87
- package/esm/collections/workflows.js.map +0 -1
- package/esm/constants.d.ts +0 -17
- package/esm/constants.js +0 -18
- package/esm/constants.js.map +0 -1
- package/esm/index.d.ts +0 -5
- package/esm/index.js +0 -6
- package/esm/index.js.map +0 -1
- package/esm/instructions/calculation.d.ts +0 -8
- package/esm/instructions/calculation.js +0 -55
- package/esm/instructions/calculation.js.map +0 -1
- package/esm/instructions/condition.d.ts +0 -5
- package/esm/instructions/condition.js +0 -99
- package/esm/instructions/condition.js.map +0 -1
- package/esm/instructions/create.d.ts +0 -8
- package/esm/instructions/create.js +0 -26
- package/esm/instructions/create.js.map +0 -1
- package/esm/instructions/destroy.d.ts +0 -8
- package/esm/instructions/destroy.js +0 -25
- package/esm/instructions/destroy.js.map +0 -1
- package/esm/instructions/index.d.ts +0 -15
- package/esm/instructions/index.js +0 -20
- package/esm/instructions/index.js.map +0 -1
- package/esm/instructions/parallel.d.ts +0 -13
- package/esm/instructions/parallel.js +0 -88
- package/esm/instructions/parallel.js.map +0 -1
- package/esm/instructions/prompt.d.ts +0 -7
- package/esm/instructions/prompt.js +0 -13
- package/esm/instructions/prompt.js.map +0 -1
- package/esm/instructions/query.d.ts +0 -8
- package/esm/instructions/query.js +0 -29
- package/esm/instructions/query.js.map +0 -1
- package/esm/instructions/update.d.ts +0 -9
- package/esm/instructions/update.js +0 -25
- package/esm/instructions/update.js.map +0 -1
- package/esm/models/Execution.d.ts +0 -51
- package/esm/models/Execution.js +0 -256
- package/esm/models/Execution.js.map +0 -1
- package/esm/models/FlowNode.d.ts +0 -17
- package/esm/models/FlowNode.js +0 -4
- package/esm/models/FlowNode.js.map +0 -1
- package/esm/models/Job.d.ts +0 -15
- package/esm/models/Job.js +0 -4
- package/esm/models/Job.js.map +0 -1
- package/esm/models/Workflow.d.ts +0 -29
- package/esm/models/Workflow.js +0 -88
- package/esm/models/Workflow.js.map +0 -1
- package/esm/server.d.ts +0 -5
- package/esm/server.js +0 -62
- package/esm/server.js.map +0 -1
- package/esm/triggers/collection.d.ts +0 -12
- package/esm/triggers/collection.js +0 -61
- package/esm/triggers/collection.js.map +0 -1
- package/esm/triggers/index.d.ts +0 -9
- package/esm/triggers/index.js +0 -6
- package/esm/triggers/index.js.map +0 -1
- package/lib/actions/index.js.map +0 -1
- package/lib/actions/nodes.js.map +0 -1
- package/lib/actions/workflows.js.map +0 -1
- package/lib/calculators/index.js.map +0 -1
- package/lib/collections/executions.js.map +0 -1
- package/lib/collections/flow_nodes.js.map +0 -1
- package/lib/collections/jobs.js.map +0 -1
- package/lib/collections/workflows.js.map +0 -1
- package/lib/constants.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/instructions/calculation.js.map +0 -1
- package/lib/instructions/condition.js.map +0 -1
- package/lib/instructions/create.js.map +0 -1
- package/lib/instructions/destroy.js.map +0 -1
- package/lib/instructions/index.js.map +0 -1
- package/lib/instructions/parallel.js.map +0 -1
- package/lib/instructions/prompt.js.map +0 -1
- package/lib/instructions/query.js.map +0 -1
- package/lib/instructions/update.js.map +0 -1
- package/lib/models/Execution.js.map +0 -1
- package/lib/models/FlowNode.js.map +0 -1
- package/lib/models/Job.js.map +0 -1
- package/lib/models/Workflow.js.map +0 -1
- package/lib/server.js.map +0 -1
- package/lib/triggers/collection.js.map +0 -1
- package/lib/triggers/index.js.map +0 -1
- package/tsconfig.build.json +0 -9
package/lib/models/Execution.js
CHANGED
|
@@ -1,262 +1,415 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
function _database() {
|
|
9
|
+
const data = require("@nocobase/database");
|
|
10
|
+
|
|
11
|
+
_database = function _database() {
|
|
12
|
+
return data;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
return data;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function _jsonTemplates() {
|
|
19
|
+
const data = _interopRequireDefault(require("json-templates"));
|
|
20
|
+
|
|
21
|
+
_jsonTemplates = function _jsonTemplates() {
|
|
22
|
+
return data;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
return data;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
var _constants = require("../constants");
|
|
29
|
+
|
|
30
|
+
var _instructions = _interopRequireDefault(require("../instructions"));
|
|
31
|
+
|
|
32
|
+
var _calculators = _interopRequireDefault(require("../calculators"));
|
|
33
|
+
|
|
34
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
35
|
+
|
|
36
|
+
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
37
|
+
|
|
38
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
39
|
+
|
|
40
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
41
|
+
|
|
42
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
43
|
+
|
|
44
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
45
|
+
|
|
46
|
+
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
47
|
+
|
|
48
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
49
|
+
|
|
50
|
+
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; }
|
|
51
|
+
|
|
52
|
+
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; }
|
|
53
|
+
|
|
54
|
+
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; }
|
|
55
|
+
|
|
56
|
+
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); } }
|
|
57
|
+
|
|
58
|
+
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); }); }; }
|
|
59
|
+
|
|
60
|
+
class ExecutionModel extends _database().Model {
|
|
61
|
+
constructor(...args) {
|
|
62
|
+
super(...args);
|
|
63
|
+
this.options = void 0;
|
|
64
|
+
this.tx = void 0;
|
|
65
|
+
this.nodes = [];
|
|
66
|
+
this.nodesMap = new Map();
|
|
67
|
+
this.jobsMap = new Map();
|
|
68
|
+
this.jobsMapByNodeId = {};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// make dual linked nodes list then cache
|
|
72
|
+
makeNodes(nodes = []) {
|
|
73
|
+
this.nodes = nodes;
|
|
74
|
+
nodes.forEach(node => {
|
|
75
|
+
this.nodesMap.set(node.id, node);
|
|
9
76
|
});
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
getTransaction() {
|
|
51
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
-
const { sequelize } = this.constructor.database;
|
|
53
|
-
if (!this.useTransaction) {
|
|
54
|
-
return undefined;
|
|
55
|
-
}
|
|
56
|
-
const { options } = this;
|
|
57
|
-
// @ts-ignore
|
|
58
|
-
const transaction = options.transaction && !options.transaction.finished
|
|
59
|
-
? options.transaction
|
|
60
|
-
: sequelize.transaction();
|
|
61
|
-
// @ts-ignore
|
|
62
|
-
if (this.transaction !== transaction.id) {
|
|
63
|
-
// @ts-ignore
|
|
64
|
-
yield this.update({ transaction: transaction.id }, { transaction });
|
|
65
|
-
}
|
|
66
|
-
return transaction;
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
prepare(options, commit = false) {
|
|
70
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
71
|
-
this.options = options || {};
|
|
72
|
-
const transaction = yield this.getTransaction();
|
|
73
|
-
this.tx = transaction;
|
|
74
|
-
if (!this.workflow) {
|
|
75
|
-
this.workflow = yield this.getWorkflow({ transaction });
|
|
76
|
-
}
|
|
77
|
-
const nodes = yield this.workflow.getNodes({ transaction });
|
|
78
|
-
this.makeNodes(nodes);
|
|
79
|
-
const jobs = yield this.getJobs({
|
|
80
|
-
order: [['id', 'ASC']],
|
|
81
|
-
transaction,
|
|
82
|
-
});
|
|
83
|
-
this.makeJobs(jobs);
|
|
84
|
-
if (commit) {
|
|
85
|
-
yield this.commit();
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
start(options) {
|
|
90
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
91
|
-
if (this.status !== constants_1.EXECUTION_STATUS.STARTED) {
|
|
92
|
-
throw new Error(`execution was ended with status ${this.status}`);
|
|
93
|
-
}
|
|
94
|
-
yield this.prepare(options);
|
|
95
|
-
if (this.nodes.length) {
|
|
96
|
-
const head = this.nodes.find(item => !item.upstream);
|
|
97
|
-
yield this.run(head, { result: this.context });
|
|
98
|
-
}
|
|
99
|
-
else {
|
|
100
|
-
yield this.exit(null);
|
|
101
|
-
}
|
|
102
|
-
yield this.commit();
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
resume(job, options) {
|
|
106
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
107
|
-
if (this.status !== constants_1.EXECUTION_STATUS.STARTED) {
|
|
108
|
-
throw new Error(`execution was ended with status ${this.status}`);
|
|
109
|
-
}
|
|
110
|
-
yield this.prepare(options);
|
|
111
|
-
const node = this.nodesMap.get(job.nodeId);
|
|
112
|
-
yield this.recall(node, job);
|
|
113
|
-
yield this.commit();
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
commit() {
|
|
117
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
118
|
-
// @ts-ignore
|
|
119
|
-
if (this.tx && (!this.options.transaction || this.options.transaction.finished)) {
|
|
120
|
-
yield this.tx.commit();
|
|
121
|
-
}
|
|
77
|
+
nodes.forEach(node => {
|
|
78
|
+
if (node.upstreamId) {
|
|
79
|
+
node.upstream = this.nodesMap.get(node.upstreamId);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (node.downstreamId) {
|
|
83
|
+
node.downstream = this.nodesMap.get(node.downstreamId);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
makeJobs(jobs) {
|
|
89
|
+
jobs.forEach(job => {
|
|
90
|
+
this.jobsMap.set(job.id, job); // TODO: should consider cycle, and from previous job
|
|
91
|
+
|
|
92
|
+
this.jobsMapByNodeId[job.nodeId] = job.result;
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
getTransaction() {
|
|
97
|
+
var _this = this;
|
|
98
|
+
|
|
99
|
+
return _asyncToGenerator(function* () {
|
|
100
|
+
const sequelize = _this.constructor.database.sequelize;
|
|
101
|
+
|
|
102
|
+
if (!_this.useTransaction) {
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const options = _this.options; // @ts-ignore
|
|
107
|
+
|
|
108
|
+
const transaction = options.transaction && !options.transaction.finished ? options.transaction : sequelize.transaction(); // @ts-ignore
|
|
109
|
+
|
|
110
|
+
if (_this.transaction !== transaction.id) {
|
|
111
|
+
// @ts-ignore
|
|
112
|
+
yield _this.update({
|
|
113
|
+
transaction: transaction.id
|
|
114
|
+
}, {
|
|
115
|
+
transaction
|
|
122
116
|
});
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
status: constants_1.JOB_STATUS.REJECTED,
|
|
141
|
-
};
|
|
142
|
-
// if previous job is from resuming
|
|
143
|
-
if (prevJob && prevJob.nodeId === node.id) {
|
|
144
|
-
prevJob.set(job);
|
|
145
|
-
job = prevJob;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
let savedJob;
|
|
149
|
-
// TODO(optimize): many checking of resuming or new could be improved
|
|
150
|
-
// could be implemented separately in exec() / resume()
|
|
151
|
-
if (job instanceof database_1.Model) {
|
|
152
|
-
savedJob = (yield job.save({ transaction: this.tx }));
|
|
153
|
-
}
|
|
154
|
-
else {
|
|
155
|
-
const upstreamId = prevJob instanceof database_1.Model ? prevJob.get('id') : null;
|
|
156
|
-
savedJob = yield this.saveJob(Object.assign({ nodeId: node.id, upstreamId }, job));
|
|
157
|
-
}
|
|
158
|
-
if (savedJob.status === constants_1.JOB_STATUS.RESOLVED && node.downstream) {
|
|
159
|
-
// run next node
|
|
160
|
-
return this.run(node.downstream, savedJob);
|
|
161
|
-
}
|
|
162
|
-
// all nodes in scope have been executed
|
|
163
|
-
return this.end(node, savedJob);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return transaction;
|
|
120
|
+
})();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
prepare(options, commit = false) {
|
|
124
|
+
var _this2 = this;
|
|
125
|
+
|
|
126
|
+
return _asyncToGenerator(function* () {
|
|
127
|
+
_this2.options = options || {};
|
|
128
|
+
const transaction = yield _this2.getTransaction();
|
|
129
|
+
_this2.tx = transaction;
|
|
130
|
+
|
|
131
|
+
if (!_this2.workflow) {
|
|
132
|
+
_this2.workflow = yield _this2.getWorkflow({
|
|
133
|
+
transaction
|
|
164
134
|
});
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const nodes = yield _this2.workflow.getNodes({
|
|
138
|
+
transaction
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
_this2.makeNodes(nodes);
|
|
142
|
+
|
|
143
|
+
const jobs = yield _this2.getJobs({
|
|
144
|
+
order: [['id', 'ASC']],
|
|
145
|
+
transaction
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
_this2.makeJobs(jobs);
|
|
149
|
+
|
|
150
|
+
if (commit) {
|
|
151
|
+
yield _this2.commit();
|
|
152
|
+
}
|
|
153
|
+
})();
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
start(options) {
|
|
157
|
+
var _this3 = this;
|
|
158
|
+
|
|
159
|
+
return _asyncToGenerator(function* () {
|
|
160
|
+
if (_this3.status !== _constants.EXECUTION_STATUS.STARTED) {
|
|
161
|
+
throw new Error(`execution was ended with status ${_this3.status}`);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
yield _this3.prepare(options);
|
|
165
|
+
|
|
166
|
+
if (_this3.nodes.length) {
|
|
167
|
+
const head = _this3.nodes.find(item => !item.upstream);
|
|
168
|
+
|
|
169
|
+
yield _this3.run(head, {
|
|
170
|
+
result: _this3.context
|
|
173
171
|
});
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
172
|
+
} else {
|
|
173
|
+
yield _this3.exit(null);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
yield _this3.commit();
|
|
177
|
+
})();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
resume(job, options) {
|
|
181
|
+
var _this4 = this;
|
|
182
|
+
|
|
183
|
+
return _asyncToGenerator(function* () {
|
|
184
|
+
if (_this4.status !== _constants.EXECUTION_STATUS.STARTED) {
|
|
185
|
+
throw new Error(`execution was ended with status ${_this4.status}`);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
yield _this4.prepare(options);
|
|
189
|
+
|
|
190
|
+
const node = _this4.nodesMap.get(job.nodeId);
|
|
191
|
+
|
|
192
|
+
yield _this4.recall(node, job);
|
|
193
|
+
yield _this4.commit();
|
|
194
|
+
})();
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
commit() {
|
|
198
|
+
var _this5 = this;
|
|
199
|
+
|
|
200
|
+
return _asyncToGenerator(function* () {
|
|
201
|
+
// @ts-ignore
|
|
202
|
+
if (_this5.tx && (!_this5.options.transaction || _this5.options.transaction.finished)) {
|
|
203
|
+
yield _this5.tx.commit();
|
|
204
|
+
}
|
|
205
|
+
})();
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
exec(instruction, node, prevJob) {
|
|
209
|
+
var _this6 = this;
|
|
210
|
+
|
|
211
|
+
return _asyncToGenerator(function* () {
|
|
212
|
+
let job;
|
|
213
|
+
|
|
214
|
+
try {
|
|
215
|
+
// call instruction to get result and status
|
|
216
|
+
job = yield instruction.call(node, prevJob, _this6);
|
|
217
|
+
|
|
218
|
+
if (!job) {
|
|
219
|
+
return null;
|
|
181
220
|
}
|
|
182
|
-
|
|
183
|
-
//
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
exit(job) {
|
|
196
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
197
|
-
const status = job ? ExecutionModel.StatusMap[job.status] : constants_1.EXECUTION_STATUS.RESOLVED;
|
|
198
|
-
yield this.update({ status }, { transaction: this.tx });
|
|
199
|
-
return null;
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
// TODO(optimize)
|
|
203
|
-
saveJob(payload) {
|
|
204
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
205
|
-
const { database } = this.constructor;
|
|
206
|
-
const { model } = database.getCollection('jobs');
|
|
207
|
-
const [job] = (yield model.upsert(Object.assign(Object.assign({}, payload), { executionId: this.id }), { transaction: this.tx }));
|
|
208
|
-
this.jobsMap.set(job.id, job);
|
|
209
|
-
this.jobsMapByNodeId[job.nodeId] = job.result;
|
|
210
|
-
return job;
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
// find the first node in current branch
|
|
214
|
-
findBranchStartNode(node) {
|
|
215
|
-
for (let n = node; n; n = n.upstream) {
|
|
216
|
-
if (n.branchIndex !== null) {
|
|
217
|
-
return n;
|
|
218
|
-
}
|
|
221
|
+
} catch (err) {
|
|
222
|
+
// for uncaught error, set to rejected
|
|
223
|
+
job = {
|
|
224
|
+
result: err instanceof Error ? {
|
|
225
|
+
message: err.message,
|
|
226
|
+
stack: process.env.NODE_ENV === 'production' ? [] : err.stack
|
|
227
|
+
} : err,
|
|
228
|
+
status: _constants.JOB_STATUS.REJECTED
|
|
229
|
+
}; // if previous job is from resuming
|
|
230
|
+
|
|
231
|
+
if (prevJob && prevJob.nodeId === node.id) {
|
|
232
|
+
prevJob.set(job);
|
|
233
|
+
job = prevJob;
|
|
219
234
|
}
|
|
220
|
-
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
let savedJob; // TODO(optimize): many checking of resuming or new could be improved
|
|
238
|
+
// could be implemented separately in exec() / resume()
|
|
239
|
+
|
|
240
|
+
if (job instanceof _database().Model) {
|
|
241
|
+
savedJob = yield job.save({
|
|
242
|
+
transaction: _this6.tx
|
|
243
|
+
});
|
|
244
|
+
} else {
|
|
245
|
+
const upstreamId = prevJob instanceof _database().Model ? prevJob.get('id') : null;
|
|
246
|
+
savedJob = yield _this6.saveJob(_objectSpread({
|
|
247
|
+
nodeId: node.id,
|
|
248
|
+
upstreamId
|
|
249
|
+
}, job));
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (savedJob.status === _constants.JOB_STATUS.RESOLVED && node.downstream) {
|
|
253
|
+
// run next node
|
|
254
|
+
return _this6.run(node.downstream, savedJob);
|
|
255
|
+
} // all nodes in scope have been executed
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
return _this6.end(node, savedJob);
|
|
259
|
+
})();
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
run(node, input) {
|
|
263
|
+
var _this7 = this;
|
|
264
|
+
|
|
265
|
+
return _asyncToGenerator(function* () {
|
|
266
|
+
const _instructions$get = _instructions.default.get(node.type),
|
|
267
|
+
run = _instructions$get.run;
|
|
268
|
+
|
|
269
|
+
if (typeof run !== 'function') {
|
|
270
|
+
return Promise.reject(new Error('`run` should be implemented for customized execution of the node'));
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return _this7.exec(run, node, input);
|
|
274
|
+
})();
|
|
275
|
+
} // parent node should take over the control
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
end(node, job) {
|
|
279
|
+
const parentNode = this.findBranchParentNode(node); // no parent, means on main flow
|
|
280
|
+
|
|
281
|
+
if (parentNode) {
|
|
282
|
+
return this.recall(parentNode, job);
|
|
283
|
+
} // really done for all nodes
|
|
284
|
+
// * should mark execution as done with last job status
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
return this.exit(job);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
recall(node, job) {
|
|
291
|
+
var _this8 = this;
|
|
292
|
+
|
|
293
|
+
return _asyncToGenerator(function* () {
|
|
294
|
+
const _instructions$get2 = _instructions.default.get(node.type),
|
|
295
|
+
resume = _instructions$get2.resume;
|
|
296
|
+
|
|
297
|
+
if (typeof resume !== 'function') {
|
|
298
|
+
return Promise.reject(new Error('`resume` should be implemented because the node made branch'));
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
return _this8.exec(resume, node, job);
|
|
302
|
+
})();
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
exit(job) {
|
|
306
|
+
var _this9 = this;
|
|
307
|
+
|
|
308
|
+
return _asyncToGenerator(function* () {
|
|
309
|
+
const status = job ? ExecutionModel.StatusMap[job.status] : _constants.EXECUTION_STATUS.RESOLVED;
|
|
310
|
+
yield _this9.update({
|
|
311
|
+
status
|
|
312
|
+
}, {
|
|
313
|
+
transaction: _this9.tx
|
|
314
|
+
});
|
|
315
|
+
return null;
|
|
316
|
+
})();
|
|
317
|
+
} // TODO(optimize)
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
saveJob(payload) {
|
|
321
|
+
var _this10 = this;
|
|
322
|
+
|
|
323
|
+
return _asyncToGenerator(function* () {
|
|
324
|
+
const database = _this10.constructor.database;
|
|
325
|
+
|
|
326
|
+
const _database$getCollecti = database.getCollection('jobs'),
|
|
327
|
+
model = _database$getCollecti.model;
|
|
328
|
+
|
|
329
|
+
const _yield$model$upsert = yield model.upsert(_objectSpread(_objectSpread({}, payload), {}, {
|
|
330
|
+
executionId: _this10.id
|
|
331
|
+
}), {
|
|
332
|
+
transaction: _this10.tx
|
|
333
|
+
}),
|
|
334
|
+
_yield$model$upsert2 = _slicedToArray(_yield$model$upsert, 1),
|
|
335
|
+
job = _yield$model$upsert2[0];
|
|
336
|
+
|
|
337
|
+
_this10.jobsMap.set(job.id, job);
|
|
338
|
+
|
|
339
|
+
_this10.jobsMapByNodeId[job.nodeId] = job.result;
|
|
340
|
+
return job;
|
|
341
|
+
})();
|
|
342
|
+
} // find the first node in current branch
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
findBranchStartNode(node) {
|
|
346
|
+
for (let n = node; n; n = n.upstream) {
|
|
347
|
+
if (n.branchIndex !== null) {
|
|
348
|
+
return n;
|
|
349
|
+
}
|
|
221
350
|
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
351
|
+
|
|
352
|
+
return null;
|
|
353
|
+
} // find the node start current branch
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
findBranchParentNode(node) {
|
|
357
|
+
for (let n = node; n; n = n.upstream) {
|
|
358
|
+
if (n.branchIndex !== null) {
|
|
359
|
+
return n.upstream;
|
|
360
|
+
}
|
|
230
361
|
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
362
|
+
|
|
363
|
+
return null;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
findBranchParentJob(job, node) {
|
|
367
|
+
for (let j = job; j; j = this.jobsMap.get(j.upstreamId)) {
|
|
368
|
+
if (j.nodeId === node.id) {
|
|
369
|
+
return j;
|
|
370
|
+
}
|
|
238
371
|
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
372
|
+
|
|
373
|
+
return null;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
getParsedValue(value, node) {
|
|
377
|
+
const injectedFns = {};
|
|
378
|
+
const scope = {
|
|
379
|
+
execution: this,
|
|
380
|
+
node
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
var _iterator = _createForOfIteratorHelper(_calculators.default.getEntities()),
|
|
384
|
+
_step;
|
|
385
|
+
|
|
386
|
+
try {
|
|
387
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
388
|
+
let _step$value = _slicedToArray(_step.value, 2),
|
|
389
|
+
name = _step$value[0],
|
|
390
|
+
fn = _step$value[1];
|
|
391
|
+
|
|
392
|
+
injectedFns[name] = fn.bind(scope);
|
|
393
|
+
}
|
|
394
|
+
} catch (err) {
|
|
395
|
+
_iterator.e(err);
|
|
396
|
+
} finally {
|
|
397
|
+
_iterator.f();
|
|
253
398
|
}
|
|
399
|
+
|
|
400
|
+
return (0, _jsonTemplates().default)(value)({
|
|
401
|
+
$context: this.context,
|
|
402
|
+
$jobsMapByNodeId: this.jobsMapByNodeId,
|
|
403
|
+
$fn: injectedFns
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
|
|
254
407
|
}
|
|
408
|
+
|
|
255
409
|
exports.default = ExecutionModel;
|
|
256
410
|
ExecutionModel.StatusMap = {
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
};
|
|
262
|
-
//# sourceMappingURL=Execution.js.map
|
|
411
|
+
[_constants.JOB_STATUS.PENDING]: _constants.EXECUTION_STATUS.STARTED,
|
|
412
|
+
[_constants.JOB_STATUS.RESOLVED]: _constants.EXECUTION_STATUS.RESOLVED,
|
|
413
|
+
[_constants.JOB_STATUS.REJECTED]: _constants.EXECUTION_STATUS.REJECTED,
|
|
414
|
+
[_constants.JOB_STATUS.CANCELLED]: _constants.EXECUTION_STATUS.CANCELLED
|
|
415
|
+
};
|