1

我正在编写一个小节点来通过 Mongoose 进行简单查询来监控 MongoDB。如果有东西被退回,则该服务被视为可用。这是我的脚本

var res;
var logPrefix = '[MongoDB]';
var counter = 0;

var mongoose = require('mongoose');
// Simple schema and model for getting a companyprofiles document
var companyProfile = new mongoose.Schema({
    _id: String
});
companyProfile.virtual('Id').get(function () {return this._id});

function closeConnection() {
    console.log('8');
    mongoose.connection.close(function () {
        console.log('9');
        console.log('%s Closed connection%d', logPrefix, counter);
    });
}

function connectAndCheckHealth() {
    console.log('2');
    mongoose.connect('mongodb://localhost:27017/testdb');

    console.log('3');
    mongoose.connection.on('error', function(err) {
        console.log('%s Error connecting to DB:\n%s %s', logPrefix, logPrefix, err);
        res.send(503, 'ERR');
    });

    mongoose.connection.on('open', function() {
        mongoose.connection.db.serverConfig.options.auto_reconnect = false;
    });

    console.log('4');
    mongoose.connection.on('connected', checkHealth);

    console.log('5');
    mongoose.connection.on('close', function() {
        console.log('%s Connection to MongoDB closed %d', logPrefix, counter);
    });
}

function checkHealth() {
    console.log('6');
    cpModel = mongoose.model('companyProfile', companyProfile, 'companyprofiles');
    cpModel.findById('spin', handleModelResponse);
}

function handleModelResponse(error, doc) {
    console.log('7');
    closeConnection();
    console.log('10');
    if (error !== null) {
        console.log('11');
        console.log('%s Error handling response from model:\n%s %s', logPrefix, logPrefix, error);
        res.send(503, 'ERR');
    } else {
        console.log('12');
        if (doc.Id.match(/company1/)) {
            console.log('13');
            console.log('%s App status is ok', logPrefix);
            res.send(200, 'OK');
        } else {
            console.log('14');
            console.log('%s Couldn\'t find the spin company profile. Found %s', logPrefix, doc.Id);
            res.send(503, 'ERR');
        }
    }
}

module.exports = {
    health: function(response) {
        counter++;
        var date = new Date();
        console.log('%s Retrieving health from MongoDB at %s', logPrefix, date);
        res = response;
        console.log(mongoose.connection);
        console.log('1');
        connectAndCheckHealth();
        console.log('15');
    }
}

如您所见,我在脚本中添加了带有数字的 console.log 行,以尝试制定控制流程。这是输出:

1
2
3
4
5
15
6
6
6
7
8
[MongoDB] Connection to MongoDB closed 3
[MongoDB] Connection to MongoDB closed 3
[MongoDB] Connection to MongoDB closed 3
9
[MongoDB] Closed connection3
10
12
13
[MongoDB] App status is ok
7
8
9
[MongoDB] Closed connection3
10
12
13
[MongoDB] App status is ok

/home/GTP/healthcheck/node_modules/mongoose/lib/utils.js:413
        throw err;
              ^
Error: Can't set headers after they are sent.

注意 6 出现了 3 次(我们连接后的回调)。我不知道为什么我们要打开多个连接。我已关闭自动连接并在每次请求后关闭连接。

任何帮助将不胜感激。

4

2 回答 2

3

Mongoose 默认打开一个 5 个连接的池;如果您只想要一个连接,您可以将connect呼叫更改为:

mongoose.connect('mongodb://localhost:27017/testdb', { server: { poolSize: 1 }});
于 2012-11-06T16:15:59.677 回答
1

db.once('open') 为我工作。

在测试我的页面时,我注意到双重和快速的表单提交会触发有关已发送标题的错误。这停止了​​。

于 2013-08-17T01:23:10.933 回答