In this GitHub issue for mongoose the developer stated that it is intended behaviour to crash the Node.js process if the initial database connection to MongoDB fails. It does this instead of trying to reconnect.
In my code I catch this error because I want to log it.
var mongooseOptions = {
useMongoClient: true,
reconnectInterval: 10000,
reconnectTries: Number.MAX_VALUE
};
mongoose.connect(connectionString, mongooseOptions)
.catch(err => {
logger.error('Mongodb first connection failed: ' + err.stack);
// what to do here? - process.exit(0); maybe?
});
But after that, what is the best practice to do?
crash the process? or write my own reconnecting logic? I maybe should mention that the mongodb.service
is listed as a requirement for the node.service
to start (using systemd in linux).
[Unit]
After=mongodb.service
...
I am also unsure how often I can expect to see this happen.
I have also used this guide but I cannot find a clear answer.