Home > Mobile >  how frequently check mongoose connection status nodejs
how frequently check mongoose connection status nodejs

Time:06-14

I need to check the mongoose package connection string status dynamically after instance start, like npm start.

I tried this method,

setInterval(function(){ 
  if(mongoose.connection.readyState == 1){
    mongoStatus = true;
  }else{
    mongoStatus = false;
  }
}, 2000);

but i need mongoose predefined events?

NOTE: I am trying to create a custom circuit breaker for all services so that i need to check frequently the status

CodePudding user response:

You can use the built-in Connection events in Mongoose.

mongoose.connection.on('error' | 'disconnecting' | 'disconnected' | 'close', event => {
  mongoStatus = false // Or whatever you want to do
})
  • Related