Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mfrancois3k/e563a23562dcc1f88914b4c56394e92d to your computer and use it in GitHub Desktop.
Save mfrancois3k/e563a23562dcc1f88914b4c56394e92d to your computer and use it in GitHub Desktop.
const mongoose = require('mongoose');
const {
MONGO_IP,
MONGO_PORT,
MONGO_USER,
MONGO_PASSWORD,
} = require('./config/config');
const mongoURL = `mongodb://${MONGO_USER}:${MONGO_PASSWORD}@${MONGO_IP}:${MONGO_PORT}/?authSource=admin`;
// console.log(mongoURL);
let retryAttempts = 0;
const connectWithRetry = () => {
retryAttempts++;
mongoose
.connect(mongoURL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
})
.then(() =>
console.log('connected to mongo', `on attempt ${retryAttempts}`)
)
.catch((err) => {
console.log(`error on mongoose connection ${err}\nRetrying ...`);
if (retryAttempts < 4) {
setTimeout(connectWithRetry, 5000);
} else {
console.log(
`bailing out on connection attempts after ${retryAttempts}, escalate to devOps`
);
}
});
};
connectWithRetry();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment