-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy patheos.js
77 lines (64 loc) · 2.16 KB
/
eos.js
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
const { Kafka } = require('@confluentinc/kafka-javascript').KafkaJS;
async function eosStart() {
const consumer = new Kafka().consumer({
'bootstrap.servers': 'localhost:9092',
'group.id': 'test-group4',
'enable.auto.commit': false,
'auto.offset.reset': 'earliest',
});
const producer = new Kafka().producer({
'bootstrap.servers': 'localhost:9092',
'transactional.id': 'txid',
});
await consumer.connect();
await producer.connect();
await consumer.subscribe({
topics: ["consumeTopic"]
});
// The run method acts like a consume-transform-produce loop.
consumer.run({
eachMessage: async ({ topic, partition, message }) => {
const msgAckString = JSON.stringify({
topic,
partition,
offset: message.offset,
key: message.key?.toString(),
value: message.value.toString()
});
console.log(msgAckString);
try {
const transaction = await producer.transaction();
await transaction.send({
topic: 'produceTopic',
messages: [
{ value: 'consumed a message: ' + msgAckString },
]
});
await transaction.sendOffsets({
consumer,
topics: [
{
topic,
partitions: [
{ partition, offset: message.offset },
],
}
],
});
await transaction.commit();
} catch (e) {
console.log({ e, s: "ERROR" });
await transaction.abort();
}
},
});
const disconnect = async () => {
process.off('SIGINT', disconnect);
process.off('SIGTERM', disconnect);
await consumer.disconnect();
await producer.disconnect();
}
process.on('SIGINT', disconnect);
process.on('SIGTERM', disconnect);
}
eosStart();