9

I'm trying to properly close a MongoDB connection when the app is shut down. Here is the code:

var express = require('express')
  , http = require('http')
  , mongoose = require('mongoose')
  , path = require('path');

var app = express();

app.set('port', process.env.PORT || 3000);

mongoose.connect('mongodb://localhost/test');

// some post and get handlers etc. (removed for shorter output)

var server = app.listen(app.get('port'), function(){
    console.log('Express server listening on port ' + app.get('port'));
});

function cleanup () {
    server.close(function () {
        console.log("Closed out remaining connections.");
        mongoose.connection.close();
        process.exit();
    });

    setTimeout( function () {
        console.error("Could not close connections in time, forcing shut down");
        process.exit(1);
    }, 30*1000);
}

process.on('SIGINT', cleanup);
process.on('SIGTERM', cleanup);

All is well and works when the app starts for the first time. When I hit Ctrl-c right after it has started, it cleanly shuts down with the Closed out remaining connections. message. However, as soon as the app interacts with the database or even serves a static page, and I try to shut it down after that, it exits with the following error:

net.js:1225
    throw new Error('Not running');
          ^
Error: Not running
    at Server.close (net.js:1225:11)
    at process.cleanup (<...>/app.js:77:12)
    at process.EventEmitter.emit (events.js:92:17)
    at Signal.wrap.onsignal (node.js:756:46)
22 Aug 15:15:28 - [nodemon] exiting

Any ideas what is causing this error and how I could fix it?

4

3 回答 3

4

调用 server.close 时,会检查两个属性。

   handle<tcp handle>
   connections

server.close 负责此错误的代码片段;

  if (!this._handle) {
    // Throw error. Follows net_legacy behaviour.
    throw new Error('Not running');
  }

只有当handle===null 和connections ===0 的回调传递给close 时才会被调用。

案例:服务器启动并发送没有服务的信号。

在调用 close 之前;

  handle === TCP handle.
  connection===0;

关闭句柄后===null; 连接===0;

回调被调用。

案例:服务器启动并在请求服务器之后发送信号。

在调用 close 之前;

  handle === TCP.handle;
  connection===1;

关闭句柄后=== null; 连接===1;

没有回调被触发。

第二次按 ctrl-c

在调用 close 之前;

  handle === null;
  connection===1;

由于 handle===null ,检查会引发您看到的错误。

于 2013-08-22T15:03:18.703 回答
2

您的服务器打开连接的原因是因为您正在发送Connection: keep-alive标头。

摩根的回答干净利落地关闭了服务器上的所有连接,断开了所有客户端。

如果您只是在测试您的应用程序并希望在测试之前/之后彻底关闭它,我建议您发送Connection: close标头并按server.close()预期工作。

于 2015-09-08T18:14:48.900 回答
1

我不认为设置 server._connections = 0 实际上会关闭连接。它只是满足执行回调的条件。

您可能想尝试这样的事情:

// Everything else how you had it before ...

var sockets = [];

server.on('connection', function(socket) {
  sockets.push(socket);
});

function cleanup () {
  server.close(function () {
    console.log("Closed out remaining connections.");
     // mongoose.connection.close(); Might want to comment this out
    process.exit();
  });

  // Add this part to manually destroy all the connections.
  sockets.forEach(function(socket) {
    socket.destroy();
  });

  // setTimeout() ...
}
于 2014-02-13T03:43:47.343 回答