@dbos-inc/dbos-sdk 1.9.7-preview → 1.9.10-preview.g0dc217a304
Sign up to get free protection for your applications and to get access to all the features.
@@ -4,11 +4,11 @@ import { ClassRegistration, MethodRegistration, RegistrationDefaults } from "../
|
|
4
4
|
import { DBOSExecutor } from "../dbos-executor";
|
5
5
|
type KafkaArgs = [string, number, KafkaMessage];
|
6
6
|
export declare class KafkaRegistration<This, Args extends unknown[], Return> extends MethodRegistration<This, Args, Return> {
|
7
|
-
|
7
|
+
kafkaTopics?: string | RegExp | Array<string | RegExp>;
|
8
8
|
consumerConfig?: ConsumerConfig;
|
9
9
|
constructor(origFunc: (this: This, ...args: Args) => Promise<Return>);
|
10
10
|
}
|
11
|
-
export declare function KafkaConsume(
|
11
|
+
export declare function KafkaConsume(topics: string | RegExp | Array<string | RegExp>, consumerConfig?: ConsumerConfig): <This, Ctx extends DBOSContext, Return>(target: object, propertyKey: string, inDescriptor: TypedPropertyDescriptor<(this: This, ctx: Ctx, args_0: string, args_1: number, args_2: KafkaMessage) => Promise<Return>>) => TypedPropertyDescriptor<(this: This, ctx: Ctx, args_1: string, args_2: number, args_3: KafkaMessage) => Promise<Return>>;
|
12
12
|
export interface KafkaDefaults extends RegistrationDefaults {
|
13
13
|
kafkaConfig?: KafkaConfig;
|
14
14
|
}
|
@@ -25,6 +25,7 @@ export declare class DBOSKafka {
|
|
25
25
|
constructor(dbosExec: DBOSExecutor);
|
26
26
|
initKafka(): Promise<void>;
|
27
27
|
destroyKafka(): Promise<void>;
|
28
|
+
safeGroupName(topics: Array<string | RegExp>): string;
|
28
29
|
logRegisteredKafkaEndpoints(): void;
|
29
30
|
}
|
30
31
|
export {};
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"kafka.d.ts","sourceRoot":"","sources":["../../../src/kafka/kafka.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,QAAQ,EAAE,cAAc,EAAE,WAAW,EAAE,YAAY,EAAwB,MAAM,SAAS,CAAC;AACtH,OAAO,EAAE,WA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G,MAAM,GAAG,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,cAAc,CAAC,EAAE,cAAc,mDAElG,MAAM,eACD,MAAM,sQAWtB;AAMD,MAAM,WAAW,aAAc,SAAQ,oBAAoB;IACzD,WAAW,CAAC,EAAE,WAAW,CAAC;CAC3B;AAED,qBAAa,sBAAsB,CAAC,EAAE,SAAS;IAAE,KAAI,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,MAAM,CAAA;CAAE,CAAE,SAAQ,iBAAiB,CAAC,EAAE,CAAE,YAAW,aAAa;IACxI,WAAW,CAAC,EAAE,WAAW,CAAC;gBAEd,IAAI,EAAE,EAAE;CAGrB;AAED,wBAAgB,KAAK,CAAC,WAAW,EAAE,WAAW,4BACH,OAAO,EAAE,KAAG,MAAM,mBAK5D;AAMD,qBAAa,SAAS;IAGR,QAAQ,CAAC,QAAQ,EAAE,YAAY;IAF3C,QAAQ,CAAC,SAAS,EAAE,QAAQ,EAAE,CAAM;gBAEf,QAAQ,EAAE,YAAY;IAErC,SAAS;IAiET,YAAY;IAMlB,aAAa,CAAC,MAAM,EAAE,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC;IAQ5C,2BAA2B;CAiB5B"}
|
package/dist/src/kafka/kafka.js
CHANGED
@@ -9,18 +9,18 @@ const utils_1 = require("../utils");
|
|
9
9
|
/* Kafka Method Decorators */
|
10
10
|
/////////////////////////////
|
11
11
|
class KafkaRegistration extends decorators_1.MethodRegistration {
|
12
|
-
|
12
|
+
kafkaTopics;
|
13
13
|
consumerConfig;
|
14
14
|
constructor(origFunc) {
|
15
15
|
super(origFunc);
|
16
16
|
}
|
17
17
|
}
|
18
18
|
exports.KafkaRegistration = KafkaRegistration;
|
19
|
-
function KafkaConsume(
|
19
|
+
function KafkaConsume(topics, consumerConfig) {
|
20
20
|
function kafkadec(target, propertyKey, inDescriptor) {
|
21
21
|
const { descriptor, registration } = (0, decorators_1.registerAndWrapFunction)(target, propertyKey, inDescriptor);
|
22
22
|
const kafkaRegistration = registration;
|
23
|
-
kafkaRegistration.
|
23
|
+
kafkaRegistration.kafkaTopics = topics;
|
24
24
|
kafkaRegistration.consumerConfig = consumerConfig;
|
25
25
|
return descriptor;
|
26
26
|
}
|
@@ -54,7 +54,7 @@ class DBOSKafka {
|
|
54
54
|
async initKafka() {
|
55
55
|
for (const registeredOperation of this.dbosExec.registeredOperations) {
|
56
56
|
const ro = registeredOperation;
|
57
|
-
if (ro.
|
57
|
+
if (ro.kafkaTopics) {
|
58
58
|
const defaults = ro.defaults;
|
59
59
|
if (!ro.txnConfig && !ro.workflowConfig) {
|
60
60
|
throw new error_1.DBOSError(`Error registering method ${defaults.name}.${ro.name}: A Kafka decorator can only be assigned to a transaction or workflow!`);
|
@@ -62,8 +62,15 @@ class DBOSKafka {
|
|
62
62
|
if (!defaults.kafkaConfig) {
|
63
63
|
throw new error_1.DBOSError(`Error registering method ${defaults.name}.${ro.name}: Kafka configuration not found. Does class ${defaults.name} have an @Kafka decorator?`);
|
64
64
|
}
|
65
|
+
const topics = [];
|
66
|
+
if (Array.isArray(ro.kafkaTopics)) {
|
67
|
+
topics.push(...ro.kafkaTopics);
|
68
|
+
}
|
69
|
+
else if (ro.kafkaTopics) {
|
70
|
+
topics.push(ro.kafkaTopics);
|
71
|
+
}
|
65
72
|
const kafka = new kafkajs_1.Kafka(defaults.kafkaConfig);
|
66
|
-
const consumerConfig = ro.consumerConfig ?? { groupId:
|
73
|
+
const consumerConfig = ro.consumerConfig ?? { groupId: `${this.safeGroupName(topics)}` };
|
67
74
|
const consumer = kafka.consumer(consumerConfig);
|
68
75
|
await consumer.connect();
|
69
76
|
// A temporary workaround for https://github.com/tulios/kafkajs/pull/1558 until it gets fixed
|
@@ -74,7 +81,7 @@ class DBOSKafka {
|
|
74
81
|
const multiplier = defaults.kafkaConfig.retry ? defaults.kafkaConfig.retry.multiplier ?? 2 : 2;
|
75
82
|
for (let i = 0; i < maxRetries; i++) {
|
76
83
|
try {
|
77
|
-
await consumer.subscribe({
|
84
|
+
await consumer.subscribe({ topics: topics, fromBeginning: true });
|
78
85
|
break;
|
79
86
|
}
|
80
87
|
catch (error) {
|
@@ -116,14 +123,28 @@ class DBOSKafka {
|
|
116
123
|
await consumer.disconnect();
|
117
124
|
}
|
118
125
|
}
|
126
|
+
safeGroupName(topics) {
|
127
|
+
const safeGroupIdPart = topics
|
128
|
+
.map(r => r.toString())
|
129
|
+
.map(r => r.replaceAll(/[^a-zA-Z0-9\\-]/g, ''))
|
130
|
+
.join('-');
|
131
|
+
return `dbos-kafka-group-${safeGroupIdPart}`.slice(0, 255);
|
132
|
+
}
|
119
133
|
logRegisteredKafkaEndpoints() {
|
120
134
|
const logger = this.dbosExec.logger;
|
121
135
|
logger.info("Kafka endpoints supported:");
|
122
136
|
this.dbosExec.registeredOperations.forEach((registeredOperation) => {
|
123
137
|
const ro = registeredOperation;
|
124
|
-
if (ro.
|
138
|
+
if (ro.kafkaTopics) {
|
125
139
|
const defaults = ro.defaults;
|
126
|
-
|
140
|
+
if (Array.isArray(ro.kafkaTopics)) {
|
141
|
+
ro.kafkaTopics.forEach(kafkaTopic => {
|
142
|
+
logger.info(` ${kafkaTopic} -> ${defaults.name}.${ro.name}`);
|
143
|
+
});
|
144
|
+
}
|
145
|
+
else {
|
146
|
+
logger.info(` ${ro.kafkaTopics} -> ${defaults.name}.${ro.name}`);
|
147
|
+
}
|
127
148
|
}
|
128
149
|
});
|
129
150
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"kafka.js","sourceRoot":"","sources":["../../../src/kafka/kafka.ts"],"names":[],"mappings":";;;AAAA,qCAAsH;AAEtH,8CAAmJ;AAInJ,oCAAqC;AACrC,oCAAiC;AAIjC,6BAA6B;AAC7B,6BAA6B;AAC7B,6BAA6B;AAE7B,MAAa,iBAAwD,SAAQ,+BAAsC;IACjH,
|
1
|
+
{"version":3,"file":"kafka.js","sourceRoot":"","sources":["../../../src/kafka/kafka.ts"],"names":[],"mappings":";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