@midwayjs/kafka 3.4.9 → 3.4.12

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/framework.js CHANGED
@@ -22,9 +22,15 @@ let MidwayKafkaFramework = class MidwayKafkaFramework extends core_1.BaseFramewo
22
22
  });
23
23
  }
24
24
  async run() {
25
- await this.app.connect(this.configurationOptions.kafkaConfig, this.configurationOptions.consumerConfig);
26
- await this.loadSubscriber();
27
- this.logger.info('Kafka consumer server start success');
25
+ try {
26
+ await this.app.connect(this.configurationOptions.kafkaConfig, this.configurationOptions.consumerConfig);
27
+ await this.loadSubscriber();
28
+ this.logger.info('Kafka consumer server start success');
29
+ }
30
+ catch (error) {
31
+ this.logger.error('Kafka consumer connect fail', error);
32
+ await this.app.closeConnection();
33
+ }
28
34
  }
29
35
  async loadSubscriber() {
30
36
  const subscriberModules = (0, decorator_1.listModule)(decorator_1.MS_CONSUMER_KEY, module => {
@@ -33,49 +39,78 @@ let MidwayKafkaFramework = class MidwayKafkaFramework extends core_1.BaseFramewo
33
39
  });
34
40
  for (const module of subscriberModules) {
35
41
  const data = (0, decorator_1.listPropertyDataFromClass)(decorator_1.MS_CONSUMER_KEY, module);
36
- for (const methodBindListeners of data) {
37
- for (const listenerOptions of methodBindListeners) {
38
- await this.app.connection.subscribe({
39
- topic: listenerOptions.topic,
40
- // fromBeginning: true,
41
- });
42
- await this.app.connection.run({
43
- autoCommit: false,
44
- eachMessage: async ({ topic, partition, message }) => {
45
- const ctx = {
46
- topic: topic,
47
- partition: partition,
48
- message: message,
49
- commitOffsets: (offset) => {
50
- return this.app.connection.commitOffsets([
51
- {
52
- topic: topic,
53
- partition: partition,
54
- offset,
42
+ const topics = [...new Set(data.map(e => e[0].topic))];
43
+ let tempTopics = [];
44
+ topics.forEach(e => {
45
+ tempTopics.push({ topic: e });
46
+ });
47
+ tempTopics = tempTopics.map(e => {
48
+ let subscription = {};
49
+ let runConfig = {};
50
+ data.forEach(e2 => {
51
+ if (e2[0].topic === e.topic) {
52
+ if (typeof e2[0].subscription !== 'undefined' &&
53
+ Object.keys(e2[0].subscription).length > 0) {
54
+ subscription = e2[0].subscription;
55
+ }
56
+ if (typeof e2[0].runConfig !== 'undefined' &&
57
+ Object.keys(e2[0].runConfig).length > 0) {
58
+ runConfig = e2[0].runConfig;
59
+ }
60
+ }
61
+ });
62
+ e.subscription = subscription;
63
+ e.runConfig = runConfig;
64
+ return e;
65
+ });
66
+ tempTopics.forEach(async (e) => {
67
+ await this.app.connection.subscribe(Object.assign({
68
+ topics: topics,
69
+ }, e.subscription));
70
+ await this.app.connection.run(Object.assign(e.runConfig, {
71
+ eachMessage: async ({ topic, partition, message }) => {
72
+ let propertyKey;
73
+ for (const methodBindListeners of data) {
74
+ for (const listenerOptions of methodBindListeners) {
75
+ if (topic === listenerOptions.topic) {
76
+ propertyKey = listenerOptions.propertyKey;
77
+ const ctx = {
78
+ topic: topic,
79
+ partition: partition,
80
+ message: message,
81
+ commitOffsets: (offset) => {
82
+ return this.app.connection.commitOffsets([
83
+ {
84
+ topic: topic,
85
+ partition: partition,
86
+ offset,
87
+ },
88
+ ]);
55
89
  },
56
- ]);
57
- },
58
- };
59
- this.app.createAnonymousContext(ctx);
60
- const ins = await ctx.requestContext.getAsync(module);
61
- const fn = await this.applyMiddleware(async (ctx) => {
62
- return await ins[listenerOptions.propertyKey].call(ins, message);
63
- });
64
- try {
65
- const result = await fn(ctx);
66
- // 返回为undefined,下面永远不会执行
67
- if (result) {
68
- const res = await this.app.connection.commitOffsets(message.offset);
69
- return res;
90
+ };
91
+ this.app.createAnonymousContext(ctx);
92
+ const ins = await ctx.requestContext.getAsync(module);
93
+ const fn = await this.applyMiddleware(async (ctx) => {
94
+ return await ins[propertyKey].call(ins, message);
95
+ });
96
+ try {
97
+ const result = await fn(ctx);
98
+ // 返回为undefined,下面永远不会执行
99
+ if (result) {
100
+ const res = await this.app.connection.commitOffsets(message.offset);
101
+ return res;
102
+ }
103
+ }
104
+ catch (error) {
105
+ // 记录偏移量提交的异常情况
106
+ this.logger.error(error);
107
+ }
70
108
  }
71
109
  }
72
- catch (error) {
73
- this.logger.error(error);
74
- }
75
- },
76
- });
77
- }
78
- }
110
+ }
111
+ },
112
+ }));
113
+ });
79
114
  }
80
115
  }
81
116
  getFrameworkName() {
package/dist/kafka.d.ts CHANGED
@@ -5,7 +5,7 @@ import { IKafkaApplication } from './interface';
5
5
  import { EventEmitter } from 'stream';
6
6
  export declare class KafkaConsumerServer extends EventEmitter implements IKafkaApplication {
7
7
  protected loggers: ILogger;
8
- protected connection: Consumer;
8
+ connection: Consumer;
9
9
  constructor(options?: any);
10
10
  bindError(): void;
11
11
  connect(options: KafkaConfig, consumerOptions: ConsumerConfig): Promise<void>;
package/dist/kafka.js CHANGED
@@ -16,24 +16,18 @@ class KafkaConsumerServer extends stream_1.EventEmitter {
16
16
  });
17
17
  }
18
18
  async connect(options, consumerOptions) {
19
- try {
20
- this.connection = new kafkajs_1.Kafka(options).consumer(consumerOptions);
21
- this.connection.on('consumer.connect', () => {
22
- this.loggers.info('Kafka consumer connected!');
23
- });
24
- this.connection.on('consumer.disconnect', err => {
25
- if (err) {
26
- this.loggers.error('Kafka consumer disconnected', err);
27
- }
28
- else {
29
- this.loggers.info('Kafka consumer disconnected!');
30
- }
31
- });
32
- }
33
- catch (error) {
34
- this.loggers.error('Kafka consumer connect fail', error);
35
- await this.closeConnection();
36
- }
19
+ this.connection = new kafkajs_1.Kafka(options).consumer(consumerOptions);
20
+ this.connection.on('consumer.connect', () => {
21
+ this.loggers.info('Kafka consumer connected!');
22
+ });
23
+ this.connection.on('consumer.disconnect', err => {
24
+ if (err) {
25
+ this.loggers.error('Kafka consumer disconnected', err);
26
+ }
27
+ else {
28
+ this.loggers.info('Kafka consumer disconnected!');
29
+ }
30
+ });
37
31
  }
38
32
  async closeConnection() {
39
33
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/kafka",
3
- "version": "3.4.9",
3
+ "version": "3.4.12",
4
4
  "description": "Midway Framework for kafka",
5
5
  "main": "dist/index",
6
6
  "typings": "index.d.ts",
@@ -33,10 +33,10 @@
33
33
  },
34
34
  "homepage": "https://github.com/midwayjs/midway#readme",
35
35
  "devDependencies": {
36
- "@midwayjs/core": "^3.4.9",
37
- "@midwayjs/decorator": "^3.4.4",
36
+ "@midwayjs/core": "^3.4.12",
37
+ "@midwayjs/decorator": "^3.4.11",
38
38
  "@midwayjs/logger": "^2.15.0",
39
- "@midwayjs/mock": "^3.4.9"
39
+ "@midwayjs/mock": "^3.4.12"
40
40
  },
41
41
  "dependencies": {
42
42
  "kafkajs": "2.1.0"
@@ -44,5 +44,5 @@
44
44
  "engines": {
45
45
  "node": ">=12"
46
46
  },
47
- "gitHead": "bc8113cff70d88a31e44bde69c395f61a7de2045"
47
+ "gitHead": "96425bbb3a39dbe8f66069facc5b7440886f9cfe"
48
48
  }