@nocobase/plugin-workflow 0.7.0-alpha.82 → 0.7.0-alpha.83

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/server.d.ts CHANGED
@@ -6,5 +6,5 @@ export default class extends Plugin {
6
6
  triggers: Registry<Trigger>;
7
7
  getName(): string;
8
8
  load(options?: {}): Promise<void>;
9
- toggle(workflow: WorkflowModel, enable?: boolean): Promise<void>;
9
+ toggle(workflow: WorkflowModel, enable?: boolean): void;
10
10
  }
package/lib/server.js CHANGED
@@ -45,6 +45,12 @@ function _utils() {
45
45
 
46
46
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
47
47
 
48
+ 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; }
49
+
50
+ 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; }
51
+
52
+ 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; }
53
+
48
54
  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); } }
49
55
 
50
56
  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); }); }; }
@@ -87,8 +93,7 @@ class _default extends _server().Plugin {
87
93
  workflows.forEach(workflow => {
88
94
  _this.toggle(workflow);
89
95
  });
90
- db.on('workflows.afterCreate', model => _this.toggle(model));
91
- db.on('workflows.afterUpdate', model => _this.toggle(model));
96
+ db.on('workflows.afterSave', model => _this.toggle(model));
92
97
  db.on('workflows.afterDestroy', model => _this.toggle(model, false));
93
98
  })); // [Life Cycle]: initialize all necessary seed data
94
99
  // this.app.on('db.init', async () => {});
@@ -97,19 +102,21 @@ class _default extends _server().Plugin {
97
102
  }
98
103
 
99
104
  toggle(workflow, enable) {
100
- var _this2 = this;
105
+ const type = workflow.get('type');
106
+ const trigger = this.triggers.get(type);
101
107
 
102
- return _asyncToGenerator(function* () {
103
- const type = workflow.get('type');
108
+ if (typeof enable !== 'undefined' ? enable : workflow.get('enabled')) {
109
+ // NOTE: remove previous listener if config updated
110
+ const prev = workflow.previous();
104
111
 
105
- const trigger = _this2.triggers.get(type);
106
-
107
- if (typeof enable !== 'undefined' ? enable : workflow.get('enabled')) {
108
- yield trigger.on(workflow);
109
- } else {
110
- yield trigger.off(workflow);
112
+ if (prev.config) {
113
+ trigger.off(_objectSpread(_objectSpread({}, workflow.get()), prev));
111
114
  }
112
- })();
115
+
116
+ trigger.on(workflow);
117
+ } else {
118
+ trigger.off(workflow);
119
+ }
113
120
  }
114
121
 
115
122
  }
@@ -19,12 +19,6 @@ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o =
19
19
 
20
20
  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; }
21
21
 
22
- 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; }
23
-
24
- 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; }
25
-
26
- 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; }
27
-
28
22
  const MODE_BITMAP = {
29
23
  CREATE: 1,
30
24
  UPDATE: 2,
@@ -69,13 +63,6 @@ class CollectionTrigger {
69
63
  }
70
64
 
71
65
  on(workflow) {
72
- // NOTE: remove previous listener if config updated
73
- const prev = workflow.previous();
74
-
75
- if (prev.config) {
76
- this.off(_objectSpread(_objectSpread({}, workflow.get()), prev));
77
- }
78
-
79
66
  const _workflow$config = workflow.config,
80
67
  collection = _workflow$config.collection,
81
68
  mode = _workflow$config.mode;
@@ -7,9 +7,12 @@ exports.default = _default;
7
7
 
8
8
  var _collection = _interopRequireDefault(require("./collection"));
9
9
 
10
+ var _schedule = _interopRequireDefault(require("./schedule"));
11
+
10
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
13
 
12
14
  function _default(plugin) {
13
15
  const triggers = plugin.triggers;
14
- triggers.register('collection', new _collection.default(plugin)); // triggers.register('schedule', new Schedule(plugin));
16
+ triggers.register('collection', new _collection.default(plugin));
17
+ triggers.register('schedule', new _schedule.default(plugin));
15
18
  }
@@ -0,0 +1,41 @@
1
+ import { Trigger } from '.';
2
+ export declare type ScheduleOnField = string | {
3
+ field: string;
4
+ offset?: number;
5
+ unit?: 1000 | 60000 | 3600000 | 86400000;
6
+ };
7
+ export interface ScheduleTriggerConfig {
8
+ mode: number;
9
+ cron?: string;
10
+ limit?: number;
11
+ startsOn?: ScheduleOnField;
12
+ endsOn?: ScheduleOnField;
13
+ }
14
+ export declare const SCHEDULE_MODE: {
15
+ readonly CONSTANT: 0;
16
+ readonly COLLECTION_FIELD: 1;
17
+ };
18
+ export default class ScheduleTrigger implements Trigger {
19
+ static CacheRules: ((workflow: any, now: any) => any)[];
20
+ static TriggerRules: ((workflow: any, now: any) => boolean)[];
21
+ readonly db: any;
22
+ events: Map<any, any>;
23
+ private timer;
24
+ private cache;
25
+ interval: number;
26
+ cacheCycle: number;
27
+ constructor({ app }: {
28
+ app: any;
29
+ });
30
+ init(): void;
31
+ run: () => void;
32
+ onTick(now: any): Promise<any>;
33
+ reload(): Promise<void>;
34
+ inspect(workflows: any): void;
35
+ setCache(workflow: any, out?: boolean): void;
36
+ shouldCache(workflow: any, now: any): Promise<boolean>;
37
+ shouldTrigger(workflow: any, now: any): boolean;
38
+ trigger(workflow: any, date: Date): Promise<any>;
39
+ on(workflow: any): void;
40
+ off(workflow: any): void;
41
+ }
@@ -0,0 +1,602 @@
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 _lodash() {
19
+ const data = require("lodash");
20
+
21
+ _lodash = function _lodash() {
22
+ return data;
23
+ };
24
+
25
+ return data;
26
+ }
27
+
28
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
+
30
+ 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; } } }; }
31
+
32
+ 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); }
33
+
34
+ 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; }
35
+
36
+ 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); } }
37
+
38
+ 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); }); }; }
39
+
40
+ 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"); }
41
+
42
+ 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); }
43
+
44
+ const SCHEDULE_MODE = {
45
+ CONSTANT: 0,
46
+ COLLECTION_FIELD: 1
47
+ };
48
+ exports.SCHEDULE_MODE = SCHEDULE_MODE;
49
+ const ScheduleModes = new Map();
50
+ ScheduleModes.set(SCHEDULE_MODE.CONSTANT, {
51
+ shouldCache(workflow, now) {
52
+ const _workflow$config = workflow.config,
53
+ startsOn = _workflow$config.startsOn,
54
+ endsOn = _workflow$config.endsOn;
55
+ const timestamp = now.getTime();
56
+
57
+ if (startsOn) {
58
+ const startTime = Date.parse(startsOn);
59
+
60
+ if (!startTime || startTime > timestamp + this.cacheCycle) {
61
+ return false;
62
+ }
63
+ }
64
+
65
+ if (endsOn) {
66
+ const endTime = Date.parse(endsOn);
67
+
68
+ if (!endTime || endTime <= timestamp) {
69
+ return false;
70
+ }
71
+ }
72
+
73
+ return true;
74
+ },
75
+
76
+ trigger(workflow, date) {
77
+ return workflow.trigger({
78
+ date
79
+ });
80
+ }
81
+
82
+ });
83
+
84
+ function getDateRangeFilter(on, now, dir) {
85
+ const timestamp = now.getTime();
86
+ const op = dir < 0 ? '$lt' : '$gte';
87
+
88
+ switch (typeof on) {
89
+ case 'string':
90
+ const time = Date.parse(on);
91
+
92
+ if (!time || (dir < 0 ? timestamp < time : time <= timestamp)) {
93
+ return null;
94
+ }
95
+
96
+ break;
97
+
98
+ case 'object':
99
+ const field = on.field,
100
+ _on$offset = on.offset,
101
+ offset = _on$offset === void 0 ? 0 : _on$offset,
102
+ _on$unit = on.unit,
103
+ unit = _on$unit === void 0 ? 1000 : _on$unit;
104
+ return {
105
+ [field]: {
106
+ [op]: new Date(timestamp + offset * unit * dir)
107
+ }
108
+ };
109
+
110
+ default:
111
+ break;
112
+ }
113
+
114
+ return {};
115
+ }
116
+
117
+ function getDataOptionTime(data, on, now, dir = 1) {
118
+ switch (typeof on) {
119
+ case 'string':
120
+ const time = Date.parse(on);
121
+ return time ? time : null;
122
+
123
+ case 'object':
124
+ const field = on.field,
125
+ _on$offset2 = on.offset,
126
+ offset = _on$offset2 === void 0 ? 0 : _on$offset2,
127
+ _on$unit2 = on.unit,
128
+ unit = _on$unit2 === void 0 ? 1000 : _on$unit2;
129
+ return data[field] ? data[field].getTime() - offset * unit * dir : null;
130
+
131
+ default:
132
+ return null;
133
+ }
134
+ }
135
+
136
+ function getHookId(workflow, type) {
137
+ return `${type}#${workflow.id}`;
138
+ }
139
+
140
+ ScheduleModes.set(SCHEDULE_MODE.COLLECTION_FIELD, {
141
+ on(workflow) {
142
+ const _workflow$config2 = workflow.config,
143
+ collection = _workflow$config2.collection,
144
+ startsOn = _workflow$config2.startsOn,
145
+ endsOn = _workflow$config2.endsOn,
146
+ cron = _workflow$config2.cron;
147
+ const event = `${collection}.afterSave`;
148
+ const name = getHookId(workflow, event);
149
+
150
+ if (!this.events.has(name)) {
151
+ // NOTE: toggle cache depends on new date
152
+ const listener = (data, options) => {
153
+ // check if saved collection data in cache cycle
154
+ // in: add workflow to cache
155
+ // out: 1. do nothing because if any other data in
156
+ // 2. another way is always check all data to match cycle
157
+ // by calling: inspect(workflow)
158
+ // this may lead to performance issues
159
+ // so we can only check single row and only set in if true
160
+ // how to check?
161
+ // * startsOn only : startsOn in cycle
162
+ // * endsOn only : invalid
163
+ // * cron only : invalid
164
+ // * startsOn and endsOn: equal to only startsOn
165
+ // * startsOn and cron : startsOn in cycle and cron in cycle
166
+ // * endsOn and cron : invalid
167
+ // * all : all rules effect
168
+ // * none : invalid
169
+ // this means, startsOn and cron should be present at least one
170
+ // and no startsOn equals run on cron, and could ends on endsOn,
171
+ // this will be a little wired, only means the end date should use collection field.
172
+ const now = new Date();
173
+ now.setMilliseconds(0);
174
+ const timestamp = now.getTime();
175
+ const startTime = getDataOptionTime(data, startsOn, now);
176
+ const endTime = getDataOptionTime(data, endsOn, now, -1);
177
+
178
+ if (!startTime && !cron) {
179
+ return;
180
+ }
181
+
182
+ if (startTime && startTime > timestamp + this.cacheCycle) {
183
+ return;
184
+ }
185
+
186
+ if (endTime && endTime <= timestamp) {
187
+ return;
188
+ }
189
+
190
+ if (!cronInCycle.call(this, workflow, now)) {
191
+ return;
192
+ }
193
+
194
+ console.log('set cache', now);
195
+ this.setCache(workflow);
196
+ };
197
+
198
+ this.events.set(name, listener);
199
+ this.db.on(`${collection}.afterSave`, listener);
200
+ }
201
+ },
202
+
203
+ off(workflow) {
204
+ const collection = workflow.config.collection;
205
+ const event = `${collection}.afterSave`;
206
+ const name = getHookId(workflow, event);
207
+
208
+ if (this.events.has(name)) {
209
+ const listener = this.events.get(name);
210
+ this.events.delete(name);
211
+ this.db.off(`${collection}.afterSave`, listener);
212
+ }
213
+ },
214
+
215
+ shouldCache(workflow, now) {
216
+ var _this = this;
217
+
218
+ return _asyncToGenerator(function* () {
219
+ const _workflow$config3 = workflow.config,
220
+ startsOn = _workflow$config3.startsOn,
221
+ endsOn = _workflow$config3.endsOn,
222
+ collection = _workflow$config3.collection;
223
+ const starts = getDateRangeFilter(startsOn, now, -1);
224
+
225
+ if (!starts) {
226
+ return false;
227
+ }
228
+
229
+ const ends = getDateRangeFilter(endsOn, now, 1);
230
+
231
+ if (!ends) {
232
+ return false;
233
+ }
234
+
235
+ const filter = (0, _lodash().merge)(starts, ends); // if neither startsOn nor endsOn is provided
236
+
237
+ if (!Object.keys(filter).length) {
238
+ // consider as invalid
239
+ return false;
240
+ }
241
+
242
+ const repo = _this.db.getCollection(collection).repository;
243
+
244
+ const count = yield repo.count({
245
+ filter
246
+ });
247
+ return Boolean(count);
248
+ })();
249
+ },
250
+
251
+ trigger(workflow, date) {
252
+ var _this2 = this;
253
+
254
+ return _asyncToGenerator(function* () {
255
+ var _startsOn$offset, _startsOn$unit, _endsOn$offset, _endsOn$unit;
256
+
257
+ const _workflow$config4 = workflow.config,
258
+ collection = _workflow$config4.collection,
259
+ startsOn = _workflow$config4.startsOn,
260
+ endsOn = _workflow$config4.endsOn,
261
+ cron = _workflow$config4.cron;
262
+
263
+ if (typeof startsOn !== 'object') {
264
+ return;
265
+ }
266
+
267
+ const timestamp = date.getTime();
268
+ 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);
269
+ let filter;
270
+
271
+ if (!cron) {
272
+ // startsOn exactly equal to now in 1s
273
+ filter = {
274
+ [startsOn.field]: {
275
+ $gte: new Date(startTimestamp),
276
+ $lt: new Date(startTimestamp + 1000)
277
+ }
278
+ };
279
+ } else {
280
+ // startsOn not after now
281
+ filter = {
282
+ [startsOn.field]: {
283
+ $lt: new Date(startTimestamp)
284
+ }
285
+ };
286
+
287
+ switch (typeof endsOn) {
288
+ case 'string':
289
+ const endTime = Date.parse(endsOn);
290
+
291
+ if (!endTime || endTime <= timestamp) {
292
+ return;
293
+ }
294
+
295
+ break;
296
+
297
+ case 'object':
298
+ filter[endsOn.field] = {
299
+ $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)
300
+ };
301
+ break;
302
+
303
+ default:
304
+ break;
305
+ }
306
+ }
307
+
308
+ const repo = _this2.db.getCollection(collection).repository;
309
+
310
+ const instances = yield repo.find({
311
+ filter
312
+ });
313
+ console.log('trigger at', date);
314
+ instances.forEach(item => {
315
+ workflow.trigger({
316
+ date,
317
+ data: item.get()
318
+ });
319
+ });
320
+ })();
321
+ }
322
+
323
+ });
324
+
325
+ function cronInCycle(workflow, now) {
326
+ const cron = workflow.config.cron; // no cron means no need to rerun
327
+ // but if in current cycle, should be put in cache
328
+ // no cron but in current cycle means startsOn or endsOn has been configured
329
+ // so we need to more info to determine if necessary config items
330
+
331
+ if (!cron) {
332
+ return true;
333
+ }
334
+
335
+ const currentDate = new Date(now);
336
+ currentDate.setMilliseconds(-1);
337
+ const timestamp = now.getTime();
338
+
339
+ const interval = _cronParser().default.parseExpression(cron, {
340
+ currentDate
341
+ });
342
+
343
+ let next = interval.next(); // NOTE: cache all workflows will be matched in current cycle
344
+
345
+ if (next.getTime() - timestamp <= this.cacheCycle) {
346
+ return true;
347
+ }
348
+
349
+ return false;
350
+ }
351
+
352
+ class ScheduleTrigger {
353
+ // running interval, default to 1s
354
+ // caching workflows in range, default to 1min
355
+ constructor({
356
+ app
357
+ }) {
358
+ this.db = void 0;
359
+ this.events = new Map();
360
+ this.timer = null;
361
+ this.cache = new Map();
362
+ this.interval = 1000;
363
+ this.cacheCycle = 60000;
364
+
365
+ this.run = () => {
366
+ const now = new Date();
367
+ now.setMilliseconds(0); // NOTE: trigger `onTick` for high interval jobs which are cached in last 1 min
368
+
369
+ this.onTick(now); // NOTE: reload when second match cache cycle
370
+
371
+ if (!(now.getTime() % this.cacheCycle)) {
372
+ this.reload();
373
+ }
374
+ };
375
+
376
+ this.db = app.db;
377
+ app.on('beforeStop', () => {
378
+ if (this.timer) {
379
+ clearInterval(this.timer);
380
+ }
381
+ });
382
+ }
383
+
384
+ init() {
385
+ if (!this.timer) {
386
+ const now = new Date(); // NOTE: assign to this.timer to avoid duplicated initialization
387
+
388
+ this.timer = setTimeout(() => {
389
+ this.timer = setInterval(this.run, this.interval); // initially trigger
390
+ // this.onTick(now);
391
+ }, // NOTE:
392
+ // try to align to system time on each second starts,
393
+ // after at least 1 second initialized for anything to get ready.
394
+ // so jobs in 2 seconds will be missed at first start.
395
+ 1000 - now.getMilliseconds());
396
+ }
397
+ }
398
+
399
+ onTick(now) {
400
+ var _this3 = this;
401
+
402
+ return _asyncToGenerator(function* () {
403
+ // NOTE: trigger workflows in sequence when sqlite due to only one transaction
404
+ const isSqlite = _this3.db.options.dialect === 'sqlite';
405
+ return Array.from(_this3.cache.values()).reduce((prev, workflow) => {
406
+ if (!_this3.shouldTrigger(workflow, now)) {
407
+ return prev;
408
+ }
409
+
410
+ if (isSqlite) {
411
+ return prev.then(() => _this3.trigger(workflow, now));
412
+ }
413
+
414
+ _this3.trigger(workflow, now);
415
+
416
+ return null;
417
+ }, isSqlite ? Promise.resolve() : null);
418
+ })();
419
+ }
420
+
421
+ reload() {
422
+ var _this4 = this;
423
+
424
+ return _asyncToGenerator(function* () {
425
+ const WorkflowModel = _this4.db.getCollection('workflows').model;
426
+
427
+ const workflows = yield WorkflowModel.findAll({
428
+ where: {
429
+ enabled: true,
430
+ type: 'schedule'
431
+ },
432
+ include: [{
433
+ association: 'executions',
434
+ attributes: ['id', 'createdAt'],
435
+ seperate: true,
436
+ limit: 1,
437
+ order: [['createdAt', 'DESC']]
438
+ }],
439
+ group: ['id']
440
+ }); // NOTE: clear cached jobs in last cycle
441
+
442
+ _this4.cache = new Map();
443
+
444
+ _this4.inspect(workflows);
445
+ })();
446
+ }
447
+
448
+ inspect(workflows) {
449
+ var _this5 = this;
450
+
451
+ const now = new Date();
452
+ now.setMilliseconds(0);
453
+ workflows.forEach( /*#__PURE__*/function () {
454
+ var _ref = _asyncToGenerator(function* (workflow) {
455
+ const should = yield _this5.shouldCache(workflow, now);
456
+
457
+ _this5.setCache(workflow, !should);
458
+ });
459
+
460
+ return function (_x) {
461
+ return _ref.apply(this, arguments);
462
+ };
463
+ }());
464
+ }
465
+
466
+ setCache(workflow, out = false) {
467
+ out ? this.cache.delete(workflow.id) : this.cache.set(workflow.id, workflow);
468
+ }
469
+
470
+ shouldCache(workflow, now) {
471
+ var _this6 = this;
472
+
473
+ return _asyncToGenerator(function* () {
474
+ var _iteratorAbruptCompletion = false;
475
+ var _didIteratorError = false;
476
+
477
+ var _iteratorError;
478
+
479
+ try {
480
+ for (var _iterator = _asyncIterator(_this6.constructor.CacheRules), _step; _iteratorAbruptCompletion = !(_step = yield _iterator.next()).done; _iteratorAbruptCompletion = false) {
481
+ const rule = _step.value;
482
+
483
+ if (!(yield rule.call(_this6, workflow, now))) {
484
+ return false;
485
+ }
486
+ }
487
+ } catch (err) {
488
+ _didIteratorError = true;
489
+ _iteratorError = err;
490
+ } finally {
491
+ try {
492
+ if (_iteratorAbruptCompletion && _iterator.return != null) {
493
+ yield _iterator.return();
494
+ }
495
+ } finally {
496
+ if (_didIteratorError) {
497
+ throw _iteratorError;
498
+ }
499
+ }
500
+ }
501
+
502
+ return true;
503
+ })();
504
+ }
505
+
506
+ shouldTrigger(workflow, now) {
507
+ var _iterator2 = _createForOfIteratorHelper(this.constructor.TriggerRules),
508
+ _step2;
509
+
510
+ try {
511
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
512
+ const rule = _step2.value;
513
+
514
+ if (!rule.call(this, workflow, now)) {
515
+ return false;
516
+ }
517
+ }
518
+ } catch (err) {
519
+ _iterator2.e(err);
520
+ } finally {
521
+ _iterator2.f();
522
+ }
523
+
524
+ return true;
525
+ }
526
+
527
+ trigger(workflow, date) {
528
+ var _this7 = this;
529
+
530
+ return _asyncToGenerator(function* () {
531
+ const mode = workflow.config.mode;
532
+ const modeHandlers = ScheduleModes.get(mode);
533
+ return modeHandlers.trigger.call(_this7, workflow, date);
534
+ })();
535
+ }
536
+
537
+ on(workflow) {
538
+ // NOTE: lazy initialization
539
+ this.init();
540
+ const mode = workflow.config.mode;
541
+ const modeHandlers = ScheduleModes.get(mode);
542
+
543
+ if (modeHandlers && modeHandlers.on) {
544
+ modeHandlers.on.call(this, workflow);
545
+ }
546
+
547
+ this.inspect([workflow]);
548
+ }
549
+
550
+ off(workflow) {
551
+ const mode = workflow.config.mode;
552
+ const modeHandlers = ScheduleModes.get(mode);
553
+
554
+ if (modeHandlers && modeHandlers.off) {
555
+ modeHandlers.off.call(this, workflow);
556
+ }
557
+
558
+ this.cache.delete(workflow.id);
559
+ }
560
+
561
+ }
562
+
563
+ exports.default = ScheduleTrigger;
564
+ ScheduleTrigger.CacheRules = [// ({ enabled }) => enabled,
565
+ ({
566
+ config,
567
+ executed
568
+ }) => config.limit ? executed < config.limit : true, ({
569
+ config
570
+ }) => ['cron', 'startsOn'].some(key => config[key]), cronInCycle, function (workflow, now) {
571
+ const mode = workflow.config.mode;
572
+ const modeHandlers = ScheduleModes.get(mode);
573
+ return modeHandlers.shouldCache.call(this, workflow, now);
574
+ }];
575
+ ScheduleTrigger.TriggerRules = [({
576
+ config,
577
+ executed
578
+ }) => config.limit ? executed < config.limit : true, ({
579
+ config
580
+ }) => ['cron', 'startsOn'].some(key => config[key]), function (workflow, now) {
581
+ const cron = workflow.config.cron;
582
+
583
+ if (!cron) {
584
+ return true;
585
+ }
586
+
587
+ const currentDate = new Date(now);
588
+ currentDate.setMilliseconds(-1);
589
+ const timestamp = now.getTime();
590
+
591
+ const interval = _cronParser().default.parseExpression(cron, {
592
+ currentDate
593
+ });
594
+
595
+ let next = interval.next();
596
+
597
+ if (next.getTime() === timestamp) {
598
+ return true;
599
+ }
600
+
601
+ return false;
602
+ }];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/plugin-workflow",
3
- "version": "0.7.0-alpha.82",
3
+ "version": "0.7.0-alpha.83",
4
4
  "main": "lib/index.js",
5
5
  "license": "Apache-2.0",
6
6
  "licenses": [
@@ -10,14 +10,16 @@
10
10
  }
11
11
  ],
12
12
  "dependencies": {
13
- "@nocobase/actions": "0.7.0-alpha.82",
14
- "@nocobase/database": "0.7.0-alpha.82",
15
- "@nocobase/server": "0.7.0-alpha.82",
16
- "@nocobase/utils": "0.7.0-alpha.82",
17
- "json-templates": "^4.2.0"
13
+ "@nocobase/actions": "0.7.0-alpha.83",
14
+ "@nocobase/database": "0.7.0-alpha.83",
15
+ "@nocobase/server": "0.7.0-alpha.83",
16
+ "@nocobase/utils": "0.7.0-alpha.83",
17
+ "cron-parser": "4.4.0",
18
+ "json-templates": "^4.2.0",
19
+ "lodash": "^4.17.21"
18
20
  },
19
21
  "devDependencies": {
20
- "@nocobase/test": "0.7.0-alpha.82"
22
+ "@nocobase/test": "0.7.0-alpha.83"
21
23
  },
22
- "gitHead": "4820fd09375c7200d1ea0bb0aab1bd4783b80d3d"
24
+ "gitHead": "838f4f18dcd9ed4fe2ae2660a4918e2a5bd3a869"
23
25
  }