@nocobase/plugin-workflow 0.19.0-alpha.8 → 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.
@@ -0,0 +1,74 @@
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 ScheduleTrigger_exports = {};
29
+ __export(ScheduleTrigger_exports, {
30
+ default: () => ScheduleTrigger
31
+ });
32
+ module.exports = __toCommonJS(ScheduleTrigger_exports);
33
+ var import__ = __toESM(require(".."));
34
+ var import_DateFieldScheduleTrigger = __toESM(require("./DateFieldScheduleTrigger"));
35
+ var import_StaticScheduleTrigger = __toESM(require("./StaticScheduleTrigger"));
36
+ var import_utils = require("./utils");
37
+ class ScheduleTrigger extends import__.default {
38
+ sync = false;
39
+ modes = /* @__PURE__ */ new Map();
40
+ constructor(workflow) {
41
+ super(workflow);
42
+ this.modes.set(import_utils.SCHEDULE_MODE.STATIC, new import_StaticScheduleTrigger.default(workflow));
43
+ this.modes.set(import_utils.SCHEDULE_MODE.DATE_FIELD, new import_DateFieldScheduleTrigger.default(workflow));
44
+ }
45
+ getTrigger(mode) {
46
+ return this.modes.get(mode);
47
+ }
48
+ on(workflow) {
49
+ const mode = workflow.config.mode;
50
+ const trigger = this.getTrigger(mode);
51
+ if (trigger) {
52
+ trigger.on(workflow);
53
+ }
54
+ }
55
+ off(workflow) {
56
+ const mode = workflow.config.mode;
57
+ const trigger = this.getTrigger(mode);
58
+ if (trigger) {
59
+ trigger.off(workflow);
60
+ }
61
+ }
62
+ async validateEvent(workflow, context, options) {
63
+ if (!context.date) {
64
+ return false;
65
+ }
66
+ const existed = await workflow.countExecutions({
67
+ where: {
68
+ "context.date": context.date
69
+ },
70
+ transaction: options.transaction
71
+ });
72
+ return !existed;
73
+ }
74
+ }
@@ -0,0 +1,5 @@
1
+ export declare const SCHEDULE_MODE: {
2
+ readonly STATIC: 0;
3
+ readonly DATE_FIELD: 1;
4
+ };
5
+ export declare function parseDateWithoutMs(date: string): number;
@@ -0,0 +1,35 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var utils_exports = {};
19
+ __export(utils_exports, {
20
+ SCHEDULE_MODE: () => SCHEDULE_MODE,
21
+ parseDateWithoutMs: () => parseDateWithoutMs
22
+ });
23
+ module.exports = __toCommonJS(utils_exports);
24
+ const SCHEDULE_MODE = {
25
+ STATIC: 0,
26
+ DATE_FIELD: 1
27
+ };
28
+ function parseDateWithoutMs(date) {
29
+ return Math.floor(Date.parse(date) / 1e3) * 1e3;
30
+ }
31
+ // Annotate the CommonJS export names for ESM import in node:
32
+ 0 && (module.exports = {
33
+ SCHEDULE_MODE,
34
+ parseDateWithoutMs
35
+ });
package/package.json CHANGED
@@ -2,15 +2,15 @@
2
2
  "name": "@nocobase/plugin-workflow",
3
3
  "displayName": "Workflow",
4
4
  "displayName.zh-CN": "工作流",
5
- "description": "A powerful workflow plugin designed to support business process management and automation.",
6
- "description.zh-CN": "工作流插件,为业务流程管理和自动化提供支持。",
7
- "version": "0.19.0-alpha.8",
5
+ "description": "A powerful BPM tool that provides foundational support for business automation, with the capability to extend unlimited triggers and nodes.",
6
+ "description.zh-CN": "一个强大的 BPM 工具,为业务自动化提供基础支持,并且可任意扩展更多的触发器和节点。",
7
+ "version": "0.19.0-alpha.9",
8
8
  "license": "AGPL-3.0",
9
9
  "main": "./dist/server/index.js",
10
10
  "homepage": "https://docs.nocobase.com/plugins/workflow",
11
11
  "homepage.zh-CN": "https://docs-cn.nocobase.com/plugins/workflow",
12
12
  "dependencies": {
13
- "@nocobase/plugin-workflow-test": "0.19.0-alpha.8"
13
+ "@nocobase/plugin-workflow-test": "0.19.0-alpha.9"
14
14
  },
15
15
  "devDependencies": {
16
16
  "@ant-design/icons": "5.x",
@@ -45,7 +45,7 @@
45
45
  "@nocobase/test": "0.x",
46
46
  "@nocobase/utils": "0.x"
47
47
  },
48
- "gitHead": "b8fbe613e320e40ebf2da1bf64907eab0197b589",
48
+ "gitHead": "975f9c58a1995df6a8d7a4d191a0dc4a9769ebc5",
49
49
  "keywords": [
50
50
  "Workflow"
51
51
  ]
@@ -1,42 +0,0 @@
1
- import type Plugin from '../Plugin';
2
- import Trigger from '.';
3
- export type ScheduleOnField = string | {
4
- field: string;
5
- offset?: number;
6
- unit?: 1000 | 60000 | 3600000 | 86400000;
7
- };
8
- export interface ScheduleTriggerConfig {
9
- mode: number;
10
- repeat?: string | number | null;
11
- limit?: number;
12
- startsOn?: ScheduleOnField;
13
- endsOn?: ScheduleOnField;
14
- }
15
- export declare const SCHEDULE_MODE: {
16
- readonly CONSTANT: 0;
17
- readonly COLLECTION_FIELD: 1;
18
- };
19
- declare function matchNext(this: ScheduleTrigger, workflow: any, now: Date, range?: number): boolean;
20
- export default class ScheduleTrigger extends Trigger {
21
- sync: boolean;
22
- static CacheRules: (typeof matchNext | ((workflow: any, now: any) => any))[];
23
- static TriggerRules: ((workflow: any, now: any) => any)[];
24
- events: Map<any, any>;
25
- private timer;
26
- private cache;
27
- interval: number;
28
- cacheCycle: number;
29
- constructor(workflow: Plugin);
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
- }
42
- export {};
@@ -1,481 +0,0 @@
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 ScheduleTrigger_exports = {};
29
- __export(ScheduleTrigger_exports, {
30
- SCHEDULE_MODE: () => SCHEDULE_MODE,
31
- default: () => ScheduleTrigger
32
- });
33
- module.exports = __toCommonJS(ScheduleTrigger_exports);
34
- var import_database = require("@nocobase/database");
35
- var import_cron_parser = __toESM(require("cron-parser"));
36
- var import__ = __toESM(require("."));
37
- const SCHEDULE_MODE = {
38
- CONSTANT: 0,
39
- COLLECTION_FIELD: 1
40
- };
41
- const ScheduleModes = /* @__PURE__ */ new Map();
42
- function parseDateWithoutMs(date) {
43
- return Math.floor(Date.parse(date) / 1e3) * 1e3;
44
- }
45
- ScheduleModes.set(SCHEDULE_MODE.CONSTANT, {
46
- shouldCache(workflow, now) {
47
- const { startsOn, endsOn, repeat } = workflow.config;
48
- const timestamp = now.getTime();
49
- const startTime = parseDateWithoutMs(startsOn);
50
- if (!startTime || startTime > timestamp + this.cacheCycle) {
51
- return false;
52
- }
53
- if (repeat) {
54
- if (typeof repeat === "number") {
55
- const next = timestamp - (timestamp - startTime) % repeat + repeat;
56
- if (next <= timestamp || next > timestamp + this.cacheCycle) {
57
- return false;
58
- }
59
- }
60
- if (endsOn) {
61
- const endTime = parseDateWithoutMs(endsOn);
62
- if (!endTime || endTime <= timestamp) {
63
- return false;
64
- }
65
- }
66
- } else {
67
- if (startTime <= timestamp) {
68
- return false;
69
- }
70
- }
71
- return true;
72
- },
73
- trigger(workflow, now) {
74
- const { startsOn, endsOn, repeat } = workflow.config;
75
- const timestamp = now.getTime();
76
- const startTime = parseDateWithoutMs(startsOn);
77
- if (!startTime || startTime > timestamp) {
78
- return 0;
79
- }
80
- if (repeat) {
81
- if (typeof repeat === "number") {
82
- if (Math.round(timestamp - startTime) % repeat) {
83
- return 0;
84
- }
85
- }
86
- if (endsOn) {
87
- const endTime = parseDateWithoutMs(endsOn);
88
- if (!endTime || endTime < timestamp) {
89
- return 0;
90
- }
91
- }
92
- } else {
93
- if (startTime !== timestamp) {
94
- return 0;
95
- }
96
- }
97
- this.workflow.trigger(workflow, { date: now });
98
- return 1;
99
- }
100
- });
101
- function getOnTimestampWithOffset(on, now) {
102
- switch (typeof on) {
103
- case "string":
104
- return parseDateWithoutMs(on);
105
- case "object": {
106
- const { field, offset = 0, unit = 1e3 } = on;
107
- if (!field) {
108
- return null;
109
- }
110
- const timestamp = now.getTime();
111
- return timestamp - offset * unit;
112
- }
113
- default:
114
- return null;
115
- }
116
- }
117
- function getDataOptionTime(data, on, dir = 1) {
118
- if (!on) {
119
- return null;
120
- }
121
- switch (typeof on) {
122
- case "string": {
123
- const time = parseDateWithoutMs(on);
124
- return time ? time : null;
125
- }
126
- case "object": {
127
- const { field, offset = 0, unit = 1e3 } = on;
128
- return data.get(field) ? data.get(field).getTime() - offset * unit * dir : null;
129
- }
130
- default:
131
- return null;
132
- }
133
- }
134
- function getHookId(workflow, type) {
135
- return `${type}#${workflow.id}`;
136
- }
137
- const DialectTimestampFnMap = {
138
- postgres(col) {
139
- return `CAST(FLOOR(extract(epoch from "${col}")) AS INTEGER)`;
140
- },
141
- mysql(col) {
142
- return `CAST(FLOOR(UNIX_TIMESTAMP(\`${col}\`)) AS SIGNED INTEGER)`;
143
- },
144
- sqlite(col) {
145
- return `CAST(FLOOR(unixepoch(${col})) AS INTEGER)`;
146
- }
147
- };
148
- DialectTimestampFnMap.mariadb = DialectTimestampFnMap.mysql;
149
- ScheduleModes.set(SCHEDULE_MODE.COLLECTION_FIELD, {
150
- on(workflow) {
151
- const { collection, startsOn, endsOn, repeat } = workflow.config;
152
- const event = `${collection}.afterSave`;
153
- const name = getHookId(workflow, event);
154
- if (this.events.has(name)) {
155
- return;
156
- }
157
- const listener = async (data, options) => {
158
- const now = /* @__PURE__ */ new Date();
159
- now.setMilliseconds(0);
160
- const timestamp = now.getTime();
161
- const startTime = getDataOptionTime(data, startsOn);
162
- const endTime = getDataOptionTime(data, endsOn, -1);
163
- if (!startTime) {
164
- return;
165
- }
166
- if (startTime && startTime > timestamp + this.cacheCycle) {
167
- return;
168
- }
169
- if (endTime && endTime <= timestamp) {
170
- return;
171
- }
172
- if (!matchNext.call(this, workflow, now)) {
173
- return;
174
- }
175
- if (typeof repeat === "number" && repeat > this.cacheCycle && (timestamp - startTime) % repeat > this.cacheCycle) {
176
- return;
177
- }
178
- this.setCache(workflow);
179
- };
180
- this.events.set(name, listener);
181
- this.workflow.app.db.on(event, listener);
182
- },
183
- off(workflow) {
184
- const { collection } = workflow.config;
185
- const event = `${collection}.afterSave`;
186
- const name = getHookId(workflow, event);
187
- if (this.events.has(name)) {
188
- const listener = this.events.get(name);
189
- this.events.delete(name);
190
- this.workflow.app.db.off(event, listener);
191
- }
192
- },
193
- async shouldCache(workflow, now) {
194
- const { db } = this.workflow.app;
195
- const { startsOn, endsOn, repeat, collection } = workflow.config;
196
- const timestamp = now.getTime();
197
- const startTimestamp = getOnTimestampWithOffset(startsOn, now);
198
- if (!startTimestamp) {
199
- return false;
200
- }
201
- const conditions = [
202
- {
203
- [startsOn.field]: {
204
- [import_database.Op.lt]: new Date(startTimestamp + this.cacheCycle)
205
- }
206
- }
207
- ];
208
- if (repeat) {
209
- const tsFn = DialectTimestampFnMap[db.options.dialect];
210
- if (typeof repeat === "number" && repeat > this.cacheCycle && tsFn) {
211
- const modExp = (0, import_database.fn)(
212
- "MOD",
213
- (0, import_database.literal)(`${Math.round(timestamp / 1e3)} - ${tsFn(startsOn.field)}`),
214
- Math.round(repeat / 1e3)
215
- );
216
- conditions.push((0, import_database.where)(modExp, { [import_database.Op.lt]: Math.round(this.cacheCycle / 1e3) }));
217
- }
218
- if (endsOn) {
219
- const endTimestamp = getOnTimestampWithOffset(endsOn, now);
220
- if (!endTimestamp) {
221
- return false;
222
- }
223
- if (typeof endsOn === "string") {
224
- if (endTimestamp <= timestamp) {
225
- return false;
226
- }
227
- } else {
228
- conditions.push({
229
- [endsOn.field]: {
230
- [import_database.Op.gte]: new Date(endTimestamp + this.interval)
231
- }
232
- });
233
- }
234
- }
235
- } else {
236
- conditions.push({
237
- [startsOn.field]: {
238
- [import_database.Op.gte]: new Date(startTimestamp)
239
- }
240
- });
241
- }
242
- const { model } = db.getCollection(collection);
243
- const count = await model.count({
244
- where: { [import_database.Op.and]: conditions }
245
- });
246
- return Boolean(count);
247
- },
248
- async trigger(workflow, now) {
249
- const { startsOn, repeat, endsOn, collection, appends } = workflow.config;
250
- const timestamp = now.getTime();
251
- const startTimestamp = getOnTimestampWithOffset(startsOn, now);
252
- if (!startTimestamp) {
253
- return 0;
254
- }
255
- const conditions = [
256
- {
257
- [startsOn.field]: {
258
- [import_database.Op.lt]: new Date(startTimestamp + this.interval)
259
- }
260
- }
261
- ];
262
- if (repeat) {
263
- conditions.push({
264
- [startsOn.field]: {
265
- [import_database.Op.lt]: new Date(startTimestamp)
266
- }
267
- });
268
- const tsFn = DialectTimestampFnMap[this.workflow.app.db.options.dialect];
269
- if (typeof repeat === "number" && tsFn) {
270
- const modExp = (0, import_database.fn)(
271
- "MOD",
272
- (0, import_database.literal)(`${Math.round(timestamp / 1e3)} - ${tsFn(startsOn.field)}`),
273
- Math.round(repeat / 1e3)
274
- );
275
- conditions.push((0, import_database.where)(modExp, { [import_database.Op.eq]: 0 }));
276
- }
277
- if (endsOn) {
278
- const endTimestamp = getOnTimestampWithOffset(endsOn, now);
279
- if (!endTimestamp) {
280
- return 0;
281
- }
282
- if (typeof endsOn === "string") {
283
- if (endTimestamp <= timestamp) {
284
- return 0;
285
- }
286
- } else {
287
- conditions.push({
288
- [endsOn.field]: {
289
- [import_database.Op.gte]: new Date(endTimestamp + this.interval)
290
- }
291
- });
292
- }
293
- }
294
- } else {
295
- conditions.push({
296
- [startsOn.field]: {
297
- [import_database.Op.gte]: new Date(startTimestamp)
298
- }
299
- });
300
- }
301
- const repo = this.workflow.app.db.getRepository(collection);
302
- const instances = await repo.find({
303
- where: {
304
- [import_database.Op.and]: conditions
305
- },
306
- appends,
307
- ...workflow.config.limit ? {
308
- limit: Math.max(workflow.config.limit - workflow.allExecuted, 0)
309
- } : {}
310
- });
311
- instances.forEach((item) => {
312
- this.workflow.trigger(workflow, {
313
- date: now,
314
- data: item.toJSON()
315
- });
316
- });
317
- return instances.length;
318
- }
319
- });
320
- function matchNext(workflow, now, range = this.cacheCycle) {
321
- const { repeat } = workflow.config;
322
- if (typeof repeat !== "string") {
323
- return true;
324
- }
325
- const currentDate = new Date(now);
326
- currentDate.setMilliseconds(-1);
327
- const timestamp = now.getTime();
328
- const interval = import_cron_parser.default.parseExpression(repeat, { currentDate });
329
- const next = interval.next();
330
- if (next.getTime() - timestamp <= range) {
331
- return true;
332
- }
333
- return false;
334
- }
335
- class ScheduleTrigger extends import__.default {
336
- sync = false;
337
- static CacheRules = [
338
- ({ config, allExecuted }) => (config.limit ? allExecuted < config.limit : true) && config.startsOn,
339
- matchNext,
340
- function(workflow, now) {
341
- const { mode } = workflow.config;
342
- const modeHandlers = ScheduleModes.get(mode);
343
- if (!modeHandlers) {
344
- return false;
345
- }
346
- return modeHandlers.shouldCache.call(this, workflow, now);
347
- }
348
- ];
349
- static TriggerRules = [
350
- ({ config, allExecuted }) => (config.limit ? allExecuted < config.limit : true) && config.startsOn,
351
- function(workflow, now) {
352
- return matchNext.call(this, workflow, now, 0);
353
- }
354
- ];
355
- events = /* @__PURE__ */ new Map();
356
- timer = null;
357
- cache = /* @__PURE__ */ new Map();
358
- // running interval, default to 1s
359
- interval = 1e3;
360
- // caching workflows in range, default to 1min
361
- cacheCycle = 6e4;
362
- constructor(workflow) {
363
- super(workflow);
364
- workflow.app.on("beforeStop", () => {
365
- if (this.timer) {
366
- clearInterval(this.timer);
367
- }
368
- });
369
- }
370
- init() {
371
- var _a;
372
- if (((_a = this.workflow.app.getPlugin("multi-app-share-collection")) == null ? void 0 : _a.enabled) && this.workflow.app.name !== "main") {
373
- return;
374
- }
375
- if (this.timer) {
376
- return;
377
- }
378
- const now = /* @__PURE__ */ new Date();
379
- this.timer = setTimeout(
380
- this.run,
381
- // NOTE:
382
- // try to align to system time on each second starts,
383
- // after at least 1 second initialized for everything to get ready.
384
- // so jobs in 2 seconds will be missed at first start.
385
- 1e3 - now.getMilliseconds()
386
- );
387
- }
388
- run = () => {
389
- const now = /* @__PURE__ */ new Date();
390
- const nextInterval = 1001 - now.getMilliseconds();
391
- now.setMilliseconds(0);
392
- this.onTick(now);
393
- if (!(now.getTime() % this.cacheCycle)) {
394
- this.reload();
395
- }
396
- this.timer = setTimeout(this.run, nextInterval);
397
- };
398
- async onTick(now) {
399
- const isSqlite = this.workflow.app.db.options.dialect === "sqlite";
400
- return Array.from(this.cache.values()).reduce(
401
- (prev, workflow) => {
402
- if (!this.shouldTrigger(workflow, now)) {
403
- return prev;
404
- }
405
- if (isSqlite) {
406
- return prev.then(() => this.trigger(workflow, now));
407
- }
408
- this.trigger(workflow, now);
409
- return null;
410
- },
411
- isSqlite ? Promise.resolve() : null
412
- );
413
- }
414
- async reload() {
415
- const WorkflowRepo = this.workflow.app.db.getRepository("workflows");
416
- const workflows = await WorkflowRepo.find({
417
- filter: { enabled: true, type: "schedule" }
418
- });
419
- this.cache = /* @__PURE__ */ new Map();
420
- this.inspect(workflows);
421
- }
422
- inspect(workflows) {
423
- const now = /* @__PURE__ */ new Date();
424
- now.setMilliseconds(0);
425
- workflows.forEach(async (workflow) => {
426
- const should = await this.shouldCache(workflow, now);
427
- if (should) {
428
- this.workflow.getLogger(workflow.id).info("caching scheduled workflow will run in next minute");
429
- }
430
- this.setCache(workflow, !should);
431
- });
432
- }
433
- setCache(workflow, out = false) {
434
- out ? this.cache.delete(workflow.id) : this.cache.set(workflow.id, workflow);
435
- }
436
- async shouldCache(workflow, now) {
437
- for await (const rule of this.constructor.CacheRules) {
438
- if (!await rule.call(this, workflow, now)) {
439
- return false;
440
- }
441
- }
442
- return true;
443
- }
444
- shouldTrigger(workflow, now) {
445
- for (const rule of this.constructor.TriggerRules) {
446
- if (!rule.call(this, workflow, now)) {
447
- return false;
448
- }
449
- }
450
- return true;
451
- }
452
- async trigger(workflow, date) {
453
- const { mode } = workflow.config;
454
- const modeHandlers = ScheduleModes.get(mode);
455
- if (!modeHandlers) {
456
- return;
457
- }
458
- return modeHandlers.trigger.call(this, workflow, date);
459
- }
460
- on(workflow) {
461
- this.init();
462
- const { mode } = workflow.config;
463
- const modeHandlers = ScheduleModes.get(mode);
464
- if (modeHandlers && modeHandlers.on) {
465
- modeHandlers.on.call(this, workflow);
466
- }
467
- this.inspect([workflow]);
468
- }
469
- off(workflow) {
470
- const { mode } = workflow.config;
471
- const modeHandlers = ScheduleModes.get(mode);
472
- if (modeHandlers && modeHandlers.off) {
473
- modeHandlers.off.call(this, workflow);
474
- }
475
- this.cache.delete(workflow.id);
476
- }
477
- }
478
- // Annotate the CommonJS export names for ESM import in node:
479
- 0 && (module.exports = {
480
- SCHEDULE_MODE
481
- });