@nocobase/plugin-workflow 0.19.0-alpha.7 → 0.19.0-alpha.9
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/dist/client/index.js +19 -19
- package/dist/client/triggers/schedule/constants.d.ts +1 -1
- package/dist/externalVersion.js +9 -9
- package/dist/locale/zh-CN.json +2 -2
- package/dist/node_modules/cron-parser/package.json +1 -1
- package/dist/node_modules/lru-cache/package.json +1 -1
- package/dist/server/triggers/ScheduleTrigger/DateFieldScheduleTrigger.d.ts +31 -0
- package/dist/server/triggers/ScheduleTrigger/DateFieldScheduleTrigger.js +334 -0
- package/dist/server/triggers/ScheduleTrigger/StaticScheduleTrigger.d.ts +15 -0
- package/dist/server/triggers/ScheduleTrigger/StaticScheduleTrigger.js +129 -0
- package/dist/server/triggers/ScheduleTrigger/index.d.ts +13 -0
- package/dist/server/triggers/ScheduleTrigger/index.js +74 -0
- package/dist/server/triggers/ScheduleTrigger/utils.d.ts +5 -0
- package/dist/server/triggers/ScheduleTrigger/utils.js +35 -0
- package/package.json +5 -5
- package/dist/server/triggers/ScheduleTrigger.d.ts +0 -42
- package/dist/server/triggers/ScheduleTrigger.js +0 -481
package/dist/externalVersion.js
CHANGED
|
@@ -2,21 +2,21 @@ module.exports = {
|
|
|
2
2
|
"react": "18.2.0",
|
|
3
3
|
"antd": "5.12.8",
|
|
4
4
|
"@ant-design/icons": "5.2.6",
|
|
5
|
-
"@nocobase/client": "0.19.0-alpha.
|
|
5
|
+
"@nocobase/client": "0.19.0-alpha.9",
|
|
6
6
|
"react-router-dom": "6.21.0",
|
|
7
|
-
"@nocobase/utils": "0.19.0-alpha.
|
|
7
|
+
"@nocobase/utils": "0.19.0-alpha.9",
|
|
8
8
|
"react-i18next": "11.18.6",
|
|
9
9
|
"@formily/core": "2.3.0",
|
|
10
10
|
"@formily/react": "2.3.0",
|
|
11
|
-
"@nocobase/database": "0.19.0-alpha.
|
|
12
|
-
"@nocobase/server": "0.19.0-alpha.
|
|
13
|
-
"@nocobase/logger": "0.19.0-alpha.
|
|
14
|
-
"@nocobase/evaluators": "0.19.0-alpha.
|
|
11
|
+
"@nocobase/database": "0.19.0-alpha.9",
|
|
12
|
+
"@nocobase/server": "0.19.0-alpha.9",
|
|
13
|
+
"@nocobase/logger": "0.19.0-alpha.9",
|
|
14
|
+
"@nocobase/evaluators": "0.19.0-alpha.9",
|
|
15
15
|
"lodash": "4.17.21",
|
|
16
16
|
"@formily/antd-v5": "1.1.9",
|
|
17
|
-
"@nocobase/actions": "0.19.0-alpha.
|
|
17
|
+
"@nocobase/actions": "0.19.0-alpha.9",
|
|
18
18
|
"sequelize": "6.35.2",
|
|
19
|
-
"@nocobase/plugin-workflow-test": "0.19.0-alpha.
|
|
20
|
-
"@nocobase/test": "0.19.0-alpha.
|
|
19
|
+
"@nocobase/plugin-workflow-test": "0.19.0-alpha.9",
|
|
20
|
+
"@nocobase/test": "0.19.0-alpha.9",
|
|
21
21
|
"dayjs": "1.11.10"
|
|
22
22
|
};
|
package/dist/locale/zh-CN.json
CHANGED
|
@@ -94,8 +94,8 @@
|
|
|
94
94
|
"Scope variables": "局域变量",
|
|
95
95
|
|
|
96
96
|
"Operator": "运算符",
|
|
97
|
-
"Calculate an expression based on a calculation engine and obtain a value as the result. Variables in the upstream nodes can be used in the expression.
|
|
98
|
-
"
|
|
97
|
+
"Calculate an expression based on a calculation engine and obtain a value as the result. Variables in the upstream nodes can be used in the expression.":
|
|
98
|
+
"基于计算引擎对一个表达式进行计算,并获得一个值作为结果。表达式中可以使用上游节点里的变量。",
|
|
99
99
|
"String operation": "字符串",
|
|
100
100
|
"System variables": "系统变量",
|
|
101
101
|
"System time": "系统时间",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"cron-parser","version":"4.4.0","description":"Node.js library for parsing crontab instructions","main":"lib/parser.js","types":"index.d.ts","typesVersions":{"<4.1":{"*":["types/ts3/*"]}},"directories":{"test":"test"},"scripts":{"test:tsd":"tsd","test:unit":"TZ=UTC tap ./test/*.js","test:cover":"TZ=UTC tap --coverage-report=html ./test/*.js","lint":"eslint .","lint:fix":"eslint --fix .","test":"npm run lint && npm run test:unit && npm run test:tsd"},"repository":{"type":"git","url":"https://github.com/harrisiirak/cron-parser.git"},"keywords":["cron","crontab","parser"],"author":"Harri Siirak","contributors":["Nicholas Clawson","Daniel Prentis <daniel@salsitasoft.com>","Renault John Lecoultre","Richard Astbury <richard.astbury@gmail.com>","Meaglin Wasabi <Meaglin.wasabi@gmail.com>","Mike Kusold <hello@mikekusold.com>","Alex Kit <alex.kit@atmajs.com>","Santiago Gimeno <santiago.gimeno@gmail.com>","Daniel <darc.tec@gmail.com>","Christian Steininger <christian.steininger.cs@gmail.com>","Mykola Piskovyi <m.piskovyi@gmail.com>","Brian Vaughn <brian.david.vaughn@gmail.com>","Nicholas Clawson <nickclaw@gmail.com>","Yasuhiroki <yasuhiroki.duck@gmail.com>","Nicholas Clawson <nickclaw@gmail.com>","Brendan Warkentin <faazshift@gmail.com>","Charlie Fish <fishcharlie.code@gmail.com>","Ian Graves <ian+diskimage@iangrav.es>","Andy Thompson <me@andytson.com>","Regev Brody <regevbr@gmail.com>"],"license":"MIT","dependencies":{"luxon":"^1.28.0"},"devDependencies":{"eslint":"^8.2.0","sinon":"^10.0.0","tap":"^16.0.1","tsd":"^0.19.0"},"engines":{"node":">=0.8"},"browser":{"fs":false},"tap":{"check-coverage":false},"tsd":{"directory":"test","compilerOptions":{"lib":["es2017","dom"]}},"_lastModified":"2024-02-
|
|
1
|
+
{"name":"cron-parser","version":"4.4.0","description":"Node.js library for parsing crontab instructions","main":"lib/parser.js","types":"index.d.ts","typesVersions":{"<4.1":{"*":["types/ts3/*"]}},"directories":{"test":"test"},"scripts":{"test:tsd":"tsd","test:unit":"TZ=UTC tap ./test/*.js","test:cover":"TZ=UTC tap --coverage-report=html ./test/*.js","lint":"eslint .","lint:fix":"eslint --fix .","test":"npm run lint && npm run test:unit && npm run test:tsd"},"repository":{"type":"git","url":"https://github.com/harrisiirak/cron-parser.git"},"keywords":["cron","crontab","parser"],"author":"Harri Siirak","contributors":["Nicholas Clawson","Daniel Prentis <daniel@salsitasoft.com>","Renault John Lecoultre","Richard Astbury <richard.astbury@gmail.com>","Meaglin Wasabi <Meaglin.wasabi@gmail.com>","Mike Kusold <hello@mikekusold.com>","Alex Kit <alex.kit@atmajs.com>","Santiago Gimeno <santiago.gimeno@gmail.com>","Daniel <darc.tec@gmail.com>","Christian Steininger <christian.steininger.cs@gmail.com>","Mykola Piskovyi <m.piskovyi@gmail.com>","Brian Vaughn <brian.david.vaughn@gmail.com>","Nicholas Clawson <nickclaw@gmail.com>","Yasuhiroki <yasuhiroki.duck@gmail.com>","Nicholas Clawson <nickclaw@gmail.com>","Brendan Warkentin <faazshift@gmail.com>","Charlie Fish <fishcharlie.code@gmail.com>","Ian Graves <ian+diskimage@iangrav.es>","Andy Thompson <me@andytson.com>","Regev Brody <regevbr@gmail.com>"],"license":"MIT","dependencies":{"luxon":"^1.28.0"},"devDependencies":{"eslint":"^8.2.0","sinon":"^10.0.0","tap":"^16.0.1","tsd":"^0.19.0"},"engines":{"node":">=0.8"},"browser":{"fs":false},"tap":{"check-coverage":false},"tsd":{"directory":"test","compilerOptions":{"lib":["es2017","dom"]}},"_lastModified":"2024-02-28T01:30:12.718Z"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"lru-cache","description":"A cache object that deletes the least-recently-used items.","version":"8.0.5","author":"Isaac Z. Schlueter <i@izs.me>","keywords":["mru","lru","cache"],"sideEffects":false,"scripts":{"build":"npm run prepare","preprepare":"rm -rf dist","prepare":"tsc -p tsconfig.json && tsc -p tsconfig-esm.json","postprepare":"bash fixup.sh","pretest":"npm run prepare","presnap":"npm run prepare","test":"c8 tap","snap":"c8 tap","preversion":"npm test","postversion":"npm publish","prepublishOnly":"git push origin --follow-tags","format":"prettier --write .","typedoc":"typedoc --tsconfig tsconfig-esm.json ./src/*.ts","benchmark-results-typedoc":"bash scripts/benchmark-results-typedoc.sh","prebenchmark":"npm run prepare","benchmark":"make -C benchmark","preprofile":"npm run prepare","profile":"make -C benchmark profile"},"main":"./dist/cjs/index-cjs.js","module":"./dist/mjs/index.js","types":"./dist/mjs/index.d.ts","exports":{"./min":{"import":{"types":"./dist/mjs/index.d.ts","default":"./dist/mjs/index.min.js"},"require":{"types":"./dist/cjs/index.d.ts","default":"./dist/cjs/index.min.js"}},".":{"import":{"types":"./dist/mjs/index.d.ts","default":"./dist/mjs/index.js"},"require":{"types":"./dist/cjs/index.d.ts","default":"./dist/cjs/index-cjs.js"}}},"repository":"git://github.com/isaacs/node-lru-cache.git","devDependencies":{"@size-limit/preset-small-lib":"^7.0.8","@types/node":"^17.0.31","@types/tap":"^15.0.6","benchmark":"^2.1.4","c8":"^7.11.2","clock-mock":"^1.0.6","esbuild":"^0.17.11","eslint-config-prettier":"^8.5.0","marked":"^4.2.12","mkdirp":"^2.1.5","prettier":"^2.6.2","size-limit":"^7.0.8","tap":"^16.3.4","ts-node":"^10.7.0","tslib":"^2.4.0","typedoc":"^0.23.24","typescript":"^4.6.4"},"license":"ISC","files":["dist"],"engines":{"node":">=16.14"},"prettier":{"semi":false,"printWidth":70,"tabWidth":2,"useTabs":false,"singleQuote":true,"jsxSingleQuote":false,"bracketSameLine":true,"arrowParens":"avoid","endOfLine":"lf"},"tap":{"coverage":false,"node-arg":["--expose-gc","--no-warnings","--loader","ts-node/esm"],"ts":false},"size-limit":[{"path":"./dist/mjs/index.js"}],"_lastModified":"2024-02-
|
|
1
|
+
{"name":"lru-cache","description":"A cache object that deletes the least-recently-used items.","version":"8.0.5","author":"Isaac Z. Schlueter <i@izs.me>","keywords":["mru","lru","cache"],"sideEffects":false,"scripts":{"build":"npm run prepare","preprepare":"rm -rf dist","prepare":"tsc -p tsconfig.json && tsc -p tsconfig-esm.json","postprepare":"bash fixup.sh","pretest":"npm run prepare","presnap":"npm run prepare","test":"c8 tap","snap":"c8 tap","preversion":"npm test","postversion":"npm publish","prepublishOnly":"git push origin --follow-tags","format":"prettier --write .","typedoc":"typedoc --tsconfig tsconfig-esm.json ./src/*.ts","benchmark-results-typedoc":"bash scripts/benchmark-results-typedoc.sh","prebenchmark":"npm run prepare","benchmark":"make -C benchmark","preprofile":"npm run prepare","profile":"make -C benchmark profile"},"main":"./dist/cjs/index-cjs.js","module":"./dist/mjs/index.js","types":"./dist/mjs/index.d.ts","exports":{"./min":{"import":{"types":"./dist/mjs/index.d.ts","default":"./dist/mjs/index.min.js"},"require":{"types":"./dist/cjs/index.d.ts","default":"./dist/cjs/index.min.js"}},".":{"import":{"types":"./dist/mjs/index.d.ts","default":"./dist/mjs/index.js"},"require":{"types":"./dist/cjs/index.d.ts","default":"./dist/cjs/index-cjs.js"}}},"repository":"git://github.com/isaacs/node-lru-cache.git","devDependencies":{"@size-limit/preset-small-lib":"^7.0.8","@types/node":"^17.0.31","@types/tap":"^15.0.6","benchmark":"^2.1.4","c8":"^7.11.2","clock-mock":"^1.0.6","esbuild":"^0.17.11","eslint-config-prettier":"^8.5.0","marked":"^4.2.12","mkdirp":"^2.1.5","prettier":"^2.6.2","size-limit":"^7.0.8","tap":"^16.3.4","ts-node":"^10.7.0","tslib":"^2.4.0","typedoc":"^0.23.24","typescript":"^4.6.4"},"license":"ISC","files":["dist"],"engines":{"node":">=16.14"},"prettier":{"semi":false,"printWidth":70,"tabWidth":2,"useTabs":false,"singleQuote":true,"jsxSingleQuote":false,"bracketSameLine":true,"arrowParens":"avoid","endOfLine":"lf"},"tap":{"coverage":false,"node-arg":["--expose-gc","--no-warnings","--loader","ts-node/esm"],"ts":false},"size-limit":[{"path":"./dist/mjs/index.js"}],"_lastModified":"2024-02-28T01:30:12.385Z"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Transactionable } from '@nocobase/database';
|
|
2
|
+
import type Plugin from '../../Plugin';
|
|
3
|
+
import type { WorkflowModel } from '../../types';
|
|
4
|
+
export type ScheduleOnField = {
|
|
5
|
+
field: string;
|
|
6
|
+
offset?: number;
|
|
7
|
+
unit?: 1000 | 60000 | 3600000 | 86400000;
|
|
8
|
+
};
|
|
9
|
+
export interface ScheduleTriggerConfig {
|
|
10
|
+
mode: number;
|
|
11
|
+
repeat?: string | number | null;
|
|
12
|
+
limit?: number;
|
|
13
|
+
startsOn?: ScheduleOnField;
|
|
14
|
+
endsOn?: string | ScheduleOnField;
|
|
15
|
+
}
|
|
16
|
+
export default class ScheduleTrigger {
|
|
17
|
+
workflow: Plugin;
|
|
18
|
+
events: Map<any, any>;
|
|
19
|
+
private timer;
|
|
20
|
+
private cache;
|
|
21
|
+
cacheCycle: number;
|
|
22
|
+
constructor(workflow: Plugin);
|
|
23
|
+
reload(): Promise<void>;
|
|
24
|
+
inspect(workflows: WorkflowModel[]): void;
|
|
25
|
+
loadRecordsToSchedule({ config: { collection, limit, startsOn, repeat, endsOn }, allExecuted }: WorkflowModel, currentDate: Date): Promise<import("@nocobase/database").Model<any, any>[]>;
|
|
26
|
+
getRecordNextTime(workflow: WorkflowModel, record: any, nextSecond?: boolean): any;
|
|
27
|
+
schedule(workflow: WorkflowModel, record: any, nextTime: any, toggle?: boolean, options?: {}): Promise<void>;
|
|
28
|
+
trigger(workflow: WorkflowModel, record: any, nextTime: any, { transaction }?: Transactionable): Promise<void>;
|
|
29
|
+
on(workflow: WorkflowModel): void;
|
|
30
|
+
off(workflow: WorkflowModel): void;
|
|
31
|
+
}
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var DateFieldScheduleTrigger_exports = {};
|
|
29
|
+
__export(DateFieldScheduleTrigger_exports, {
|
|
30
|
+
default: () => ScheduleTrigger
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(DateFieldScheduleTrigger_exports);
|
|
33
|
+
var import_database = require("@nocobase/database");
|
|
34
|
+
var import_cron_parser = __toESM(require("cron-parser"));
|
|
35
|
+
var import_utils = require("./utils");
|
|
36
|
+
function getOnTimestampWithOffset({ field, offset = 0, unit = 1e3 }, now) {
|
|
37
|
+
if (!field) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
const timestamp = now.getTime();
|
|
41
|
+
return timestamp - offset * unit;
|
|
42
|
+
}
|
|
43
|
+
function getDataOptionTime(record, on, dir = 1) {
|
|
44
|
+
if (!on) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
switch (typeof on) {
|
|
48
|
+
case "string": {
|
|
49
|
+
const time = (0, import_utils.parseDateWithoutMs)(on);
|
|
50
|
+
return time ? time : null;
|
|
51
|
+
}
|
|
52
|
+
case "object": {
|
|
53
|
+
const { field, offset = 0, unit = 1e3 } = on;
|
|
54
|
+
if (!record.get(field)) {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
const second = new Date(record.get(field).getTime());
|
|
58
|
+
second.setMilliseconds(0);
|
|
59
|
+
return second.getTime() + offset * unit * dir;
|
|
60
|
+
}
|
|
61
|
+
default:
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const DialectTimestampFnMap = {
|
|
66
|
+
postgres(col) {
|
|
67
|
+
return `CAST(FLOOR(extract(epoch from "${col}")) AS INTEGER)`;
|
|
68
|
+
},
|
|
69
|
+
mysql(col) {
|
|
70
|
+
return `CAST(FLOOR(UNIX_TIMESTAMP(\`${col}\`)) AS SIGNED INTEGER)`;
|
|
71
|
+
},
|
|
72
|
+
sqlite(col) {
|
|
73
|
+
return `CAST(FLOOR(unixepoch(${col})) AS INTEGER)`;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
DialectTimestampFnMap.mariadb = DialectTimestampFnMap.mysql;
|
|
77
|
+
function getCronNextTime(cron, currentDate) {
|
|
78
|
+
const interval = import_cron_parser.default.parseExpression(cron, { currentDate });
|
|
79
|
+
const next = interval.next();
|
|
80
|
+
return next.getTime();
|
|
81
|
+
}
|
|
82
|
+
function matchCronNextTime(cron, currentDate, range) {
|
|
83
|
+
return getCronNextTime(cron, currentDate) - currentDate.getTime() <= range;
|
|
84
|
+
}
|
|
85
|
+
function getHookId(workflow, type) {
|
|
86
|
+
return `${type}#${workflow.id}`;
|
|
87
|
+
}
|
|
88
|
+
class ScheduleTrigger {
|
|
89
|
+
constructor(workflow) {
|
|
90
|
+
this.workflow = workflow;
|
|
91
|
+
workflow.app.on("afterStart", async () => {
|
|
92
|
+
if (this.timer) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
this.timer = setInterval(() => this.reload(), this.cacheCycle);
|
|
96
|
+
this.reload();
|
|
97
|
+
});
|
|
98
|
+
workflow.app.on("beforeStop", () => {
|
|
99
|
+
if (this.timer) {
|
|
100
|
+
clearInterval(this.timer);
|
|
101
|
+
}
|
|
102
|
+
for (const [key, timer] of this.cache.entries()) {
|
|
103
|
+
clearTimeout(timer);
|
|
104
|
+
this.cache.delete(key);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
events = /* @__PURE__ */ new Map();
|
|
109
|
+
timer = null;
|
|
110
|
+
cache = /* @__PURE__ */ new Map();
|
|
111
|
+
// caching workflows in range, default to 5min
|
|
112
|
+
cacheCycle = 3e5;
|
|
113
|
+
async reload() {
|
|
114
|
+
const WorkflowRepo = this.workflow.app.db.getRepository("workflows");
|
|
115
|
+
const workflows = await WorkflowRepo.find({
|
|
116
|
+
filter: { enabled: true, type: "schedule", "config.mode": import_utils.SCHEDULE_MODE.DATE_FIELD }
|
|
117
|
+
});
|
|
118
|
+
this.cache = /* @__PURE__ */ new Map();
|
|
119
|
+
this.inspect(workflows);
|
|
120
|
+
}
|
|
121
|
+
inspect(workflows) {
|
|
122
|
+
const now = /* @__PURE__ */ new Date();
|
|
123
|
+
workflows.forEach(async (workflow) => {
|
|
124
|
+
const records = await this.loadRecordsToSchedule(workflow, now);
|
|
125
|
+
records.forEach((record) => {
|
|
126
|
+
const nextTime = this.getRecordNextTime(workflow, record);
|
|
127
|
+
this.schedule(workflow, record, nextTime, Boolean(nextTime));
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
// 1. startsOn in range -> yes
|
|
132
|
+
// 2. startsOn before now, has no repeat -> no
|
|
133
|
+
// 3. startsOn before now, and has repeat:
|
|
134
|
+
// a. repeat out of range -> no
|
|
135
|
+
// b. repeat in range (number or cron):
|
|
136
|
+
// i. endsOn after now -> yes
|
|
137
|
+
// ii. endsOn before now -> no
|
|
138
|
+
async loadRecordsToSchedule({ config: { collection, limit, startsOn, repeat, endsOn }, allExecuted }, currentDate) {
|
|
139
|
+
const { db } = this.workflow.app;
|
|
140
|
+
if (limit && allExecuted >= limit) {
|
|
141
|
+
return [];
|
|
142
|
+
}
|
|
143
|
+
if (!startsOn) {
|
|
144
|
+
return [];
|
|
145
|
+
}
|
|
146
|
+
const timestamp = currentDate.getTime();
|
|
147
|
+
const startTimestamp = getOnTimestampWithOffset(startsOn, currentDate);
|
|
148
|
+
if (!startTimestamp) {
|
|
149
|
+
return [];
|
|
150
|
+
}
|
|
151
|
+
const range = this.cacheCycle * 2;
|
|
152
|
+
const conditions = [
|
|
153
|
+
{
|
|
154
|
+
[startsOn.field]: {
|
|
155
|
+
// cache next 2 cycles
|
|
156
|
+
[import_database.Op.lt]: new Date(startTimestamp + range)
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
];
|
|
160
|
+
if (repeat) {
|
|
161
|
+
if (typeof repeat === "number") {
|
|
162
|
+
const tsFn = DialectTimestampFnMap[db.options.dialect];
|
|
163
|
+
if (repeat > range && tsFn) {
|
|
164
|
+
const modExp = (0, import_database.fn)(
|
|
165
|
+
"MOD",
|
|
166
|
+
(0, import_database.literal)(`${Math.round(timestamp / 1e3)} - ${tsFn(startsOn.field)}`),
|
|
167
|
+
Math.round(repeat / 1e3)
|
|
168
|
+
);
|
|
169
|
+
conditions.push((0, import_database.where)(modExp, { [import_database.Op.lt]: Math.round(range / 1e3) }));
|
|
170
|
+
}
|
|
171
|
+
} else if (typeof repeat === "string") {
|
|
172
|
+
if (!matchCronNextTime(repeat, currentDate, range)) {
|
|
173
|
+
return [];
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (endsOn) {
|
|
177
|
+
const now = /* @__PURE__ */ new Date();
|
|
178
|
+
const endTimestamp = getOnTimestampWithOffset(endsOn, now);
|
|
179
|
+
if (!endTimestamp) {
|
|
180
|
+
return [];
|
|
181
|
+
}
|
|
182
|
+
if (typeof endsOn === "string") {
|
|
183
|
+
if (endTimestamp <= timestamp) {
|
|
184
|
+
return [];
|
|
185
|
+
}
|
|
186
|
+
} else {
|
|
187
|
+
conditions.push({
|
|
188
|
+
[endsOn.field]: {
|
|
189
|
+
[import_database.Op.gte]: new Date(endTimestamp)
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
} else {
|
|
195
|
+
conditions.push({
|
|
196
|
+
[startsOn.field]: {
|
|
197
|
+
[import_database.Op.gte]: new Date(startTimestamp)
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
const { model } = db.getCollection(collection);
|
|
202
|
+
return model.findAll({
|
|
203
|
+
where: {
|
|
204
|
+
[import_database.Op.and]: conditions
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
getRecordNextTime(workflow, record, nextSecond = false) {
|
|
209
|
+
const {
|
|
210
|
+
config: { startsOn, endsOn, repeat, limit },
|
|
211
|
+
allExecuted
|
|
212
|
+
} = workflow;
|
|
213
|
+
if (limit && allExecuted >= limit) {
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
const range = this.cacheCycle;
|
|
217
|
+
const now = /* @__PURE__ */ new Date();
|
|
218
|
+
now.setMilliseconds(nextSecond ? 1e3 : 0);
|
|
219
|
+
const timestamp = now.getTime();
|
|
220
|
+
const startTime = getDataOptionTime(record, startsOn);
|
|
221
|
+
const endTime = getDataOptionTime(record, endsOn);
|
|
222
|
+
let nextTime = null;
|
|
223
|
+
if (!startTime) {
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
if (startTime > timestamp + range) {
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
if (startTime >= timestamp) {
|
|
230
|
+
return !endTime || endTime >= startTime && endTime < timestamp + range ? startTime : null;
|
|
231
|
+
} else {
|
|
232
|
+
if (!repeat) {
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (typeof repeat === "number") {
|
|
237
|
+
const nextRepeatTime = (startTime - timestamp) % repeat + repeat;
|
|
238
|
+
if (nextRepeatTime > range) {
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
if (endTime && endTime < timestamp + nextRepeatTime) {
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
nextTime = timestamp + nextRepeatTime;
|
|
245
|
+
} else if (typeof repeat === "string") {
|
|
246
|
+
nextTime = getCronNextTime(repeat, now);
|
|
247
|
+
if (nextTime - timestamp > range) {
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
if (endTime && endTime < nextTime) {
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
if (endTime && endTime <= timestamp) {
|
|
255
|
+
return null;
|
|
256
|
+
}
|
|
257
|
+
return nextTime;
|
|
258
|
+
}
|
|
259
|
+
schedule(workflow, record, nextTime, toggle = true, options = {}) {
|
|
260
|
+
const { model } = this.workflow.app.db.getCollection(workflow.config.collection);
|
|
261
|
+
const recordPk = record.get(model.primaryKeyAttribute);
|
|
262
|
+
if (toggle) {
|
|
263
|
+
const nextInterval = Math.max(0, nextTime - Date.now());
|
|
264
|
+
const key = `${workflow.id}:${recordPk}@${nextTime}`;
|
|
265
|
+
if (!this.cache.has(key)) {
|
|
266
|
+
if (nextInterval) {
|
|
267
|
+
this.cache.set(key, setTimeout(this.trigger.bind(this, workflow, record, nextTime), nextInterval));
|
|
268
|
+
} else {
|
|
269
|
+
return this.trigger(workflow, record, nextTime, options);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
} else {
|
|
273
|
+
for (const [key, timer] of this.cache.entries()) {
|
|
274
|
+
if (key.startsWith(`${workflow.id}:${recordPk}@`)) {
|
|
275
|
+
clearTimeout(timer);
|
|
276
|
+
this.cache.delete(key);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
async trigger(workflow, record, nextTime, { transaction } = {}) {
|
|
282
|
+
const { repository, model } = this.workflow.app.db.getCollection(workflow.config.collection);
|
|
283
|
+
const recordPk = record.get(model.primaryKeyAttribute);
|
|
284
|
+
const data = await repository.findOne({
|
|
285
|
+
filterByTk: recordPk,
|
|
286
|
+
appends: workflow.config.appends,
|
|
287
|
+
transaction
|
|
288
|
+
});
|
|
289
|
+
const key = `${workflow.id}:${recordPk}@${nextTime}`;
|
|
290
|
+
this.cache.delete(key);
|
|
291
|
+
this.workflow.trigger(workflow, {
|
|
292
|
+
data: data.toJSON(),
|
|
293
|
+
date: new Date(nextTime)
|
|
294
|
+
});
|
|
295
|
+
if (!workflow.config.repeat || workflow.config.limit && workflow.allExecuted >= workflow.config.limit - 1) {
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
const n = this.getRecordNextTime(workflow, data, true);
|
|
299
|
+
if (n) {
|
|
300
|
+
this.schedule(workflow, data, n, true);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
on(workflow) {
|
|
304
|
+
this.inspect([workflow]);
|
|
305
|
+
const { collection } = workflow.config;
|
|
306
|
+
const event = `${collection}.afterSaveWithAssociations`;
|
|
307
|
+
const name = getHookId(workflow, event);
|
|
308
|
+
if (this.events.has(name)) {
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
const listener = async (data, { transaction }) => {
|
|
312
|
+
const nextTime = this.getRecordNextTime(workflow, data);
|
|
313
|
+
return this.schedule(workflow, data, nextTime, Boolean(nextTime), { transaction });
|
|
314
|
+
};
|
|
315
|
+
this.events.set(name, listener);
|
|
316
|
+
this.workflow.app.db.on(event, listener);
|
|
317
|
+
}
|
|
318
|
+
off(workflow) {
|
|
319
|
+
for (const [key, timer] of this.cache.entries()) {
|
|
320
|
+
if (key.startsWith(`${workflow.id}:`)) {
|
|
321
|
+
clearTimeout(timer);
|
|
322
|
+
this.cache.delete(key);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
const { collection } = workflow.config;
|
|
326
|
+
const event = `${collection}.afterSave`;
|
|
327
|
+
const name = getHookId(workflow, event);
|
|
328
|
+
if (this.events.has(name)) {
|
|
329
|
+
const listener = this.events.get(name);
|
|
330
|
+
this.events.delete(name);
|
|
331
|
+
this.workflow.app.db.off(event, listener);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type Plugin from '../../Plugin';
|
|
2
|
+
export default class StaticScheduleTrigger {
|
|
3
|
+
workflow: Plugin;
|
|
4
|
+
private timers;
|
|
5
|
+
constructor(workflow: Plugin);
|
|
6
|
+
inspect(workflows: any): void;
|
|
7
|
+
getNextTime({ config, allExecuted }: {
|
|
8
|
+
config: any;
|
|
9
|
+
allExecuted: any;
|
|
10
|
+
}, currentDate: any, nextSecond?: boolean): any;
|
|
11
|
+
schedule(workflow: any, nextTime: any, toggle?: boolean): void;
|
|
12
|
+
trigger(workflow: any, time: any): Promise<void>;
|
|
13
|
+
on(workflow: any): void;
|
|
14
|
+
off(workflow: any): void;
|
|
15
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var StaticScheduleTrigger_exports = {};
|
|
29
|
+
__export(StaticScheduleTrigger_exports, {
|
|
30
|
+
default: () => StaticScheduleTrigger
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(StaticScheduleTrigger_exports);
|
|
33
|
+
var import_cron_parser = __toESM(require("cron-parser"));
|
|
34
|
+
var import_utils = require("./utils");
|
|
35
|
+
class StaticScheduleTrigger {
|
|
36
|
+
constructor(workflow) {
|
|
37
|
+
this.workflow = workflow;
|
|
38
|
+
workflow.app.on("afterStart", async () => {
|
|
39
|
+
const WorkflowRepo = this.workflow.app.db.getRepository("workflows");
|
|
40
|
+
const workflows = await WorkflowRepo.find({
|
|
41
|
+
filter: { enabled: true, type: "schedule", "config.mode": import_utils.SCHEDULE_MODE.STATIC }
|
|
42
|
+
});
|
|
43
|
+
this.inspect(workflows);
|
|
44
|
+
});
|
|
45
|
+
workflow.app.on("beforeStop", () => {
|
|
46
|
+
for (const timer of this.timers.values()) {
|
|
47
|
+
clearInterval(timer);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
timers = /* @__PURE__ */ new Map();
|
|
52
|
+
inspect(workflows) {
|
|
53
|
+
const now = /* @__PURE__ */ new Date();
|
|
54
|
+
now.setMilliseconds(0);
|
|
55
|
+
workflows.forEach((workflow) => {
|
|
56
|
+
const nextTime = this.getNextTime(workflow, now);
|
|
57
|
+
if (nextTime) {
|
|
58
|
+
this.workflow.getLogger(workflow.id).info(`caching scheduled workflow will run at: ${new Date(nextTime).toISOString()}`);
|
|
59
|
+
} else {
|
|
60
|
+
this.workflow.getLogger(workflow.id).info("workflow will not be scheduled");
|
|
61
|
+
}
|
|
62
|
+
this.schedule(workflow, nextTime, nextTime >= now.getTime());
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
getNextTime({ config, allExecuted }, currentDate, nextSecond = false) {
|
|
66
|
+
if (config.limit && allExecuted >= config.limit) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
if (!config.startsOn) {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
currentDate.setMilliseconds(nextSecond ? 1e3 : 0);
|
|
73
|
+
const timestamp = currentDate.getTime();
|
|
74
|
+
const startTime = (0, import_utils.parseDateWithoutMs)(config.startsOn);
|
|
75
|
+
if (startTime > timestamp) {
|
|
76
|
+
return startTime;
|
|
77
|
+
}
|
|
78
|
+
if (config.repeat) {
|
|
79
|
+
const endTime = config.endsOn ? (0, import_utils.parseDateWithoutMs)(config.endsOn) : null;
|
|
80
|
+
if (endTime && endTime < timestamp) {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
if (typeof config.repeat === "string") {
|
|
84
|
+
const interval = import_cron_parser.default.parseExpression(config.repeat, { currentDate });
|
|
85
|
+
const next = interval.next();
|
|
86
|
+
return next.getTime();
|
|
87
|
+
} else if (typeof config.repeat === "number") {
|
|
88
|
+
return timestamp + (timestamp - startTime) % config.repeat;
|
|
89
|
+
} else {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
} else {
|
|
93
|
+
if (startTime < timestamp) {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
return timestamp;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
schedule(workflow, nextTime, toggle = true) {
|
|
100
|
+
const key = `${workflow.id}@${nextTime}`;
|
|
101
|
+
if (toggle) {
|
|
102
|
+
if (!this.timers.has(key)) {
|
|
103
|
+
const interval = Math.max(nextTime - Date.now(), 0);
|
|
104
|
+
this.timers.set(key, setTimeout(this.trigger.bind(this, workflow, nextTime), interval));
|
|
105
|
+
}
|
|
106
|
+
} else {
|
|
107
|
+
const timer = this.timers.get(key);
|
|
108
|
+
clearTimeout(timer);
|
|
109
|
+
this.timers.delete(key);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
async trigger(workflow, time) {
|
|
113
|
+
this.timers.delete(`${workflow.id}@${time}`);
|
|
114
|
+
this.workflow.trigger(workflow, { date: new Date(time) });
|
|
115
|
+
if (!workflow.config.repeat || workflow.config.limit && workflow.allExecuted >= workflow.config.limit - 1) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const nextTime = this.getNextTime(workflow, /* @__PURE__ */ new Date(), true);
|
|
119
|
+
if (nextTime) {
|
|
120
|
+
this.schedule(workflow, nextTime);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
on(workflow) {
|
|
124
|
+
this.inspect([workflow]);
|
|
125
|
+
}
|
|
126
|
+
off(workflow) {
|
|
127
|
+
this.schedule(workflow, null, false);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Transactionable } from 'sequelize';
|
|
2
|
+
import Trigger from '..';
|
|
3
|
+
import type Plugin from '../../Plugin';
|
|
4
|
+
import { WorkflowModel } from '../../types';
|
|
5
|
+
export default class ScheduleTrigger extends Trigger {
|
|
6
|
+
sync: boolean;
|
|
7
|
+
private modes;
|
|
8
|
+
constructor(workflow: Plugin);
|
|
9
|
+
private getTrigger;
|
|
10
|
+
on(workflow: any): void;
|
|
11
|
+
off(workflow: any): void;
|
|
12
|
+
validateEvent(workflow: WorkflowModel, context: any, options: Transactionable): Promise<boolean>;
|
|
13
|
+
}
|