我有这个代码打开了与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
然后我在app.coffee中调用它,如:
#Connect to database
dbconnection = require "./utils/dbconnect"
dbconnection.init (result) ->
logger.info "Database initialized: " + result, logCategory
当我关闭mongo db时,我得到一个info: [DATABASE Connection] DISCONNECTED from the database: mongodb://127.0.0.1:27017/zmgc-mongo
所以在我的logger.coffee中我想“听”如果mongodb出现以便建立连接。
什么是捕获所有de / re连接到db的事件的好方法?