2020-05-11 19:40:39 +00:00
|
|
|
/**
|
|
|
|
* Created by Syed Afzal
|
|
|
|
*/
|
2020-05-12 13:16:03 +00:00
|
|
|
const mongoose = require("mongoose");
|
2020-05-11 19:40:39 +00:00
|
|
|
|
|
|
|
exports.connect = (app) => {
|
2020-05-12 13:16:03 +00:00
|
|
|
const options = {
|
|
|
|
useNewUrlParser: true,
|
|
|
|
autoIndex: false, // Don't build indexes
|
|
|
|
reconnectTries: 30, // Retry up to 30 times
|
|
|
|
reconnectInterval: 500, // Reconnect every 500ms
|
|
|
|
poolSize: 10, // Maintain up to 10 socket connections
|
|
|
|
// If not connected, return errors immediately rather than waiting for reconnect
|
|
|
|
bufferMaxEntries: 0,
|
|
|
|
};
|
2020-05-11 19:40:39 +00:00
|
|
|
|
2020-05-12 13:16:03 +00:00
|
|
|
const connectWithRetry = () => {
|
|
|
|
mongoose.Promise = global.Promise;
|
|
|
|
console.log("MongoDB connection with retry");
|
|
|
|
mongoose
|
|
|
|
.connect(process.env.MONGODB_URI, options)
|
|
|
|
.then(() => {
|
|
|
|
console.log("MongoDB is connected");
|
|
|
|
app.emit("ready");
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log("MongoDB connection unsuccessful, retry after 2 seconds.");
|
|
|
|
setTimeout(connectWithRetry, 2000);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
connectWithRetry();
|
2020-05-11 19:40:39 +00:00
|
|
|
};
|