-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsumer.js
More file actions
68 lines (56 loc) · 1.61 KB
/
Copy pathconsumer.js
File metadata and controls
68 lines (56 loc) · 1.61 KB
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
const { Kafka } = require('@confluentinc/kafka-javascript').KafkaJS;
// the consumerStart function is largely from the Confluent JavaScript client documentation: https://github.com/confluentinc/confluent-kafka-javascript/blob/dev_early_access_development_branch/QUICKSTART.md
require("dotenv").config({path:'.env'});
const brokers = JSON.parse(process.env.brokers);
const kafka = new Kafka({
kafkaJS: {
brokers: brokers,
connectionTimeout: 10000,
ssl: true,
sasl: {
mechanism: "plain",
username: process.env.USERNAMEID,
password: process.env.PASSWORD,
},
},
});
const consumer = kafka.consumer({'group.id':"waheed_test", 'auto.offset.reset': "earliest"});
async function consumerStart() {
const topic = "waheed_test"
await consumer.connect();
await consumer.subscribe({ topics: [topic] });
consumer.run({
eachMessage: async ({ topic, partition, message }) => {
console.log({
topic,
partition,
offset: message.offset,
key: message.key.toString(),
value: message.value.toString(),
});
}
});
}
consumerStart();
const errorTypes = ['unhandledRejection', 'uncaughtException']
const signalTraps = ['SIGTERM', 'SIGINT', 'SIGUSR2']
errorTypes.forEach(type => {
process.on(type, async () => {
try {
console.log(`process.on ${type}`)
await consumer.disconnect()
process.exit(0)
} catch (_) {
process.exit(1)
}
})
})
signalTraps.forEach(type => {
process.once(type, async () => {
try {
await consumer.disconnect()
} finally {
process.kill(process.pid, type)
}
})
})