-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup.js
55 lines (53 loc) · 1.55 KB
/
cleanup.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
const amqp = require("amqplib");
/**
* Performs closing of connections for mongoose and rabbitmq
* @param {string} service
* @param {amqp.Connection} conn
* @param {Array<[string, amqp.Channel]>} channels
*/
function cleanup(service, conn, channels, server) {
console.log(`${service} received SIGTERM`);
try {
if (server) {
server.close((err) => {
if (err) {
throw err;
}
});
console.log(`${service} express server closed`);
}
if (channels.length !== 0) {
for (const index in channels) {
const item = channels[index];
if (item[1]) {
item[1]
.close()
.then(
console.log(
`${service} ${item[0]} channel closed`
)
);
}
}
}
if (conn) {
conn.close().then(
console.log(`${service} RabbitMQ connection closed`)
);
}
if (service !== "frontend-service" && service !== "payment-service") {
const mongoose = require("mongoose");
mongoose.connection
.close()
.then(
console.log(`${service} MongoDB connection closed`)
);
}
} catch (err) {
console.error(
`Error occurred during ${service} shutdown:`,
err
);
}
}
module.exports = cleanup