0

我在亚马逊 ec2 实例上有一个带有 mongo 服务器的节点应用程序。它工作得很好,但我只是添加了一个新的 API 调用,每次调用它时,服务器都会冻结,我无法访问/ssh 几个小时。发生这种情况时,我的服务器出现故障,这使得依赖它的应用程序无法使用,我的用户很生气......

这段代码在我的 localhost 上运行良好,但是一旦我在我的服务器上运行它,它就会冻结。我的想法是它可能会崩溃 mongo?我不知道为什么会发生这种情况......

如果有人有任何想法可能会出错,请告诉我。

节点正在使用快递。send_error 函数将执行 res.send({some error})。db.CommentModel 返回 mongoose.model('comment', Comment);

在 app.js 中

app.get('/puzzle/comment/:id', auth.restrict, puzzle.getComments);

在定义 getComments 的文件中

exports.getComments = function(req, res)
{   
    var userID = _u.stripNonAlphaNum(req.params.id);

    var CommentModel = db.CommentModel;
    CommentModel.find({user: userID}, function(e, comments) {
        if(e) 
        {
            err.send_error(err.DB_ERROR, res);
        }
        else if (!comments)
        {
            err.send_error(err.DB_ERROR, res);
        }
        else if (comments.length == 0)
        {
            res.send([]);
    }
    else
    {
        var commentIDs = [];
        for (var i = 0; i<comments.length; i++)
        {
            commentIDs.push({_id: comments[i].puzzle});
        }
        var TargetModel = pApp.findPuzzleModel(_u.stripNonAlphaNum(req.apiKey));
            TargetModel.find({removed: false, $or: commentIDs}, function(e, puzzles) {
                if(e) 
                {
                    err.send_error(err.DB_ERROR, res);
                }
                else if (!puzzles)
                {
                    err.send_error(err.DB_ERROR, res);
                }
                else
                {
                    res.send(puzzles);
                }
            });
        }
    });
}
4

1 回答 1

0

听起来您的查询导致服务器上的某些东西(可能是 mongo)消耗大量 CPU - 因为这通常是导致您在 SSH 访问中看到的问题的原因。

您应该尝试阅读 mongo 实例的日志并查看是否有任何长时间运行的查询。

Monngodb 提供了一个内部分析器来检查长时间运行的命令。尝试将长时间运行的分析级别设置为 1,运行命令并检查日志文件输出。

有关分析器的更多详细信息,请访问http://www.mongodb.org/display/DOCS/Database+Profiler

于 2012-09-07T02:52:01.547 回答