@dbos-inc/dbos-sdk 1.9.2-preview → 1.9.7-preview

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. package/dist/schemas/system_db_schema.d.ts +4 -0
  2. package/dist/schemas/system_db_schema.d.ts.map +1 -1
  3. package/dist/src/dbos-executor.d.ts.map +1 -1
  4. package/dist/src/dbos-executor.js +1 -1
  5. package/dist/src/dbos-executor.js.map +1 -1
  6. package/dist/src/dbos-runtime/cli.d.ts +1 -0
  7. package/dist/src/dbos-runtime/cli.d.ts.map +1 -1
  8. package/dist/src/dbos-runtime/cli.js +4 -2
  9. package/dist/src/dbos-runtime/cli.js.map +1 -1
  10. package/dist/src/dbos-runtime/config.d.ts.map +1 -1
  11. package/dist/src/dbos-runtime/config.js +6 -0
  12. package/dist/src/dbos-runtime/config.js.map +1 -1
  13. package/dist/src/dbos-runtime/runtime.d.ts +1 -0
  14. package/dist/src/dbos-runtime/runtime.d.ts.map +1 -1
  15. package/dist/src/dbos-runtime/runtime.js +7 -0
  16. package/dist/src/dbos-runtime/runtime.js.map +1 -1
  17. package/dist/src/decorators.d.ts +0 -2
  18. package/dist/src/decorators.d.ts.map +1 -1
  19. package/dist/src/decorators.js +1 -11
  20. package/dist/src/decorators.js.map +1 -1
  21. package/dist/src/index.d.ts +2 -1
  22. package/dist/src/index.d.ts.map +1 -1
  23. package/dist/src/index.js +5 -2
  24. package/dist/src/index.js.map +1 -1
  25. package/dist/src/kafka/kafka.d.ts +3 -2
  26. package/dist/src/kafka/kafka.d.ts.map +1 -1
  27. package/dist/src/kafka/kafka.js +35 -1
  28. package/dist/src/kafka/kafka.js.map +1 -1
  29. package/dist/src/scheduler/crontab.d.ts +19 -0
  30. package/dist/src/scheduler/crontab.d.ts.map +1 -0
  31. package/dist/src/scheduler/crontab.js +286 -0
  32. package/dist/src/scheduler/crontab.js.map +1 -0
  33. package/dist/src/scheduler/scheduler.d.ts +45 -0
  34. package/dist/src/scheduler/scheduler.d.ts.map +1 -0
  35. package/dist/src/scheduler/scheduler.js +165 -0
  36. package/dist/src/scheduler/scheduler.js.map +1 -0
  37. package/dist/src/system_database.d.ts +4 -0
  38. package/dist/src/system_database.d.ts.map +1 -1
  39. package/dist/src/system_database.js +22 -0
  40. package/dist/src/system_database.js.map +1 -1
  41. package/dist/src/testing/testing_runtime.d.ts.map +1 -1
  42. package/dist/src/testing/testing_runtime.js +5 -0
  43. package/dist/src/testing/testing_runtime.js.map +1 -1
  44. package/dist/src/workflow.d.ts.map +1 -1
  45. package/dist/src/workflow.js.map +1 -1
  46. package/dist/tsconfig.build.tsbuildinfo +1 -1
  47. package/migrations/20240430090000_tables.js +13 -0
  48. package/package.json +1 -1
@@ -4,6 +4,7 @@ exports.DBOSKafka = exports.Kafka = exports.KafkaClassRegistration = exports.Kaf
4
4
  const kafkajs_1 = require("kafkajs");
5
5
  const decorators_1 = require("../decorators");
6
6
  const error_1 = require("../error");
7
+ const utils_1 = require("../utils");
7
8
  /////////////////////////////
8
9
  /* Kafka Method Decorators */
9
10
  /////////////////////////////
@@ -65,7 +66,29 @@ class DBOSKafka {
65
66
  const consumerConfig = ro.consumerConfig ?? { groupId: `dbos-kafka-group-${ro.kafkaTopic}` };
66
67
  const consumer = kafka.consumer(consumerConfig);
67
68
  await consumer.connect();
68
- await consumer.subscribe({ topic: ro.kafkaTopic, fromBeginning: true });
69
+ // A temporary workaround for https://github.com/tulios/kafkajs/pull/1558 until it gets fixed
70
+ // If topic autocreation is on and you try to subscribe to a nonexistent topic, KafkaJS should retry until the topic is created.
71
+ // However, it has a bug where it won't. Thus, we retry instead.
72
+ const maxRetries = defaults.kafkaConfig.retry ? defaults.kafkaConfig.retry.retries ?? 5 : 5;
73
+ let retryTime = defaults.kafkaConfig.retry ? defaults.kafkaConfig.retry.maxRetryTime ?? 300 : 300;
74
+ const multiplier = defaults.kafkaConfig.retry ? defaults.kafkaConfig.retry.multiplier ?? 2 : 2;
75
+ for (let i = 0; i < maxRetries; i++) {
76
+ try {
77
+ await consumer.subscribe({ topic: ro.kafkaTopic, fromBeginning: true });
78
+ break;
79
+ }
80
+ catch (error) {
81
+ const e = error;
82
+ if (e.code === 3 && i + 1 < maxRetries) { // UNKNOWN_TOPIC_OR_PARTITION
83
+ await (0, utils_1.sleep)(retryTime);
84
+ retryTime *= multiplier;
85
+ continue;
86
+ }
87
+ else {
88
+ throw e;
89
+ }
90
+ }
91
+ }
69
92
  await consumer.run({
70
93
  eachMessage: async ({ topic, partition, message }) => {
71
94
  // This combination uniquely identifies a message for a given Kafka cluster
@@ -93,6 +116,17 @@ class DBOSKafka {
93
116
  await consumer.disconnect();
94
117
  }
95
118
  }
119
+ logRegisteredKafkaEndpoints() {
120
+ const logger = this.dbosExec.logger;
121
+ logger.info("Kafka endpoints supported:");
122
+ this.dbosExec.registeredOperations.forEach((registeredOperation) => {
123
+ const ro = registeredOperation;
124
+ if (ro.kafkaTopic) {
125
+ const defaults = ro.defaults;
126
+ logger.info(` ${ro.kafkaTopic} -> ${defaults.name}.${ro.name}`);
127
+ }
128
+ });
129
+ }
96
130
  }
97
131
  exports.DBOSKafka = DBOSKafka;
98
132
  //# sourceMappingURL=kafka.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"kafka.js","sourceRoot":"","sources":["../../../src/kafka/kafka.ts"],"names":[],"mappings":";;;AAAA,qCAAgG;AAEhG,8CAAmJ;AAInJ,oCAAqC;AAIrC,6BAA6B;AAC7B,6BAA6B;AAC7B,6BAA6B;AAE7B,MAAa,iBAAwD,SAAQ,+BAAsC;IACjH,UAAU,CAAU;IACpB,cAAc,CAAkB;IAEhC,YAAY,QAAwD;QAClE,KAAK,CAAC,QAAQ,CAAC,CAAC;IAClB,CAAC;CACF;AAPD,8CAOC;AAED,SAAgB,YAAY,CAAC,KAAa,EAAE,cAA+B;IACzE,SAAS,QAAQ,CACf,MAAc,EACd,WAAmB,EACnB,YAAoG;QAEpG,MAAM,EAAE,UAAU,EAAE,YAAY,EAAE,GAAG,IAAA,oCAAuB,EAAC,MAAM,EAAE,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AAC,MAAM,CAAC;QACpC,MAAM,CAAC,IAAI,CAAC,4BAA4B,CAAC,CAAC;QAC1C,IAAI,CAAC,QAAQ,CAAC,oBAAoB,CAAC,OAAO,CAAC,CAAC,mBAAmB,EAAE,EAAE;YACjE,MAAM,EAAE,GAAG,mBAAqE,CAAC;YACjF,IAAI,EAAE,CAAC,UAAU,EAAE,CAAC;gBAClB,MAAM,QAAQ,GAAG,EAAE,CAAC,QAAyB,CAAC;gBAC9C,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,UAAU,OAAO,QAAQ,CAAC,IAAI,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC;YACrE,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAhFD,8BAgFC"}
@@ -0,0 +1,19 @@
1
+ export declare function convertAsterisksToRanges(expressions: string[]): string[];
2
+ /**
3
+ * Validates a Cron-Job expression pattern.
4
+ * Throws on error.
5
+ */
6
+ export declare function validateCrontab(pattern: string): string;
7
+ export declare function convertExpression(crontab: string): string;
8
+ export declare class TimeMatcher {
9
+ pattern: string;
10
+ expressions: string[];
11
+ dtf: Intl.DateTimeFormat | null;
12
+ timezone?: string;
13
+ constructor(pattern: string, timezone?: string);
14
+ match(date: Date): boolean;
15
+ private runsThisDay;
16
+ nextWakeupTime(date: Date): Date;
17
+ apply(date: Date): Date;
18
+ }
19
+ //# sourceMappingURL=crontab.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crontab.d.ts","sourceRoot":"","sources":["../../../src/scheduler/crontab.ts"],"names":[],"mappings":"AA0EA,wBAAgB,wBAAwB,CAAC,WAAW,EAAE,MAAM,EAAE,YAQ7D;AAyHD;;;GAGG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,UAY9C;AAmBD,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,UAYhD;AAeD,qBAAa,WAAW;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,GAAG,EAAE,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC;IAChC,QAAQ,CAAC,EAAE,MAAM,CAAC;gBAEN,OAAO,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM;IAmB9C,KAAK,CAAC,IAAI,EAAE,IAAI;IAWhB,OAAO,CAAC,WAAW;IAQnB,cAAc,CAAC,IAAI,EAAE,IAAI;IAYzB,KAAK,CAAC,IAAI,EAAE,IAAI;CAOnB"}
@@ -0,0 +1,286 @@
1
+ "use strict";
2
+ // This code was based on code from node-cron:
3
+ // https://github.com/node-cron/node-cron
4
+ /*
5
+ ISC License
6
+ Copyright (c) 2016, Lucas Merencia <lucas.merencia@gmail.com>
7
+
8
+ Permission to use, copy, modify, and/or distribute this software for any
9
+ purpose with or without fee is hereby granted, provided that the above
10
+ copyright notice and this permission notice appear in all copies.
11
+
12
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
+ */
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.TimeMatcher = exports.convertExpression = exports.validateCrontab = exports.convertAsterisksToRanges = void 0;
22
+ function removeExtraSpaces(str) {
23
+ return str.replace(/\s{2,}/g, ' ').trim();
24
+ }
25
+ function prependSecondExpression(expressions) {
26
+ if (expressions.length === 5) {
27
+ return ['0'].concat(expressions);
28
+ }
29
+ return expressions;
30
+ }
31
+ const months = ['january', 'february', 'march', 'april', 'may', 'june', 'july',
32
+ 'august', 'september', 'october', 'november', 'december'];
33
+ const shortMonths = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
34
+ 'sep', 'oct', 'nov', 'dec'];
35
+ function convertMonthNameI(expression, items) {
36
+ for (let i = 0; i < items.length; i++) {
37
+ expression = expression.replace(new RegExp(items[i], 'gi'), `${i + 1}`);
38
+ }
39
+ return expression;
40
+ }
41
+ function monthNamesConversion(monthExpression) {
42
+ monthExpression = convertMonthNameI(monthExpression, months);
43
+ monthExpression = convertMonthNameI(monthExpression, shortMonths);
44
+ return monthExpression;
45
+ }
46
+ const weekDays = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
47
+ 'friday', 'saturday'];
48
+ const shortWeekDays = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
49
+ function convertWeekDayName(weekExpression, items) {
50
+ for (let i = 0; i < items.length; i++) {
51
+ weekExpression = weekExpression.replace(new RegExp(items[i], 'gi'), `${i}`);
52
+ }
53
+ return weekExpression;
54
+ }
55
+ function weekDayNamesConversion(expression) {
56
+ expression = expression.replace('7', '0');
57
+ expression = convertWeekDayName(expression, weekDays);
58
+ return convertWeekDayName(expression, shortWeekDays);
59
+ }
60
+ function convertAsterisk(expression, replacement) {
61
+ if (expression.indexOf('*') !== -1) {
62
+ return expression.replace('*', replacement);
63
+ }
64
+ return expression;
65
+ }
66
+ // Based on position in full crontab, convert asterisk to appropriate range
67
+ function convertAsterisksToRanges(expressions) {
68
+ expressions[0] = convertAsterisk(expressions[0], '0-59');
69
+ expressions[1] = convertAsterisk(expressions[1], '0-59');
70
+ expressions[2] = convertAsterisk(expressions[2], '0-23');
71
+ expressions[3] = convertAsterisk(expressions[3], '1-31');
72
+ expressions[4] = convertAsterisk(expressions[4], '1-12');
73
+ expressions[5] = convertAsterisk(expressions[5], '0-6');
74
+ return expressions;
75
+ }
76
+ exports.convertAsterisksToRanges = convertAsterisksToRanges;
77
+ function replaceWithRange(expression, text, init, end) {
78
+ const numbers = [];
79
+ let last = parseInt(end);
80
+ let first = parseInt(init);
81
+ if (first > last) {
82
+ last = parseInt(init);
83
+ first = parseInt(end);
84
+ }
85
+ for (let i = first; i <= last; i++) {
86
+ numbers.push(i.toString());
87
+ }
88
+ return expression.replace(new RegExp(text, 'i'), numbers.join());
89
+ }
90
+ function convertRange(expression) {
91
+ const rangeRegEx = /(\d+)-(\d+)/;
92
+ let match = rangeRegEx.exec(expression);
93
+ while (match !== null && match.length > 0) {
94
+ expression = replaceWithRange(expression, match[0], match[1], match[2]);
95
+ match = rangeRegEx.exec(expression);
96
+ }
97
+ return expression;
98
+ }
99
+ function convertAllRanges(expressions) {
100
+ for (let i = 0; i < expressions.length; i++) {
101
+ expressions[i] = convertRange(expressions[i]);
102
+ }
103
+ return expressions;
104
+ }
105
+ function convertSteps(expressions) {
106
+ const stepValuePattern = /^(.+)\/(\w+)$/;
107
+ for (let i = 0; i < expressions.length; i++) {
108
+ const match = stepValuePattern.exec(expressions[i]);
109
+ const isStepValue = match !== null && match.length > 0;
110
+ if (isStepValue) {
111
+ const baseDivider = match[2];
112
+ if (isNaN(parseInt(baseDivider))) {
113
+ throw new Error(baseDivider + ' is not a valid step value');
114
+ }
115
+ const values = match[1].split(',');
116
+ const stepValues = [];
117
+ const divider = parseInt(baseDivider, 10);
118
+ for (let j = 0; j <= values.length; j++) {
119
+ const value = parseInt(values[j], 10);
120
+ if (value % divider === 0) {
121
+ stepValues.push(`${value}`);
122
+ }
123
+ }
124
+ expressions[i] = stepValues.join(',');
125
+ }
126
+ }
127
+ return expressions;
128
+ }
129
+ // Function that takes care of normalization.
130
+ function normalizeIntegers(expressions) {
131
+ for (let i = 0; i < expressions.length; i++) {
132
+ const numbers = expressions[i].split(',');
133
+ for (let j = 0; j < numbers.length; j++) {
134
+ numbers[j] = parseInt(numbers[j]).toString();
135
+ }
136
+ expressions[i] = numbers.join(',');
137
+ }
138
+ return expressions;
139
+ }
140
+ const validationRegex = /^(?:\d+|\*|\*\/\d+)$/;
141
+ // Check comma-delimited list to see if elements are in range
142
+ function isValidExpression(expression, min, max) {
143
+ const options = expression.split(',');
144
+ for (const option of options) {
145
+ const optionAsInt = parseInt(option, 10);
146
+ if ((!Number.isNaN(optionAsInt) &&
147
+ (optionAsInt < min || optionAsInt > max)) ||
148
+ !validationRegex.test(option)) {
149
+ return false;
150
+ }
151
+ }
152
+ return true;
153
+ }
154
+ function isInvalidSecond(expression) { return !isValidExpression(expression, 0, 59); }
155
+ function isInvalidMinute(expression) { return !isValidExpression(expression, 0, 59); }
156
+ function isInvalidHour(expression) { return !isValidExpression(expression, 0, 23); }
157
+ function isInvalidDayOfMonth(expression) { return !isValidExpression(expression, 1, 31); }
158
+ function isInvalidMonth(expression) { return !isValidExpression(expression, 1, 12); }
159
+ function isInvalidWeekDay(expression) { return !isValidExpression(expression, 0, 7); }
160
+ function validateFields(patterns, executablePatterns) {
161
+ if (isInvalidSecond(executablePatterns[0]))
162
+ throw new Error(`${patterns[0]} is a invalid expression for second`);
163
+ if (isInvalidMinute(executablePatterns[1]))
164
+ throw new Error(`${patterns[1]} is a invalid expression for minute`);
165
+ if (isInvalidHour(executablePatterns[2]))
166
+ throw new Error(`${patterns[2]} is a invalid expression for hour`);
167
+ if (isInvalidDayOfMonth(executablePatterns[3]))
168
+ throw new Error(`${patterns[3]} is a invalid expression for day of month`);
169
+ if (isInvalidMonth(executablePatterns[4]))
170
+ throw new Error(`${patterns[4]} is a invalid expression for month`);
171
+ if (isInvalidWeekDay(executablePatterns[5]))
172
+ throw new Error(`${patterns[5]} is a invalid expression for week day`);
173
+ }
174
+ /**
175
+ * Validates a Cron-Job expression pattern.
176
+ * Throws on error.
177
+ */
178
+ function validateCrontab(pattern) {
179
+ if (typeof pattern !== 'string')
180
+ throw new TypeError('pattern must be a string!');
181
+ const patterns = pattern.split(' ');
182
+ const executablePatterns = convertExpression(pattern).split(' ');
183
+ if (patterns.length === 5)
184
+ patterns.unshift('0');
185
+ validateFields(patterns, executablePatterns);
186
+ return executablePatterns.join(' ');
187
+ }
188
+ exports.validateCrontab = validateCrontab;
189
+ /*
190
+ * The node-cron core allows only numbers (including multiple numbers e.g 1,2).
191
+ * This module is going to translate the month names, week day names and ranges
192
+ * to integers relatives.
193
+ *
194
+ * Month names example:
195
+ * - expression 0 1 1 January,Sep *
196
+ * - Will be translated to 0 1 1 1,9 *
197
+ *
198
+ * Week day names example:
199
+ * - expression 0 1 1 2 Monday,Sat
200
+ * - Will be translated to 0 1 1 1,5 *
201
+ *
202
+ * Ranges example:
203
+ * - expression 1-5 * * * *
204
+ * - Will be translated to 1,2,3,4,5 * * * *
205
+ */
206
+ function convertExpression(crontab) {
207
+ let expressions = removeExtraSpaces(crontab).split(' ');
208
+ expressions = prependSecondExpression(expressions);
209
+ expressions[4] = monthNamesConversion(expressions[4]);
210
+ expressions[5] = weekDayNamesConversion(expressions[5]);
211
+ expressions = convertAsterisksToRanges(expressions);
212
+ expressions = convertAllRanges(expressions);
213
+ expressions = convertSteps(expressions);
214
+ expressions = normalizeIntegers(expressions);
215
+ return expressions.join(' ');
216
+ }
217
+ exports.convertExpression = convertExpression;
218
+ //////////
219
+ /// Time matcher
220
+ //////////
221
+ function matchPattern(pattern, nvalue) {
222
+ const value = `${nvalue}`;
223
+ if (pattern.indexOf(',') !== -1) {
224
+ const patterns = pattern.split(',');
225
+ return patterns.includes(value);
226
+ }
227
+ return pattern === value;
228
+ }
229
+ class TimeMatcher {
230
+ pattern;
231
+ expressions;
232
+ dtf;
233
+ timezone;
234
+ constructor(pattern, timezone) {
235
+ validateCrontab(pattern);
236
+ this.pattern = convertExpression(pattern);
237
+ this.timezone = timezone;
238
+ this.expressions = this.pattern.split(' ');
239
+ this.dtf = this.timezone
240
+ ? new Intl.DateTimeFormat('en-US', {
241
+ year: 'numeric',
242
+ month: '2-digit',
243
+ day: '2-digit',
244
+ hour: '2-digit',
245
+ minute: '2-digit',
246
+ second: '2-digit',
247
+ hourCycle: 'h23',
248
+ fractionalSecondDigits: 3,
249
+ timeZone: this.timezone
250
+ }) : null;
251
+ }
252
+ match(date) {
253
+ date = this.apply(date);
254
+ const runOnSecond = matchPattern(this.expressions[0], date.getSeconds());
255
+ const runOnMinute = matchPattern(this.expressions[1], date.getMinutes());
256
+ const runOnHour = matchPattern(this.expressions[2], date.getHours());
257
+ const runOnDay = this.runsThisDay(date);
258
+ return runOnSecond && runOnMinute && runOnHour && runOnDay;
259
+ }
260
+ runsThisDay(date) {
261
+ const runOnDay = matchPattern(this.expressions[3], date.getDate());
262
+ const runOnMonth = matchPattern(this.expressions[4], date.getMonth() + 1);
263
+ const runOnWeekDay = matchPattern(this.expressions[5], date.getDay());
264
+ return runOnDay && runOnMonth && runOnWeekDay;
265
+ }
266
+ nextWakeupTime(date) {
267
+ // This is conservative. Some schedules never occur, such as the 30th of February, but you can ask for them
268
+ let msec = Math.round(date.getTime());
269
+ // This can be optimized by skipping ahead, but unit test first
270
+ for (let maxIters = 3600; --maxIters; maxIters > 0) {
271
+ msec += 1000;
272
+ const nd = new Date(msec);
273
+ if (this.match(nd))
274
+ return nd;
275
+ }
276
+ return new Date(msec);
277
+ }
278
+ apply(date) {
279
+ if (this.dtf) {
280
+ return new Date(this.dtf.format(date));
281
+ }
282
+ return date;
283
+ }
284
+ }
285
+ exports.TimeMatcher = TimeMatcher;
286
+ //# sourceMappingURL=crontab.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crontab.js","sourceRoot":"","sources":["../../../src/scheduler/crontab.ts"],"names":[],"mappings":";AAAA,8CAA8C;AAC9C,2CAA2C;AAC3C;;;;;;;;;;;;;;;EAeE;;;AAEF,SAAS,iBAAiB,CAAC,GAAW;IAClC,OAAO,GAAG,CAAC,OAAO,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;AAC9C,CAAC;AAED,SAAS,uBAAuB,CAAC,WAAqB;IA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ators";
4
+ export declare enum SchedulerMode {
5
+ ExactlyOncePerInterval = "ExactlyOncePerInterval",
6
+ ExactlyOncePerIntervalWhenActive = "ExactlyOncePerIntervalWhenActive"
7
+ }
8
+ export declare class SchedulerConfig {
9
+ crontab: string;
10
+ mode?: SchedulerMode;
11
+ }
12
+ export type ScheduledArgs = [Date, Date];
13
+ export interface SchedulerRegistrationConfig {
14
+ schedulerConfig?: SchedulerConfig;
15
+ }
16
+ export declare class SchedulerRegistration<This, Args extends unknown[], Return> extends MethodRegistration<This, Args, Return> implements SchedulerRegistrationConfig {
17
+ schedulerConfig?: SchedulerConfig;
18
+ constructor(origFunc: (this: This, ...args: Args) => Promise<Return>);
19
+ }
20
+ export declare function Scheduled(schedulerConfig: SchedulerConfig): <This, Ctx extends WorkflowContext, Return>(target: object, propertyKey: string, inDescriptor: TypedPropertyDescriptor<(this: This, ctx: Ctx, args_0: Date, args_1: Date) => Promise<Return>>) => TypedPropertyDescriptor<(this: This, ctx: Ctx, args_1: Date, args_2: Date) => Promise<Return>>;
21
+ export declare class DBOSScheduler {
22
+ readonly dbosExec: DBOSExecutor;
23
+ constructor(dbosExec: DBOSExecutor);
24
+ schedLoops: DetachableLoop[];
25
+ schedTasks: Promise<void>[];
26
+ initScheduler(): void;
27
+ destroyScheduler(): Promise<void>;
28
+ logRegisteredSchedulerEndpoints(): void;
29
+ }
30
+ declare class DetachableLoop {
31
+ readonly dbosExec: DBOSExecutor;
32
+ readonly crontab: string;
33
+ readonly schedMode: SchedulerMode;
34
+ readonly scheduledMethod: SchedulerRegistration<unknown, unknown[], unknown>;
35
+ private isRunning;
36
+ private interruptResolve?;
37
+ private lastExec;
38
+ private timeMatcher;
39
+ constructor(dbosExec: DBOSExecutor, crontab: string, schedMode: SchedulerMode, scheduledMethod: SchedulerRegistration<unknown, unknown[], unknown>);
40
+ startLoop(): Promise<void>;
41
+ setStopLoopFlag(): void;
42
+ private sleepms;
43
+ }
44
+ export {};
45
+ //# sourceMappingURL=scheduler.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AC,OAAO,EAAE,MAAM;IAAE,QAAQ,CAAC,SAAS,EAAC,aAAa;IAC3F,QAAQ,CAAC,eAAe,EAAE,qBAAqB,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,OAAO,CAAC;IANxF,OAAO,CAAC,SAAS,CAAkB;IACnC,OAAO,CAAC,gBAAgB,CAAC,CAAa;IACtC,OAAO,CAAC,QAAQ,CAAO;IACvB,OAAO,CAAC,WAAW,CAAc;gBAEZ,QAAQ,EAAE,YAAY,EAAW,OAAO,EAAE,MAAM,EAAW,SAAS,EAAC,aAAa,EAClF,eAAe,EAAE,qBAAqB,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,OAAO,CAAC;IAOlF,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC;IA8DhC,eAAe;IAQf,OAAO,CAAC,OAAO;CAGlB"}
@@ -0,0 +1,165 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DBOSScheduler = exports.Scheduled = exports.SchedulerRegistration = exports.SchedulerConfig = exports.SchedulerMode = void 0;
4
+ const decorators_1 = require("../decorators");
5
+ const crontab_1 = require("./crontab");
6
+ ////
7
+ // Configuration
8
+ ////
9
+ var SchedulerMode;
10
+ (function (SchedulerMode) {
11
+ SchedulerMode["ExactlyOncePerInterval"] = "ExactlyOncePerInterval";
12
+ SchedulerMode["ExactlyOncePerIntervalWhenActive"] = "ExactlyOncePerIntervalWhenActive";
13
+ })(SchedulerMode || (exports.SchedulerMode = SchedulerMode = {}));
14
+ class SchedulerConfig {
15
+ crontab = '* * * * *'; // Every minute
16
+ mode = SchedulerMode.ExactlyOncePerInterval;
17
+ }
18
+ exports.SchedulerConfig = SchedulerConfig;
19
+ class SchedulerRegistration extends decorators_1.MethodRegistration {
20
+ schedulerConfig;
21
+ constructor(origFunc) {
22
+ super(origFunc);
23
+ }
24
+ }
25
+ exports.SchedulerRegistration = SchedulerRegistration;
26
+ function Scheduled(schedulerConfig) {
27
+ function scheddec(target, propertyKey, inDescriptor) {
28
+ const { descriptor, registration } = (0, decorators_1.registerAndWrapFunction)(target, propertyKey, inDescriptor);
29
+ const schedRegistration = registration;
30
+ schedRegistration.schedulerConfig = schedulerConfig;
31
+ return descriptor;
32
+ }
33
+ return scheddec;
34
+ }
35
+ exports.Scheduled = Scheduled;
36
+ ///////////////////////////
37
+ // Scheduler Management
38
+ ///////////////////////////
39
+ class DBOSScheduler {
40
+ dbosExec;
41
+ constructor(dbosExec) {
42
+ this.dbosExec = dbosExec;
43
+ }
44
+ schedLoops = [];
45
+ schedTasks = [];
46
+ initScheduler() {
47
+ for (const registeredOperation of this.dbosExec.registeredOperations) {
48
+ const ro = registeredOperation;
49
+ if (ro.schedulerConfig) {
50
+ const loop = new DetachableLoop(this.dbosExec, ro.schedulerConfig.crontab ?? '* * * * *', ro.schedulerConfig.mode ?? SchedulerMode.ExactlyOncePerInterval, ro);
51
+ this.schedLoops.push(loop);
52
+ this.schedTasks.push(loop.startLoop());
53
+ }
54
+ }
55
+ }
56
+ async destroyScheduler() {
57
+ for (const l of this.schedLoops) {
58
+ l.setStopLoopFlag();
59
+ }
60
+ this.schedLoops = [];
61
+ try {
62
+ await Promise.allSettled(this.schedTasks);
63
+ }
64
+ catch (e) {
65
+ // What gets caught here is the loop stopping, which is what we wanted.
66
+ }
67
+ this.schedTasks = [];
68
+ }
69
+ logRegisteredSchedulerEndpoints() {
70
+ const logger = this.dbosExec.logger;
71
+ logger.info("Scheduled endpoints:");
72
+ this.dbosExec.registeredOperations.forEach((registeredOperation) => {
73
+ const ro = registeredOperation;
74
+ if (ro.schedulerConfig) {
75
+ logger.info(` ${ro.name} @ ${ro.schedulerConfig.crontab}; ${ro.schedulerConfig.mode ?? SchedulerMode.ExactlyOncePerInterval}`);
76
+ }
77
+ });
78
+ }
79
+ }
80
+ exports.DBOSScheduler = DBOSScheduler;
81
+ class DetachableLoop {
82
+ dbosExec;
83
+ crontab;
84
+ schedMode;
85
+ scheduledMethod;
86
+ isRunning = false;
87
+ interruptResolve;
88
+ lastExec;
89
+ timeMatcher;
90
+ constructor(dbosExec, crontab, schedMode, scheduledMethod) {
91
+ this.dbosExec = dbosExec;
92
+ this.crontab = crontab;
93
+ this.schedMode = schedMode;
94
+ this.scheduledMethod = scheduledMethod;
95
+ this.lastExec = new Date();
96
+ this.lastExec.setMilliseconds(0);
97
+ this.timeMatcher = new crontab_1.TimeMatcher(crontab);
98
+ }
99
+ async startLoop() {
100
+ // See if the exec time is available in durable storage...
101
+ if (this.schedMode !== SchedulerMode.ExactlyOncePerInterval) {
102
+ const lasttm = await this.dbosExec.systemDatabase.getLastScheduledTime(this.scheduledMethod.name);
103
+ if (lasttm) {
104
+ this.lastExec = new Date(lasttm);
105
+ }
106
+ }
107
+ this.isRunning = true;
108
+ while (this.isRunning) {
109
+ const nextExecTime = this.timeMatcher.nextWakeupTime(this.lastExec);
110
+ const sleepTime = nextExecTime.getTime() - new Date().getTime();
111
+ if (sleepTime > 0) {
112
+ // Wait for either the timeout or an interruption
113
+ await Promise.race([
114
+ this.sleepms(sleepTime),
115
+ new Promise((_, reject) => this.interruptResolve = reject)
116
+ ])
117
+ .catch(); // Interrupt sleep throws
118
+ }
119
+ if (!this.isRunning) {
120
+ break;
121
+ }
122
+ // Check crontab
123
+ // If this "wake up" time is not on the schedule, we shouldn't execute.
124
+ // (While ATOW this wake-up time is a scheduled run time, it is not
125
+ // contractually obligated to be so. If this is obligated to be a
126
+ // scheduled execution time, then we could make this an assertion
127
+ // instead of a check.)
128
+ if (!this.timeMatcher.match(nextExecTime)) {
129
+ this.lastExec = nextExecTime;
130
+ continue;
131
+ }
132
+ // Init workflow
133
+ const workflowUUID = `sched-${this.scheduledMethod.name}-${nextExecTime.toISOString()}`;
134
+ this.dbosExec.logger.debug(`Executing scheduled workflow ${workflowUUID}`);
135
+ const wfParams = { workflowUUID: workflowUUID };
136
+ // All operations annotated with Scheduled decorators must take in these four
137
+ const args = [nextExecTime, new Date()];
138
+ // We currently only support scheduled workflows
139
+ if (this.scheduledMethod.workflowConfig) {
140
+ // Execute the workflow
141
+ await this.dbosExec.workflow(this.scheduledMethod.registeredFunction, wfParams, ...args);
142
+ }
143
+ else {
144
+ this.dbosExec.logger.error(`Function ${this.scheduledMethod.name} is @scheduled but not a workflow`);
145
+ }
146
+ // Record the time of the wf kicked off
147
+ const dbTime = await this.dbosExec.systemDatabase.setLastScheduledTime(this.scheduledMethod.name, nextExecTime.getTime());
148
+ if (dbTime && dbTime > nextExecTime.getTime())
149
+ nextExecTime.setTime(dbTime);
150
+ this.lastExec = nextExecTime;
151
+ }
152
+ }
153
+ setStopLoopFlag() {
154
+ if (!this.isRunning)
155
+ return;
156
+ this.isRunning = false;
157
+ if (this.interruptResolve) {
158
+ this.interruptResolve(); // Trigger the interruption
159
+ }
160
+ }
161
+ sleepms(ms) {
162
+ return new Promise(resolve => setTimeout(resolve, ms));
163
+ }
164
+ }
165
+ //# sourceMappingURL=scheduler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scheduler.js","sourceRoot":"","sources":["../../../src/scheduler/scheduler.ts"],"names":[],"mappings":";;;AAEA,8CAA4E;AAC5E,uCAAwC;AAGxC,IAAI;AACJ,gBAAgB;AAChB,IAAI;AAEJ,IAAY,aAGX;AAHD,WAAY,aAAa;IACrB,kEAAiD,CAAA;IACjD,sFAAqE,CAAA;AACzE,CAAC,EAHW,aAAa,6BAAb,aAAa,QAGxB;AAED,MAAa,eAAe;IACxB,OAAO,GAAW,WAAW,CAAC,CAAC,eAAe;IAC9C,IAAI,GAAoB,aAAa,CAAC,sBAAsB,CAAC;CAChE;AAHD,0CAGC;AAaD,MAAa,qBAA4D,SAAQ,+BAAsC;IAGnH,eAAe,CAAmB;IAClC,YAAY,QAAwD;QAChE,KAAK,CAAC,QAAQ,CAAC,CAAC;IACpB,CAAC;CACJ;AAPD,sDAOC;AAED,SAAgB,SAAS,CAAC,eAAgC;IACtD,SAAS,QAAQ,CACb,MAAc,EACd,WAAmB,EACnB,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E,CAAC,CAAC,2BAA2B;QACxD,CAAC;IACL,CAAC;IAEO,OAAO,CAAC,EAAU;QACtB,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;IAC3D,CAAC;CACJ"}
@@ -24,6 +24,8 @@ export interface SystemDatabase {
24
24
  recv<T extends NonNullable<any>>(workflowUUID: string, functionID: number, topic?: string, timeoutSeconds?: number): Promise<T | null>;
25
25
  setEvent<T extends NonNullable<any>>(workflowUUID: string, functionID: number, key: string, value: T): Promise<void>;
26
26
  getEvent<T extends NonNullable<any>>(workflowUUID: string, key: string, timeoutSeconds: number, callerUUID?: string, functionID?: number): Promise<T | null>;
27
+ getLastScheduledTime(wfn: string): Promise<number | null>;
28
+ setLastScheduledTime(wfn: string, invtime: number): Promise<number | null>;
27
29
  }
28
30
  export interface WorkflowStatusInternal {
29
31
  workflowUUID: string;
@@ -91,5 +93,7 @@ export declare class PostgresSystemDatabase implements SystemDatabase {
91
93
  * workflow listener by resolving its promise.
92
94
  */
93
95
  listenForNotifications(): Promise<void>;
96
+ getLastScheduledTime(wfn: string): Promise<number | null>;
97
+ setLastScheduledTime(wfn: string, invtime: number): Promise<number | null>;
94
98
  }
95
99
  //# sourceMappingURL=system_database.d.ts.map