编程语言
首页 > 编程语言> > javascript-与大多数节点问题相反,插入到couchbase后,node.js永远不会退出

javascript-与大多数节点问题相反,插入到couchbase后,node.js永远不会退出

作者:互联网

我的问题似乎与每个node.js问题相反:-)我有一个简单的forEach循环,可读取文件列表并将其插入Couchbase数据库.这很好用,但是在读取所有行之后它永远不会退出.因此,我添加了一个计数器,以在完成所有插入操作后关闭沙发床连接.这可行.

此过程旨在加载成千上万个文件,因此我将async模块引入了组合中,以将插入的批处理分成100组.async.eachLimit用于遍历数组并分批插入文档.现在,原始问题又回来了.无论async.eachLimit使用什么魔术来识别过程已完成,都不会发生.

我一直在进行JavaScript范围界定,回调,异步等操作.Google搜索在搜索关键字,但没有出现此问题.我将代码简化为以下测试用例.要进行测试,请创建三个文件并将其名称添加到testlist.txt.

正常运行async.eachLimit直到达到极限,然后挂起.注释掉这一行并取消注释array.forEach行,它可以正常工作.提前致谢!

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

var filelist = 'testlist.txt';
var key_count = 0;
var cb_config = { host: 'localhost:8091', bucket: 'default'};
var db = new couchbase.Connection(cb_config, function(err) {
  if (err) {
    console.log('ERRR connect to couchbase at config['+cb_config+']');
    throw err;
  }
});

var insertFile=function(line) {
    console.log('LOAD ['+line+']');

    fs.readFile(line, function(file_err, f_doc) {
        if(file_err) throw file_err;

        db.set(line, f_doc, function(db_err, db_res){
            if (db_err) {
                console.log('FAIL ['+line+'] err['+db_err+']');
            } else {
                console.log('PASS ['+line+']');
            }

            key_count--;
            if (key_count == 0) {
                console.log('DONE Shutting down client, no more keys');
                db.shutdown();
            }
        });
    });
}

// read list of files into data array from file filelist
fs.readFile(filelist, function(filelist_err, lines) {
    if(filelist_err) throw filelist_err;

    // HACK split adds empty line to array, use replace to fix
    var array = lines.toString().replace(/\n$/, '').split('\n');
    key_count = array.length;
    console.log('INIT lines['+key_count+']');

    async.eachLimit(array, 2, insertFile, function(err) { console.log('FAIL async err['+err+']');} );
    //array.forEach(function(data){insertFile(data);return;});
});

使用array.forEach的测试用例输出:

INIT lines[3]
LOAD [files.big.txt]
LOAD [files.little.txt]
LOAD [files.txt]
PASS [files.little.txt]
PASS [files.big.txt]
PASS [files.txt]
DONE Shutting down client, no more keys

使用async.eachLimit输出测试用例:

INIT lines[3]
LOAD [files.big.txt]
LOAD [files.little.txt]
PASS [files.little.txt]
PASS [files.big.txt]
... hang, never gets to 3...

解决方法:

经过与同事的审查,他们发现了我的错误.我错过了我的insertFile函数中的异步回调.将其添加到工作中,可以删除密钥计数器!解决方案代码如下:

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

var filelist = 'testlist.txt';
var key_count = 0;
var cb_config = { host: 'localhost:8091', bucket: 'default'};
var db = new couchbase.Connection(cb_config, function(err) {
  if (err) {
    console.log('ERRR connect to couchbase at config['+cb_config+']');
    throw err;
  }
});

var insertFile=function(line, callback) {
    console.log('LOAD ['+line+']');

    fs.readFile(line, function(file_err, f_doc) {
        if(file_err) throw file_err;

        db.set(line, f_doc, function(db_err, db_res){
            if (db_err) {
                console.log('FAIL ['+line+'] err['+db_err+']');
                callback(db_err);
            } else {
                console.log('PASS ['+line+']');
                callback();
            }
        });
    });
}

// read list of files into data array from file filelist
fs.readFile(filelist, function(filelist_err, data) {
    if(filelist_err) throw filelist_err;

    // HACK stoopid bug split adds empty line to array, use replace to fix
    var array = data.toString().replace(/\n$/, '').split('\n');
    key_count = array.length;
    console.log('READ files['+key_count+']');

    async.eachLimit(array, 2, insertFile, function(err) { 
        if (err) console.log('LAST with async err['+err+']');

        console.log('DONE Shutting down client, no more keys');
        db.shutdown();
    });
});

并成功输出:

$node testcase.js
READ files[3]
LOAD [files.big.txt]
LOAD [files.little.txt]
PASS [files.little.txt]
LOAD [files.txt]
PASS [files.big.txt]
PASS [files.txt]
DONE Shutting down client, no more keys

标签:node-js,asynchronous,javascript,couchbase
来源: https://codeday.me/bug/20191029/1963463.html