Nodejsasynchronous上升问题

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

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

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

Process.js
3.节点服务器将JSONfile upload到MongoDB
4.节点服务器告诉MongoDB我们处理了这个文件。

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

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

**注意: App.js调用Receive.jsReceive.js调用。从Process.js调用.save **

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"); } }); }); }; 

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

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

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