async样例

 function iniProcessDetail(isjob) {
        var cursor = logColl.find({}).sort({ip: 1});
        var insertbolk = [];
        var eveipobj = {};
        var hasinsertdetail = 0;
        var goNext = true;

        function InsertDbs(teminsert) {
            console.log("will insert" + teminsert.length);
            (function (insertbolk) {
                logprocessDetailColl.insert(insertbolk, function (err) {
                    hasinsertdetail += insertbolk.length;
                    console.log("insert insertprocessDetails count" + hasinsertdetail);
                });
            })(teminsert)
            insertbolk = [];
        }

        async.whilst(
            function () {
                return goNext;
            },
            function (callback) {
                cursor.nextObject(function (err, item) {
                    // less 1
//                    if (insertbolk.length == 1000) {
//                        InsertDbs(insertbolk);
//                    }
                    if (item) {
                        setTimeout(function () {
                            function inieveipobj(eveipobj, item) {
                                eveipobj._id = eveipobj._id || item.ip;
                                eveipobj.value = eveipobj.value || {};
                                eveipobj.value.arr = eveipobj.value.arr || [];
                                return eveipobj;
                            }

                            eveipobj = inieveipobj(eveipobj, item);
                            if (eveipobj._id != item.ip) {
                                (function (eveipobj) {
                                    var prodetail = ipMeta.getProcessDetail(eveipobj)
                                    insertbolk.push(prodetail);
                                })(eveipobj)
                                eveipobj = {};
                                eveipobj = inieveipobj(eveipobj, item);
                            }
                            var obj = {}
                            obj.gps = item.gps;
                            obj.created = item.created;
                            eveipobj.value.arr.push(obj);
                            if (insertbolk.length == 1000) {
                                InsertDbs(insertbolk);
                            }
                            callback();
                        }, 5);

                    } else {
                        goNext = false;
                        callback();
                    }
                })
            },
            function (err) {
                if (insertbolk.length > 0) {
                    InsertDbs(insertbolk);
                }
                console.log('No Map/Reduce IniProdetails Finished', err);
                if (isjob) {
                    fromDetailTolocation(isjob);
                }
            }
        );
    }

 

posted @ 2015-04-01 16:32  cclient  阅读(197)  评论(0编辑  收藏  举报