forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
87 lines (74 loc) · 2.47 KB
/
Copy pathdb.js
File metadata and controls
87 lines (74 loc) · 2.47 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import mongoose from "mongoose";
import logger from "./logger.js";
const connectDB = async () => {
const maxRetries = 5;
let retryCount = 0;
const options = {
maxPoolSize: 10, // Maintain up to 10 socket connections
minPoolSize: 5, // Maintain minimum 5 connections
serverSelectionTimeoutMS: 5000, // Keep trying to send operations for 5 seconds
socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
family: 4, // Use IPv4, skip trying IPv6
retryWrites: true,
w: "majority",
};
const connectWithRetry = async () => {
try {
await mongoose.connect(process.env.MONGO_URI, options);
logger.info("MongoDB connected successfully");
logger.info(`Database: ${mongoose.connection.name}`);
logger.info(`Host: ${mongoose.connection.host}`);
// Handle connection events
mongoose.connection.on("error", (err) => {
logger.error("MongoDB connection error:", err);
});
mongoose.connection.on("disconnected", () => {
logger.warn("MongoDB disconnected. Attempting to reconnect...");
if (retryCount < maxRetries) {
connectWithRetry();
}
});
mongoose.connection.on("reconnected", () => {
logger.info("MongoDB reconnected successfully");
});
mongoose.connection.on("connected", () => {
logger.info("MongoDB connection established");
});
// Graceful shutdown
process.on("SIGINT", async () => {
try {
await mongoose.connection.close();
logger.info("MongoDB connection closed through app termination");
process.exit(0);
} catch (err) {
logger.error("Error during MongoDB disconnection:", err);
process.exit(1);
}
});
} catch (err) {
retryCount++;
logger.error(
`MongoDB connection error (Attempt ${retryCount}/${maxRetries}):`,
err.message
);
if (retryCount < maxRetries) {
logger.info(`Retrying connection in 5 seconds...`);
setTimeout(connectWithRetry, 5000);
} else {
logger.error("Max retries reached. Could not connect to MongoDB.");
process.exit(1);
}
}
};
await connectWithRetry();
};
// Monitor MongoDB performance
mongoose.set("debug", (collectionName, method, query, doc) => {
if (process.env.NODE_ENV === "development") {
logger.debug(`MongoDB: ${collectionName}.${method}`, {
query,
doc,
});
}
});
export default connectDB;