将JS函数添加到并行队列

时间:2015-03-27 20:13:09

标签: javascript node.js mongodb asynchronous

我有这个代码,我想运行所有数据库插入,然后在脚本结束时断开与数据库的连接。问题是我需要在循环结束后执行断开连接回调,但这是非确定性的。所以我需要为每个循环创建一个函数,然后只有在完成所有这些函数之后,才调用该函数断开与mongoose / mongoDB的连接。

有没有人看到我对此代码的问题?

//此代码很接近,但并不完全,因为它会在执行非常多的插入/保存之前与mongo断开连接

var mongoose = require('mongoose')
    , Admin = mongoose.mongo.Admin;

var UserModel = require('../models/UserModel');
UserModel.registerSchema(mongoose);


var fs = require('fs');
var parsedJSON = JSON.parse(fs.readFileSync('../dummy_data/dummy_user_data', 'utf8'));


var system_db = mongoose.connect('mongodb://localhost:27017/local_dev_db');


function insertUsers(callback){


    parsedJSON.forEach(function (item, index) {

        var User = UserModel.getNewUser(system_db);

        var user = new User({
            username: item.username,
            password: item.password,
            address: item.address,
            phone: item.phone,
            email: item.email,
            gender: item.gender,
            about: item.about,
            latitude: item.latitude,
            longitude: item.longitude
        });

        user.save(function (err, result) {
            if (err) {
                console.log("error in player save method:", err);
            }
            console.log(index);
            if (result) {
                //console.log('Added!', result);
            }
        });

    });
    callback();
}


function disconnect(){
    mongoose.disconnect();
    mongoose.connection
        .close(function () {

            console
                .log('Mongoose connection disconnected');

            process.exit(0);

        });
}

insertUsers(disconnect);

所以解决方案是使用async.parallel库,并且只有在完成所有函数之后,才调用最终的回调函数。但是,如何以编程方式执行此操作而不是将一组已知函数放入async.parallel代码块中,我需要将未知数量的函数放入aysnc.parallel代码块中。雅见?

2 个答案:

答案 0 :(得分:1)

绝对可以做到。只需将您的函数动态添加到将传递给async.parallel的数组:

var fs = require('fs');
var async = require('async');

var mongoose = require('mongoose'),
    Admin = mongoose.mongo.Admin;

var UserModel = require('../models/UserModel');
UserModel.registerSchema(mongoose);

var parsedJSON = JSON.parse(fs.readFileSync('../dummy_data/dummy_user_data', 'utf8'));

var system_db = mongoose.connect('mongodb://localhost:27017/local_dev_db');

var insertFunctions = [];

parsedJSON.forEach(function (item, index) {

    insertFunctions.push(function(callback) {

        var User = UserModel.getNewUser(system_db);

        var user = new User({
            username: item.username,
            password: item.password,
            address: item.address,
            phone: item.phone,
            email: item.email,
            gender: item.gender,
            about: item.about,
            latitude: item.latitude,
            longitude: item.longitude
        });

        user.save(function (err, result) {

            if (err) {
                console.log("error in player save method:", err);
                callback(err);
                return;
            }

             callback(null, result);

        });
    });
});

function disconnect() {

    mongoose.disconnect();
    mongoose.connection
        .close(function () {

            console
                .log('Mongoose connection disconnected');

            process.exit(0);
        });
}

//First parameter is the array of functions to run in parallel,
// second parameter is the callback function
async.parallel(insertFunctions, disconnect);

答案 1 :(得分:1)

在这种特殊情况下,async.each()实际上比async.parallel()更合适。 async.each()是并行的,而async.eachSeries()则不是。这是代码:

function insertUsers(callback){
  var User = UserModel.getNewUser(system_db);

  async.each(parsedJSON, function (item, eachCb) {
    var user = new User({
        username: item.username,
        password: item.password,
        address: item.address,
        phone: item.phone,
        email: item.email,
        gender: item.gender,
        about: item.about,
        latitude: item.latitude,
        longitude: item.longitude
    });

    user.save(function (err, result) {
        if (err) {
            console.log("error in player save method:", err);
        }
        console.log(index);
        if (result) {
            //console.log('Added!', result);
        }
        eachCb();  // if calling with eachCb(err), async.each()
                   // will not continue the rest of the items
                   // in case of error occurs
    });

  }, function(err) {
    callback();    // done with all user.save() calls
  });
}