-
Notifications
You must be signed in to change notification settings - Fork 568
/
framework.ts
169 lines (159 loc) · 5.38 KB
/
framework.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import {
BaseFramework,
ConsumerMetadata,
Framework,
getClassMetadata,
listModule,
listPropertyDataFromClass,
MidwayFrameworkType,
MSListenerType,
MS_CONSUMER_KEY,
MidwayInvokeForbiddenError,
} from '@midwayjs/core';
import {
IMidwayConsumerConfig,
IMidwayKafkaApplication,
IMidwayKafkaContext,
} from './interface';
import { KafkaConsumerServer } from './kafka';
@Framework()
export class MidwayKafkaFramework extends BaseFramework<
any,
IMidwayKafkaContext,
any
> {
configure() {
return this.configService.getConfiguration('kafka');
}
async applicationInitialize() {
// Create a connection manager
this.app = new KafkaConsumerServer({
logger: this.logger,
...this.configurationOptions,
}) as unknown as IMidwayKafkaApplication;
}
public async run(): Promise<void> {
try {
await this.app.connect(
this.configurationOptions.kafkaConfig,
this.configurationOptions.consumerConfig
);
await this.loadSubscriber();
this.logger.info('Kafka consumer server start success');
} catch (error) {
this.logger.error('Kafka consumer connect fail', error);
await this.app.closeConnection();
}
}
private async loadSubscriber() {
const subscriberModules = listModule(MS_CONSUMER_KEY, module => {
const metadata: ConsumerMetadata.ConsumerMetadata = getClassMetadata(
MS_CONSUMER_KEY,
module
);
return metadata.type === MSListenerType.KAFKA;
});
for (const module of subscriberModules) {
const data = listPropertyDataFromClass(MS_CONSUMER_KEY, module);
const topicTitles = [...new Set(data.map(e => e[0].topic))];
const midwayConsumerConfigs: IMidwayConsumerConfig[] = topicTitles.map(
e => {
const midwayConsumerConfig: IMidwayConsumerConfig = {
topic: e,
subscription: {},
runConfig: {},
};
const consumerParams = data
.map(value => {
if (value[0].topic === midwayConsumerConfig.topic) {
return Object.assign(midwayConsumerConfig, value[0]);
}
})
.filter(e => e && true);
if (consumerParams && Object.keys(consumerParams[0]).length > 0) {
return consumerParams[0];
}
return midwayConsumerConfig;
}
);
midwayConsumerConfigs.forEach(
async (e: { subscription: any; runConfig: any }) => {
await this.app.connection.subscribe(
Object.assign(
{
topics: topicTitles,
},
e.subscription
)
);
await this.app.connection.run(
Object.assign(e.runConfig, {
eachMessage: async ({ topic, partition, message }) => {
let propertyKey: string | number;
for (const methodBindListeners of data) {
for (const listenerOptions of methodBindListeners) {
if (topic === listenerOptions.topic) {
propertyKey = listenerOptions.propertyKey;
const ctx = {
topic: topic,
partition: partition,
message: message,
commitOffsets: (offset: any) => {
return this.app.connection.commitOffsets([
{
topic: topic,
partition: partition,
offset,
},
]);
},
} as unknown as IMidwayKafkaContext;
this.app.createAnonymousContext(ctx);
if (typeof propertyKey === 'string') {
const isPassed = await this.app
.getFramework()
.runGuard(ctx, module, propertyKey);
if (!isPassed) {
throw new MidwayInvokeForbiddenError(
propertyKey,
module
);
}
}
const ins = await ctx.requestContext.getAsync(module);
const fn = await this.applyMiddleware(async () => {
return await ins[propertyKey].call(ins, message);
});
try {
const result = await fn(ctx);
// 返回为undefined,下面永远不会执行
if (result) {
const res = await this.app.connection.commitOffsets(
message.offset
);
return res;
}
} catch (error) {
// 记录偏移量提交的异常情况
this.logger.error(error);
}
}
}
}
},
})
);
}
);
}
}
public getFrameworkName() {
return 'midway:kafka';
}
protected async beforeStop(): Promise<void> {
await this.app.close();
}
public getFrameworkType(): MidwayFrameworkType {
return MidwayFrameworkType.MS_KAFKA;
}
}