Nodejs Async upsert问题

时间:2014-07-04 08:38:01

标签: javascript jquery ajax node.js mongodb

我目前正在开发一个Nodejs项目,该项目使用MongoDB并密切监视Nodejs服务器的数据流。

我的节点服务器的代码如下:

Receive.js
1.节点服务器接收JSON文本文件
2.节点服务器告诉MongoDB(upsert)我们收到了一个文件

Process.js
3.节点服务器将JSON文件upsert到MongoDB
节点服务器告诉MongoDB我们处理了所说的文件。

问题是有时#4 发生在#2 之前,尽管#1 始终发生在#3 之前。我的监控程序开始显示处理的文件多于收到的文件。有没有办法解决这个问题,而不使这个服务器完全同步?

例如,如果我发送节点服务器500个JSON文本文件,那么Node最后一次执行#2 应该在上一次#4 执行之前。

**注意:App.js来自Receive.js的{​​{1}}和Receive.js来电。来自Process.js **

app.js

http.createServer(app).listen(app.get('port'), function(){
  mongodb.createConnection(function(db){
    if(db){
      console.log('success connecting to mongodb!');
    }
  });
});  
app.post('/log', logCollection.receive);   

-
Receive.js

exports.receive = function(req, res, next){
  var usagelog = req.body.log;

  if(!usagelog){
    log4js.logger.error("Usagelog is Null!");
    res.end("fail");
  }else{      
    count++;

    //Upsert the Receive Count for Monitor  
      mongodb.getConnection(function(db){

          dateFormat.initDate();
          //Upsert trip (usage logs)
          var currentDateTime =  moment().format('YYYY-MM-DD hh:mm:ss');
          db.collection("ReceiveCount").update( {"date":dateFormat.currentDate(), "pid":process.pid }, 
                    {"date":dateFormat.currentDate(), "IPAddress": ip.address(), "pid":process.pid, "countReceive" : count, "LastReceivedTime": currentDateTime}, 
                    {upsert:true}, function(err, result) {            
            });
          });

    //End Receive count
    var usagelogJSON = convertToJson(usagelog);
    var usagelogWithCurrentDate = addUpdateTime(usagelogJSON);
    usagelogDao.save(usagelogWithCurrentDate, res);
  }
};  

-

Process.js

exports.save = function(usagelog, res){
  var selector = upsertCondition(usagelog);

  mongodb.getConnection(function(db){
    //Upsert trip (usage logs)
    db.collection(collectionName()).update(selector, usagelog, {upsert:true, fullResult:true}, function(err, result) {
      if(err){
        log4js.logger.error(err);
        res.end("fail");
      }else{

        if(result.nModified == 0)
            countInsert++;
        else
            countUpdate++;

        //Upsert Processed Count (both Updated and Inserted     
        db.collection("ReceiveCount").find({"date":dateFormat.currentDate(), "pid":process.pid },{}).toArray(function (err, docs) {

            var receivecount = docs[0].countReceive;
            var receivetime = docs[0].LastReceivedTime;
            var currentDateTime = moment().format('YYYY-MM-DD hh:mm:ss');
            var MongoCount=0;

            db.collection("raw_"+dateFormat.currentDate()).count(function(err, count){
                console.log("raw_"+dateFormat.currentDate());

                MongoCount = count;
                console.log("Mongo count is :" +MongoCount);
            });

            db.collection("ReceiveCount").update( {"date":dateFormat.currentDate(), "pid":process.pid }, 
                {"date":dateFormat.currentDate(), "IPAddress": ip.address(), "pid":process.pid, "countUpdate" : countUpdate, "countInsert":countInsert, "countTotal":countUpdate+countInsert, "LastProcessTime": currentDateTime,
                "countReceive":receivecount, "LastReceivedTime":receivetime, "MongoCount":MongoCount}, {upsert:true}, function(err, result) {      
            }); 
        });
        res.end("success");
      }
    });
  });
};

1 个答案:

答案 0 :(得分:0)

您可以使用异步模块(https://github.com/caolan/async)。这是伪代码:

async.parallel([
  fun1(cb) {
    tellDBYouReceivedFile(..., onFinished() {
      cb();
    });
  },
  fun2(cb) {
    saveYourFileToDB(..., onFinished() {
      cb();
    });
  },
], function() {
  tellDBYouProcessedFile();
});

您要实现的目标不能完全异步完成,因为最后一步(#4)必须等待#2和#3先完成。上面的例子可以使#2和#3异步,保证#4最后执行。