@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.
- package/lib/Plugin.d.ts +18 -0
- package/lib/Plugin.js +285 -0
- package/lib/Processor.d.ts +40 -0
- package/lib/Processor.js +440 -0
- package/lib/actions/index.d.ts +3 -1
- package/lib/actions/index.js +33 -26
- package/{esm/actions/flow_nodes.d.ts → lib/actions/nodes.d.ts} +1 -0
- package/lib/actions/nodes.js +321 -0
- package/lib/actions/workflows.d.ts +2 -0
- package/lib/actions/workflows.js +197 -0
- package/lib/calculators/index.d.ts +2 -2
- package/lib/calculators/index.js +144 -92
- package/lib/collections/executions.js +32 -38
- package/lib/collections/flow_nodes.js +60 -72
- package/lib/collections/jobs.js +26 -47
- package/lib/collections/workflows.d.ts +1 -2
- package/lib/collections/workflows.js +70 -63
- package/lib/constants.js +22 -17
- package/lib/index.d.ts +4 -3
- package/lib/index.js +85 -22
- package/lib/instructions/calculation.d.ts +1 -1
- package/lib/instructions/calculation.js +32 -29
- package/lib/instructions/condition.d.ts +2 -2
- package/lib/instructions/condition.js +91 -88
- package/lib/instructions/create.d.ts +2 -2
- package/lib/instructions/create.js +39 -25
- package/lib/instructions/delay.d.ts +14 -0
- package/lib/instructions/delay.js +138 -0
- package/lib/instructions/destroy.d.ts +1 -1
- package/lib/instructions/destroy.js +38 -25
- package/lib/instructions/index.d.ts +11 -8
- package/lib/instructions/index.js +76 -25
- package/lib/instructions/parallel.d.ts +3 -3
- package/lib/instructions/parallel.js +95 -84
- package/lib/instructions/prompt.d.ts +2 -2
- package/lib/instructions/prompt.js +21 -13
- package/lib/instructions/query.d.ts +2 -1
- package/lib/instructions/query.js +42 -25
- package/lib/instructions/update.d.ts +2 -1
- package/lib/instructions/update.js +40 -25
- package/lib/models/Execution.d.ts +3 -33
- package/lib/models/Execution.js +18 -254
- package/lib/models/FlowNode.js +18 -5
- package/lib/models/Job.js +18 -5
- package/lib/models/Workflow.d.ts +5 -6
- package/lib/models/Workflow.js +18 -76
- package/lib/triggers/collection.d.ts +12 -0
- package/lib/triggers/collection.js +184 -0
- package/lib/triggers/index.d.ts +11 -7
- package/lib/triggers/index.js +64 -11
- package/lib/triggers/schedule.d.ts +38 -0
- package/lib/triggers/schedule.js +672 -0
- package/package.json +8 -12
- package/esm/actions/flow_nodes.js +0 -139
- package/esm/actions/flow_nodes.js.map +0 -1
- package/esm/actions/index.d.ts +0 -1
- package/esm/actions/index.js +0 -8
- package/esm/actions/index.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 -38
- package/esm/collections/executions.js.map +0 -1
- package/esm/collections/flow_nodes.d.ts +0 -3
- package/esm/collections/flow_nodes.js +0 -72
- 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 -63
- 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 -25
- 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 -25
- package/esm/instructions/query.js.map +0 -1
- package/esm/instructions/update.d.ts +0 -8
- package/esm/instructions/update.js +0 -25
- package/esm/instructions/update.js.map +0 -1
- package/esm/models/Execution.d.ts +0 -50
- package/esm/models/Execution.js +0 -250
- 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 -27
- package/esm/models/Workflow.js +0 -72
- 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/index.d.ts +0 -9
- package/esm/triggers/index.js +0 -6
- package/esm/triggers/index.js.map +0 -1
- package/esm/triggers/model.d.ts +0 -12
- package/esm/triggers/model.js +0 -47
- package/esm/triggers/model.js.map +0 -1
- package/lib/actions/flow_nodes.d.ts +0 -3
- package/lib/actions/flow_nodes.js +0 -163
- package/lib/actions/flow_nodes.js.map +0 -1
- package/lib/actions/index.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.d.ts +0 -5
- package/lib/server.js +0 -68
- package/lib/server.js.map +0 -1
- package/lib/triggers/index.js.map +0 -1
- package/lib/triggers/model.d.ts +0 -12
- package/lib/triggers/model.js +0 -49
- package/lib/triggers/model.js.map +0 -1
- package/tsconfig.build.json +0 -9
|
@@ -0,0 +1,672 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = exports.SCHEDULE_MODE = void 0;
|
|
7
|
+
|
|
8
|
+
function _cronParser() {
|
|
9
|
+
const data = _interopRequireDefault(require("cron-parser"));
|
|
10
|
+
|
|
11
|
+
_cronParser = function _cronParser() {
|
|
12
|
+
return data;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
return data;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function _sequelize() {
|
|
19
|
+
const data = require("sequelize");
|
|
20
|
+
|
|
21
|
+
_sequelize = function _sequelize() {
|
|
22
|
+
return data;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
return data;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
var _ = require("..");
|
|
29
|
+
|
|
30
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
31
|
+
|
|
32
|
+
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(_e) { throw _e; }, 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(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
33
|
+
|
|
34
|
+
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); }
|
|
35
|
+
|
|
36
|
+
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; }
|
|
37
|
+
|
|
38
|
+
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); } }
|
|
39
|
+
|
|
40
|
+
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); }); }; }
|
|
41
|
+
|
|
42
|
+
function _asyncIterator(iterable) { var method, async, sync, retry = 2; for ("undefined" != typeof Symbol && (async = Symbol.asyncIterator, sync = Symbol.iterator); retry--;) { if (async && null != (method = iterable[async])) return method.call(iterable); if (sync && null != (method = iterable[sync])) return new AsyncFromSyncIterator(method.call(iterable)); async = "@@asyncIterator", sync = "@@iterator"; } throw new TypeError("Object is not async iterable"); }
|
|
43
|
+
|
|
44
|
+
function AsyncFromSyncIterator(s) { function AsyncFromSyncIteratorContinuation(r) { if (Object(r) !== r) return Promise.reject(new TypeError(r + " is not an object.")); var done = r.done; return Promise.resolve(r.value).then(function (value) { return { value: value, done: done }; }); } return AsyncFromSyncIterator = function AsyncFromSyncIterator(s) { this.s = s, this.n = s.next; }, AsyncFromSyncIterator.prototype = { s: null, n: null, next: function next() { return AsyncFromSyncIteratorContinuation(this.n.apply(this.s, arguments)); }, return: function _return(value) { var ret = this.s.return; return void 0 === ret ? Promise.resolve({ value: value, done: !0 }) : AsyncFromSyncIteratorContinuation(ret.apply(this.s, arguments)); }, throw: function _throw(value) { var thr = this.s.return; return void 0 === thr ? Promise.reject(value) : AsyncFromSyncIteratorContinuation(thr.apply(this.s, arguments)); } }, new AsyncFromSyncIterator(s); }
|
|
45
|
+
|
|
46
|
+
const SCHEDULE_MODE = {
|
|
47
|
+
CONSTANT: 0,
|
|
48
|
+
COLLECTION_FIELD: 1
|
|
49
|
+
};
|
|
50
|
+
exports.SCHEDULE_MODE = SCHEDULE_MODE;
|
|
51
|
+
const ScheduleModes = new Map();
|
|
52
|
+
ScheduleModes.set(SCHEDULE_MODE.CONSTANT, {
|
|
53
|
+
shouldCache(workflow, now) {
|
|
54
|
+
const _workflow$config = workflow.config,
|
|
55
|
+
startsOn = _workflow$config.startsOn,
|
|
56
|
+
endsOn = _workflow$config.endsOn,
|
|
57
|
+
repeat = _workflow$config.repeat;
|
|
58
|
+
const timestamp = now.getTime();
|
|
59
|
+
|
|
60
|
+
if (startsOn) {
|
|
61
|
+
const startTime = Date.parse(startsOn);
|
|
62
|
+
|
|
63
|
+
if (!startTime || startTime > timestamp + this.cacheCycle) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (typeof repeat === 'number' && repeat > this.cacheCycle && (timestamp - startTime) % repeat > this.cacheCycle) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (endsOn) {
|
|
73
|
+
const endTime = Date.parse(endsOn);
|
|
74
|
+
|
|
75
|
+
if (!endTime || endTime <= timestamp) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return true;
|
|
81
|
+
},
|
|
82
|
+
|
|
83
|
+
trigger(workflow, date) {
|
|
84
|
+
const _workflow$config2 = workflow.config,
|
|
85
|
+
startsOn = _workflow$config2.startsOn,
|
|
86
|
+
endsOn = _workflow$config2.endsOn,
|
|
87
|
+
repeat = _workflow$config2.repeat;
|
|
88
|
+
|
|
89
|
+
if (startsOn && typeof repeat === 'number') {
|
|
90
|
+
const startTime = Date.parse(startsOn);
|
|
91
|
+
|
|
92
|
+
if ((startTime - date.getTime()) % repeat) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return this.plugin.trigger(workflow, {
|
|
98
|
+
date
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
function getDateRangeFilter(on, now, dir) {
|
|
105
|
+
const timestamp = now.getTime();
|
|
106
|
+
const op = dir < 0 ? _sequelize().Op.lt : _sequelize().Op.gte;
|
|
107
|
+
|
|
108
|
+
switch (typeof on) {
|
|
109
|
+
case 'string':
|
|
110
|
+
const time = Date.parse(on);
|
|
111
|
+
|
|
112
|
+
if (!time || (dir < 0 ? timestamp < time : time <= timestamp)) {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
break;
|
|
117
|
+
|
|
118
|
+
case 'object':
|
|
119
|
+
const field = on.field,
|
|
120
|
+
_on$offset = on.offset,
|
|
121
|
+
offset = _on$offset === void 0 ? 0 : _on$offset,
|
|
122
|
+
_on$unit = on.unit,
|
|
123
|
+
unit = _on$unit === void 0 ? 1000 : _on$unit;
|
|
124
|
+
|
|
125
|
+
if (!field) {
|
|
126
|
+
return {};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
[field]: {
|
|
131
|
+
[op]: new Date(timestamp + offset * unit * dir)
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
default:
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return {};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function getDataOptionTime(data, on, dir = 1) {
|
|
143
|
+
switch (typeof on) {
|
|
144
|
+
case 'string':
|
|
145
|
+
const time = Date.parse(on);
|
|
146
|
+
return time ? time : null;
|
|
147
|
+
|
|
148
|
+
case 'object':
|
|
149
|
+
const field = on.field,
|
|
150
|
+
_on$offset2 = on.offset,
|
|
151
|
+
offset = _on$offset2 === void 0 ? 0 : _on$offset2,
|
|
152
|
+
_on$unit2 = on.unit,
|
|
153
|
+
unit = _on$unit2 === void 0 ? 1000 : _on$unit2;
|
|
154
|
+
return data[field] ? data[field].getTime() - offset * unit * dir : null;
|
|
155
|
+
|
|
156
|
+
default:
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function getHookId(workflow, type) {
|
|
162
|
+
return `${type}#${workflow.id}`;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const DialectTimestampFnMap = {
|
|
166
|
+
postgres(col) {
|
|
167
|
+
return `CAST(FLOOR(extract(epoch from "${col}")) AS INTEGER)`;
|
|
168
|
+
},
|
|
169
|
+
|
|
170
|
+
mysql(col) {
|
|
171
|
+
return `CAST(FLOOR(UNIX_TIMESTAMP(\`${col}\`)) AS SIGNED INTEGER)`;
|
|
172
|
+
},
|
|
173
|
+
|
|
174
|
+
sqlite(col) {
|
|
175
|
+
return `CAST(FLOOR(unixepoch(${col})) AS INTEGER)`;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
};
|
|
179
|
+
DialectTimestampFnMap.mariadb = DialectTimestampFnMap.mysql;
|
|
180
|
+
ScheduleModes.set(SCHEDULE_MODE.COLLECTION_FIELD, {
|
|
181
|
+
on(workflow) {
|
|
182
|
+
var _this = this;
|
|
183
|
+
|
|
184
|
+
const _workflow$config3 = workflow.config,
|
|
185
|
+
collection = _workflow$config3.collection,
|
|
186
|
+
startsOn = _workflow$config3.startsOn,
|
|
187
|
+
endsOn = _workflow$config3.endsOn,
|
|
188
|
+
repeat = _workflow$config3.repeat;
|
|
189
|
+
const event = `${collection}.afterSave`;
|
|
190
|
+
const name = getHookId(workflow, event);
|
|
191
|
+
|
|
192
|
+
if (!this.events.has(name)) {
|
|
193
|
+
// NOTE: toggle cache depends on new date
|
|
194
|
+
const listener = /*#__PURE__*/function () {
|
|
195
|
+
var _ref = _asyncToGenerator(function* (data, options) {
|
|
196
|
+
const now = new Date();
|
|
197
|
+
now.setMilliseconds(0);
|
|
198
|
+
const timestamp = now.getTime();
|
|
199
|
+
const startTime = getDataOptionTime(data, startsOn);
|
|
200
|
+
const endTime = getDataOptionTime(data, endsOn, -1);
|
|
201
|
+
|
|
202
|
+
if (!startTime && !repeat) {
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (startTime && startTime > timestamp + _this.cacheCycle) {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (endTime && endTime <= timestamp) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (!nextInCycle.call(_this, workflow, now)) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (typeof repeat === 'number' && repeat > _this.cacheCycle && (timestamp - startTime) % repeat > _this.cacheCycle) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
console.log('set cache', now);
|
|
223
|
+
|
|
224
|
+
_this.setCache(workflow);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
return function listener(_x, _x2) {
|
|
228
|
+
return _ref.apply(this, arguments);
|
|
229
|
+
};
|
|
230
|
+
}();
|
|
231
|
+
|
|
232
|
+
this.events.set(name, listener);
|
|
233
|
+
this.plugin.app.db.on(`${collection}.afterSave`, listener);
|
|
234
|
+
}
|
|
235
|
+
},
|
|
236
|
+
|
|
237
|
+
off(workflow) {
|
|
238
|
+
const collection = workflow.config.collection;
|
|
239
|
+
const event = `${collection}.afterSave`;
|
|
240
|
+
const name = getHookId(workflow, event);
|
|
241
|
+
|
|
242
|
+
if (this.events.has(name)) {
|
|
243
|
+
const listener = this.events.get(name);
|
|
244
|
+
this.events.delete(name);
|
|
245
|
+
this.plugin.app.db.off(`${collection}.afterSave`, listener);
|
|
246
|
+
}
|
|
247
|
+
},
|
|
248
|
+
|
|
249
|
+
shouldCache(workflow, now) {
|
|
250
|
+
var _this2 = this;
|
|
251
|
+
|
|
252
|
+
return _asyncToGenerator(function* () {
|
|
253
|
+
const _workflow$config4 = workflow.config,
|
|
254
|
+
startsOn = _workflow$config4.startsOn,
|
|
255
|
+
endsOn = _workflow$config4.endsOn,
|
|
256
|
+
repeat = _workflow$config4.repeat,
|
|
257
|
+
collection = _workflow$config4.collection;
|
|
258
|
+
const starts = getDateRangeFilter(startsOn, now, -1);
|
|
259
|
+
|
|
260
|
+
if (!starts || !Object.keys(starts).length) {
|
|
261
|
+
return false;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const ends = getDateRangeFilter(endsOn, now, 1);
|
|
265
|
+
|
|
266
|
+
if (!ends) {
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const conditions = [starts, ends].filter(item => Boolean(Object.keys(item).length)); // when repeat is number, means repeat after startsOn
|
|
271
|
+
// (now - startsOn) % repeat <= cacheCycle
|
|
272
|
+
|
|
273
|
+
const db = _this2.plugin.app.db;
|
|
274
|
+
const tsFn = DialectTimestampFnMap[db.options.dialect];
|
|
275
|
+
|
|
276
|
+
if (repeat && typeof repeat === 'number' && repeat > _this2.cacheCycle && tsFn) {
|
|
277
|
+
const uts = now.getTime();
|
|
278
|
+
conditions.push((0, _sequelize().where)((0, _sequelize().fn)('MOD', (0, _sequelize().literal)(`${Math.round(uts / 1000)} - ${tsFn(startsOn.field)}`), Math.round(repeat / 1000)), {
|
|
279
|
+
[_sequelize().Op.lt]: Math.round(_this2.cacheCycle / 1000)
|
|
280
|
+
})); // conditions.push(literal(`mod(${uts} - ${tsFn(startsOn.field)} * 1000, ${repeat}) < ${this.cacheCycle}`));
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const _db$getCollection = db.getCollection(collection),
|
|
284
|
+
model = _db$getCollection.model;
|
|
285
|
+
|
|
286
|
+
const count = yield model.count({
|
|
287
|
+
where: {
|
|
288
|
+
[_sequelize().Op.and]: conditions
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
return Boolean(count);
|
|
292
|
+
})();
|
|
293
|
+
},
|
|
294
|
+
|
|
295
|
+
trigger(workflow, date) {
|
|
296
|
+
var _this3 = this;
|
|
297
|
+
|
|
298
|
+
return _asyncToGenerator(function* () {
|
|
299
|
+
var _startsOn$offset, _startsOn$unit;
|
|
300
|
+
|
|
301
|
+
const _workflow$config5 = workflow.config,
|
|
302
|
+
collection = _workflow$config5.collection,
|
|
303
|
+
startsOn = _workflow$config5.startsOn,
|
|
304
|
+
endsOn = _workflow$config5.endsOn,
|
|
305
|
+
repeat = _workflow$config5.repeat;
|
|
306
|
+
|
|
307
|
+
if (typeof startsOn !== 'object') {
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
const timestamp = date.getTime();
|
|
312
|
+
const startTimestamp = timestamp - ((_startsOn$offset = startsOn.offset) !== null && _startsOn$offset !== void 0 ? _startsOn$offset : 0) * ((_startsOn$unit = startsOn.unit) !== null && _startsOn$unit !== void 0 ? _startsOn$unit : 1000);
|
|
313
|
+
const conditions = [];
|
|
314
|
+
|
|
315
|
+
if (!repeat) {
|
|
316
|
+
// startsOn exactly equal to now in 1s
|
|
317
|
+
conditions.push({
|
|
318
|
+
[startsOn.field]: {
|
|
319
|
+
[_sequelize().Op.gte]: new Date(startTimestamp),
|
|
320
|
+
[_sequelize().Op.lt]: new Date(startTimestamp + 1000)
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
} else {
|
|
324
|
+
// startsOn not after now
|
|
325
|
+
conditions.push({
|
|
326
|
+
[startsOn.field]: {
|
|
327
|
+
[_sequelize().Op.lt]: new Date(startTimestamp)
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
const tsFn = DialectTimestampFnMap[_this3.plugin.app.db.options.dialect];
|
|
331
|
+
|
|
332
|
+
if (typeof repeat === 'number' && tsFn) {
|
|
333
|
+
conditions.push((0, _sequelize().where)((0, _sequelize().fn)('MOD', (0, _sequelize().literal)(`${Math.round(timestamp / 1000)} - ${tsFn(startsOn.field)}`), Math.round(repeat / 1000)), {
|
|
334
|
+
[_sequelize().Op.eq]: 0
|
|
335
|
+
})); // conditions.push(literal(`MOD(CAST(${timestamp} AS BIGINT) - CAST((FLOOR(${tsFn(startsOn.field)}) AS BIGINT) * 1000), ${repeat}) = 0`));
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
switch (typeof endsOn) {
|
|
339
|
+
case 'string':
|
|
340
|
+
const endTime = Date.parse(endsOn);
|
|
341
|
+
|
|
342
|
+
if (!endTime || endTime <= timestamp) {
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
break;
|
|
347
|
+
|
|
348
|
+
case 'object':
|
|
349
|
+
if (endsOn.field) {
|
|
350
|
+
var _endsOn$offset, _endsOn$unit;
|
|
351
|
+
|
|
352
|
+
conditions.push({
|
|
353
|
+
[endsOn.field]: {
|
|
354
|
+
[_sequelize().Op.gte]: new Date(timestamp - ((_endsOn$offset = endsOn.offset) !== null && _endsOn$offset !== void 0 ? _endsOn$offset : 0) * ((_endsOn$unit = endsOn.unit) !== null && _endsOn$unit !== void 0 ? _endsOn$unit : 1000) + 1000)
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
break;
|
|
360
|
+
|
|
361
|
+
default:
|
|
362
|
+
break;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const _this3$plugin$app$db$ = _this3.plugin.app.db.getCollection(collection),
|
|
367
|
+
model = _this3$plugin$app$db$.model;
|
|
368
|
+
|
|
369
|
+
const instances = yield model.findAll({
|
|
370
|
+
where: {
|
|
371
|
+
[_sequelize().Op.and]: conditions
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
if (instances.length) {
|
|
376
|
+
console.log(instances.length, 'rows trigger at', date);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
instances.forEach(item => {
|
|
380
|
+
_this3.plugin.trigger(workflow, {
|
|
381
|
+
date,
|
|
382
|
+
data: item.get()
|
|
383
|
+
});
|
|
384
|
+
});
|
|
385
|
+
})();
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
function nextInCycle(workflow, now) {
|
|
391
|
+
const repeat = workflow.config.repeat; // no repeat means no need to rerun
|
|
392
|
+
// but if in current cycle, should be put in cache
|
|
393
|
+
// no repeat but in current cycle means startsOn has been configured
|
|
394
|
+
// so we need to more info to determine if necessary config items
|
|
395
|
+
|
|
396
|
+
if (!repeat) {
|
|
397
|
+
return true;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
switch (typeof repeat) {
|
|
401
|
+
case 'string':
|
|
402
|
+
break;
|
|
403
|
+
|
|
404
|
+
default:
|
|
405
|
+
return true;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
const currentDate = new Date(now);
|
|
409
|
+
currentDate.setMilliseconds(-1);
|
|
410
|
+
const timestamp = now.getTime();
|
|
411
|
+
|
|
412
|
+
const interval = _cronParser().default.parseExpression(repeat, {
|
|
413
|
+
currentDate
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
let next = interval.next(); // NOTE: cache all workflows will be matched in current cycle
|
|
417
|
+
|
|
418
|
+
if (next.getTime() - timestamp <= this.cacheCycle) {
|
|
419
|
+
return true;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
return false;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
class ScheduleTrigger extends _.Trigger {
|
|
426
|
+
// running interval, default to 1s
|
|
427
|
+
// caching workflows in range, default to 1min
|
|
428
|
+
constructor(plugin) {
|
|
429
|
+
super(plugin);
|
|
430
|
+
this.events = new Map();
|
|
431
|
+
this.timer = null;
|
|
432
|
+
this.cache = new Map();
|
|
433
|
+
this.interval = 1000;
|
|
434
|
+
this.cacheCycle = 60000;
|
|
435
|
+
|
|
436
|
+
this.run = () => {
|
|
437
|
+
const now = new Date();
|
|
438
|
+
now.setMilliseconds(0); // NOTE: trigger `onTick` for high interval jobs which are cached in last 1 min
|
|
439
|
+
|
|
440
|
+
this.onTick(now); // NOTE: reload when second match cache cycle
|
|
441
|
+
|
|
442
|
+
if (!(now.getTime() % this.cacheCycle)) {
|
|
443
|
+
this.reload();
|
|
444
|
+
}
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
plugin.app.on('beforeStop', () => {
|
|
448
|
+
if (this.timer) {
|
|
449
|
+
clearInterval(this.timer);
|
|
450
|
+
}
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
init() {
|
|
455
|
+
if (!this.timer) {
|
|
456
|
+
const now = new Date(); // NOTE: assign to this.timer to avoid duplicated initialization
|
|
457
|
+
|
|
458
|
+
this.timer = setTimeout(() => {
|
|
459
|
+
this.timer = setInterval(this.run, this.interval); // initially trigger
|
|
460
|
+
// this.onTick(now);
|
|
461
|
+
}, // NOTE:
|
|
462
|
+
// try to align to system time on each second starts,
|
|
463
|
+
// after at least 1 second initialized for anything to get ready.
|
|
464
|
+
// so jobs in 2 seconds will be missed at first start.
|
|
465
|
+
1000 - now.getMilliseconds());
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
onTick(now) {
|
|
470
|
+
var _this4 = this;
|
|
471
|
+
|
|
472
|
+
return _asyncToGenerator(function* () {
|
|
473
|
+
// NOTE: trigger workflows in sequence when sqlite due to only one transaction
|
|
474
|
+
const isSqlite = _this4.plugin.app.db.options.dialect === 'sqlite';
|
|
475
|
+
return Array.from(_this4.cache.values()).reduce((prev, workflow) => {
|
|
476
|
+
if (!_this4.shouldTrigger(workflow, now)) {
|
|
477
|
+
return prev;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
if (isSqlite) {
|
|
481
|
+
return prev.then(() => _this4.trigger(workflow, now));
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
_this4.trigger(workflow, now);
|
|
485
|
+
|
|
486
|
+
return null;
|
|
487
|
+
}, isSqlite ? Promise.resolve() : null);
|
|
488
|
+
})();
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
reload() {
|
|
492
|
+
var _this5 = this;
|
|
493
|
+
|
|
494
|
+
return _asyncToGenerator(function* () {
|
|
495
|
+
const WorkflowModel = _this5.plugin.app.db.getCollection('workflows').model;
|
|
496
|
+
|
|
497
|
+
const workflows = yield WorkflowModel.findAll({
|
|
498
|
+
where: {
|
|
499
|
+
enabled: true,
|
|
500
|
+
type: 'schedule'
|
|
501
|
+
},
|
|
502
|
+
include: [{
|
|
503
|
+
association: 'executions',
|
|
504
|
+
attributes: ['id', 'createdAt'],
|
|
505
|
+
separate: true,
|
|
506
|
+
limit: 1,
|
|
507
|
+
order: [['createdAt', 'DESC']]
|
|
508
|
+
}],
|
|
509
|
+
group: ['id']
|
|
510
|
+
}); // NOTE: clear cached jobs in last cycle
|
|
511
|
+
|
|
512
|
+
_this5.cache = new Map();
|
|
513
|
+
|
|
514
|
+
_this5.inspect(workflows);
|
|
515
|
+
})();
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
inspect(workflows) {
|
|
519
|
+
var _this6 = this;
|
|
520
|
+
|
|
521
|
+
const now = new Date();
|
|
522
|
+
now.setMilliseconds(0);
|
|
523
|
+
workflows.forEach( /*#__PURE__*/function () {
|
|
524
|
+
var _ref2 = _asyncToGenerator(function* (workflow) {
|
|
525
|
+
const should = yield _this6.shouldCache(workflow, now);
|
|
526
|
+
|
|
527
|
+
_this6.setCache(workflow, !should);
|
|
528
|
+
});
|
|
529
|
+
|
|
530
|
+
return function (_x3) {
|
|
531
|
+
return _ref2.apply(this, arguments);
|
|
532
|
+
};
|
|
533
|
+
}());
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
setCache(workflow, out = false) {
|
|
537
|
+
out ? this.cache.delete(workflow.id) : this.cache.set(workflow.id, workflow);
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
shouldCache(workflow, now) {
|
|
541
|
+
var _this7 = this;
|
|
542
|
+
|
|
543
|
+
return _asyncToGenerator(function* () {
|
|
544
|
+
var _iteratorAbruptCompletion = false;
|
|
545
|
+
var _didIteratorError = false;
|
|
546
|
+
|
|
547
|
+
var _iteratorError;
|
|
548
|
+
|
|
549
|
+
try {
|
|
550
|
+
for (var _iterator = _asyncIterator(_this7.constructor.CacheRules), _step; _iteratorAbruptCompletion = !(_step = yield _iterator.next()).done; _iteratorAbruptCompletion = false) {
|
|
551
|
+
const rule = _step.value;
|
|
552
|
+
|
|
553
|
+
if (!(yield rule.call(_this7, workflow, now))) {
|
|
554
|
+
return false;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
} catch (err) {
|
|
558
|
+
_didIteratorError = true;
|
|
559
|
+
_iteratorError = err;
|
|
560
|
+
} finally {
|
|
561
|
+
try {
|
|
562
|
+
if (_iteratorAbruptCompletion && _iterator.return != null) {
|
|
563
|
+
yield _iterator.return();
|
|
564
|
+
}
|
|
565
|
+
} finally {
|
|
566
|
+
if (_didIteratorError) {
|
|
567
|
+
throw _iteratorError;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
return true;
|
|
573
|
+
})();
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
shouldTrigger(workflow, now) {
|
|
577
|
+
var _iterator2 = _createForOfIteratorHelper(this.constructor.TriggerRules),
|
|
578
|
+
_step2;
|
|
579
|
+
|
|
580
|
+
try {
|
|
581
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
582
|
+
const rule = _step2.value;
|
|
583
|
+
|
|
584
|
+
if (!rule.call(this, workflow, now)) {
|
|
585
|
+
return false;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
} catch (err) {
|
|
589
|
+
_iterator2.e(err);
|
|
590
|
+
} finally {
|
|
591
|
+
_iterator2.f();
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
return true;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
trigger(workflow, date) {
|
|
598
|
+
var _this8 = this;
|
|
599
|
+
|
|
600
|
+
return _asyncToGenerator(function* () {
|
|
601
|
+
const mode = workflow.config.mode;
|
|
602
|
+
const modeHandlers = ScheduleModes.get(mode);
|
|
603
|
+
return modeHandlers.trigger.call(_this8, workflow, date);
|
|
604
|
+
})();
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
on(workflow) {
|
|
608
|
+
// NOTE: lazy initialization
|
|
609
|
+
this.init();
|
|
610
|
+
const mode = workflow.config.mode;
|
|
611
|
+
const modeHandlers = ScheduleModes.get(mode);
|
|
612
|
+
|
|
613
|
+
if (modeHandlers && modeHandlers.on) {
|
|
614
|
+
modeHandlers.on.call(this, workflow);
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
this.inspect([workflow]);
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
off(workflow) {
|
|
621
|
+
const mode = workflow.config.mode;
|
|
622
|
+
const modeHandlers = ScheduleModes.get(mode);
|
|
623
|
+
|
|
624
|
+
if (modeHandlers && modeHandlers.off) {
|
|
625
|
+
modeHandlers.off.call(this, workflow);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
this.cache.delete(workflow.id);
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
exports.default = ScheduleTrigger;
|
|
634
|
+
ScheduleTrigger.CacheRules = [// ({ enabled }) => enabled,
|
|
635
|
+
({
|
|
636
|
+
config,
|
|
637
|
+
allExecuted
|
|
638
|
+
}) => config.limit ? allExecuted < config.limit : true, ({
|
|
639
|
+
config
|
|
640
|
+
}) => ['repeat', 'startsOn'].some(key => config[key]), nextInCycle, function (workflow, now) {
|
|
641
|
+
const mode = workflow.config.mode;
|
|
642
|
+
const modeHandlers = ScheduleModes.get(mode);
|
|
643
|
+
return modeHandlers.shouldCache.call(this, workflow, now);
|
|
644
|
+
}];
|
|
645
|
+
ScheduleTrigger.TriggerRules = [({
|
|
646
|
+
config,
|
|
647
|
+
allExecuted
|
|
648
|
+
}) => config.limit ? allExecuted < config.limit : true, ({
|
|
649
|
+
config
|
|
650
|
+
}) => ['repeat', 'startsOn'].some(key => config[key]), function (workflow, now) {
|
|
651
|
+
const repeat = workflow.config.repeat;
|
|
652
|
+
|
|
653
|
+
if (typeof repeat !== 'string') {
|
|
654
|
+
return true;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
const currentDate = new Date(now);
|
|
658
|
+
currentDate.setMilliseconds(-1);
|
|
659
|
+
const timestamp = now.getTime();
|
|
660
|
+
|
|
661
|
+
const interval = _cronParser().default.parseExpression(repeat, {
|
|
662
|
+
currentDate
|
|
663
|
+
});
|
|
664
|
+
|
|
665
|
+
let next = interval.next();
|
|
666
|
+
|
|
667
|
+
if (next.getTime() === timestamp) {
|
|
668
|
+
return true;
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
return false;
|
|
672
|
+
}];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/plugin-workflow",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.1-alpha.6",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"licenses": [
|
|
@@ -9,20 +9,16 @@
|
|
|
9
9
|
"url": "http://www.apache.org/licenses/LICENSE-2.0"
|
|
10
10
|
}
|
|
11
11
|
],
|
|
12
|
-
"scripts": {
|
|
13
|
-
"build": "rimraf -rf lib esm dist && npm run build:cjs && npm run build:esm",
|
|
14
|
-
"build:cjs": "tsc --project tsconfig.build.json",
|
|
15
|
-
"build:esm": "tsc --project tsconfig.build.json --module es2015 --outDir esm"
|
|
16
|
-
},
|
|
17
12
|
"dependencies": {
|
|
18
|
-
"@nocobase/actions": "0.7.
|
|
19
|
-
"@nocobase/database": "0.7.
|
|
20
|
-
"@nocobase/server": "0.7.
|
|
21
|
-
"@nocobase/utils": "0.7.
|
|
13
|
+
"@nocobase/actions": "0.7.1-alpha.6",
|
|
14
|
+
"@nocobase/database": "0.7.1-alpha.6",
|
|
15
|
+
"@nocobase/server": "0.7.1-alpha.6",
|
|
16
|
+
"@nocobase/utils": "0.7.1-alpha.6",
|
|
17
|
+
"cron-parser": "4.4.0",
|
|
22
18
|
"json-templates": "^4.2.0"
|
|
23
19
|
},
|
|
24
20
|
"devDependencies": {
|
|
25
|
-
"@nocobase/test": "0.7.
|
|
21
|
+
"@nocobase/test": "0.7.1-alpha.6"
|
|
26
22
|
},
|
|
27
|
-
"gitHead": "
|
|
23
|
+
"gitHead": "b6f96c97020d278ae597bf05553442d7516b2216"
|
|
28
24
|
}
|