MonogDB连接丢失后如何确保Node.js保持运行? [英] How to ensure Node.js keeps running after MonogDB connection drops?

查看:170
本文介绍了MonogDB连接丢失后如何确保Node.js保持运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  app.use(function(错误,req,res,next){
console.error(err.stack);
res.status(500);
res.render('500.jade');
});

但是由于某种原因,每当我关闭 mongod 进程,我的应用程序崩溃与以下堆栈跟踪:

 错误:无法连接到[localhost:27017] 
在null。< anonymous> (/<hidden>/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:540:74)
在EventEmitter.emit(events.js:106:17)
在null。< anonymous> (/<hidden>/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:140:15)
在EventEmitter.emit(events.js:98:17)
在Socket。< anonymous> (/<hidden>/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:478:10)
在Socket.EventEmitter.emit(events.js:95:17)
at net.js:441:14
at process._tickCallback(node.js:415:13)

处理完成退出代码8

我尝试使用以下配置,但没有帮助我:

  var options = {
server:{
auto_reconnect:true,
poolSize:10,
socketOptions:{
keepAlive: 1
}
},
db:{
numberOfRetries:10,
retryMiliSeconds:1000
}
}

mongoose.connect(config.db,options);

有人可能会问:如果您的应用程序在没有数据库连接?。我不希望它崩溃并重新启动。主管和Forever模块似乎停止尝试在一定次数的尝试后重新连接,从而使您的应用程序挂起在崩溃状态。



理想情况下,当MongoDB崩溃我想向用户显示一个500.jade错误页面,同时服务器应该每10秒钟继续尝试重新连接数据库。一旦重新连接,请恢复所有正常操作。



编辑:除了域名外,以下发布的解决方案均不适用于我。

解决方案

尝试在 domain ,这样可以捕捉错误,而不会崩溃

  var d = require ')。创建(); 

d.on('error',function(er){
console.log('哦,不,DB有问题);
});

d.run(function(){
mongoose.connect(config.db,options);
});

通常情况下让服务器在崩溃时重启,但是您已经意识到的这个,并希望避免它,包装任何失败的域是这样做的方式。


I have an error-handling middlware in Express that tries to catch all incoming errors:

app.use(function(err, req, res, next){
  console.error(err.stack);
  res.status(500);
  res.render('500.jade');
});

But for some reason whenever I close mongod process, my application crashes with the following stack trace:

Error: failed to connect to [localhost:27017]
    at null.<anonymous> (/<hidden>/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:540:74)
    at EventEmitter.emit (events.js:106:17)
    at null.<anonymous> (/<hidden>/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:140:15)
    at EventEmitter.emit (events.js:98:17)
    at Socket.<anonymous> (/<hidden>/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:478:10)
    at Socket.EventEmitter.emit (events.js:95:17)
    at net.js:441:14
    at process._tickCallback (node.js:415:13)

Process finished with exit code 8

I have tried using the following configuration, but it didn't help me:

var options = {
  server:{
    auto_reconnect: true,
      poolSize: 10,
      socketOptions:{
      keepAlive: 1
    }
  },
  db: {
    numberOfRetries: 10,
    retryMiliSeconds: 1000
  }
}

mongoose.connect(config.db, options);

One might wonder "why would you want your application to be running if it's essentially no longer functioning without a database connection?". I don't want it to crash and restart itself. Supervisor and Forever modules seem to stop trying to reconnect after a certain number of tries, thus leaving your application hanging in a crashed state.

Ideally, when MongoDB crashes I would like to display a 500.jade error page to users, meanwhile server should keep trying to reconnect to the database every 10 seconds. Once reconnected, resume all normal operations.

EDIT: None of the solutions posted below have worked for me, with the exception of domains.

解决方案

Try initializing the DB in a domain, that would catch the errors without crashing

var d = require('domain').create();

d.on('error', function(er) {
    console.log('Oh no, something wrong with DB');
});

d.run(function() {
    mongoose.connect(config.db, options);
});

It's generally a good idea to let the server restart when something crashes, but as you're already aware of this and want to avoid it, wrapping whatever fails in domains is the way to do it.

这篇关于MonogDB连接丢失后如何确保Node.js保持运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆