2014-07-04 77 views
2

我目前正在使用MongoDB並密切監視Nodejs服務器的數據流的Nodejs項目。Nodejs異步上升問題

我的節點服務器的代碼都如下:

Receive.js
1.節點服務器接收到一個JSON文本文件
2.節點服務器通知的MongoDB(UPSERT),我們收到了文件

Process.js
3.節點服務器upserts JSON文件的MongoDB
4.節點,服務器會告訴我們的MongoDB處理所述文件。

的問題是,有時#4發生之前#2儘管#1之前#3總是存在的。我的監視器程序開始顯示處理的文件比收到的文件多。有沒有辦法解決這個問題,而不使這臺服務器完全同步?

例如,如果我把我的節點服務器500 JSON文本文件的最後時間節點執行#2應該是之前的最後一次#4得到執行。

**注:App.js電話從Receive.js .receive和Receive.js調用.save從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

儘管這裏有上調,但在SO條款中並沒有一個非常明確的問題。有很多方法可以通過異步處理和庫來解決這個問題。但是我們需要看到一些代碼是可以回答和解決的問題。顯示你的代碼,以便我們可以看到你的錯在哪裏。 Upvotes!=答案。 –

+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最後被執行。