-
-
Save mfrancois3k/e563a23562dcc1f88914b4c56394e92d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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