-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathConsumer.js
96 lines (90 loc) · 3.08 KB
/
Consumer.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const { Kafka } = require("kafkajs");
const mongoose = require("mongoose");
require("dotenv").config();
const Rating = require("./model_mongodb/dbmongo");
const clientES = require("./SearchEngine"); // Import OpenSearch client
const db = require("./database");
// MongoDB connection
mongoose
.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log("MongoDB connected"))
.catch((err) => console.error("MongoDB connection error:", err));
// Create a Kafka instance
const kafka = new Kafka({
clientId: "my-app",
brokers: ["localhost:29092"],
});
const consumer = kafka.consumer({ groupId: "test-group" });
const run = async () => {
await consumer.connect();
await consumer.subscribe({
topic: "dbserver1.bookingapp.booker",
fromBeginning: false,
});
await consumer.run({
eachMessage: async ({ topic, partition, message }) => {
try {
if (message.value) {
// Parse the message value as JSON
const messagePayload = JSON.parse(message.value.toString());
const { payload } = messagePayload;
console.log("Payload:", payload);
// Logging the operation type
const op = payload.op;
const userName = `${payload.after.first_name} ${payload.after.last_name}`;
console.log("Operation:", op);
if (op === "u" && payload.after) {
db.query(
`SELECT distinct (id) FROM bookingapp.vReservation where name= '${userName}';`,
async (err, result) => {
if(result){
for(let i = 0; i < result.length; i++) {
const newData = {
doc: {
name: `${userName}`
}
};
const response = await clientES.update({
index: 'bookingapp',
id: result[i].id, // Ensure the correct ID is used
body: newData
});
}
}
}
);
// console.log(`Updated document with name: ${userName}`);
} else if (op === "d" && payload.before) {
const idToDelete = payload.before.id;
const response = await clientES.delete({
index: "bookingapp",
id: idToDelete,
});
console.log("Deleted from Elasticsearch:", response.body);
await Rating.deleteMany({ idUser: idToDelete });
console.log(`Deleted documents with id ${idToDelete} from MongoDB`);
}
} else {
console.log("Received null or undefined message value");
}
} catch (error) {
console.error("Error processing message:", error);
}
},
});
};
run().catch(console.error);
// Gracefully close consumer on exit
process.on("SIGINT", async () => {
try {
await consumer.disconnect();
console.log("Kafka consumer disconnected");
process.exit();
} catch (error) {
console.error("Error disconnecting Kafka consumer:", error);
process.exit(1);
}
});