i have this code that opens up a connection to mongodb:
db_connect_mongo = init: (callback) ->
self = this
mongo_options = db:
safe: true
mongoose.connect DB_URL, mongo_options
db = self.db_mongo = mongoose.connection
db.on "error", (error) ->
logger.error "ERROR connecting to: " + DB_URL, logCategory
callback error, null
db.on "connected", ->
logger.info "SUCCESSFULLY connected to: " + DB_URL, logCategory
callback true, db
db.on "disconnected", ->
logger.info "DISCONNECTED from the database: " + DB_URL, logCategory
# check and connect to Redis
exports = module.exports = db_connect_mongo
i then call this in my app.coffee like:
#Connect to database
dbconnection = require "./utils/dbconnect"
dbconnection.init (result) ->
logger.info "Database initialized: " + result, logCategory
when i shut down the mongo db, i get a info: [DATABASE Connection] DISCONNECTED from the database: mongodb://127.0.0.1:27017/zmgc-mongo so in my logger.coffee i would like to 'listen' if the mongodb comes up so that a connection be made.
what would be a good approach to catch all the events of de/re-connection to the db?