正确嵌套Redis回调

时间:2012-08-20 21:41:36

标签: javascript node.js redis snmp node-redis

我正在编写一个node.js程序,它接收snmp陷阱并将它们存储在redis哈希数据库中。我还没有在Node.js中编程很长时间,我也不认为我完全理解我的代码是如何执行的。

我觉得我的一些redis调用正在其他人之前完成,这就是为什么我尝试在回调函数中嵌套所有内容但我担心我没有以正确的方式执行此操作。

我的输出看起来好像接近我想要达到的目标,但是有一些随机的问题,比如我的第一个陷阱我的第一代#是1,然后是下一次11或111。我所做的就是对整数执行redis client.incr函数。

为了简单明了,我使用两个哈希键来跟踪我收到的当前和过去的陷阱。我将两个数字生成和散列数跟踪为单独的整数键值,并根据需要递增它们。这两个数字用于创建和访问当前和过去陷阱哈希值。

这是我的代码。

function alarmCheck (key, field, value, alarmType, ipAddress) {
var historyKey = "History:"+key;
//var generationNumber;
//var numberInHash;

client.get(ipAddress+":"+field+":Gen", function (err, rep) {
    //generationNumber = rep;
    var generationNumber = rep;
    console.log("The Gen: "+rep);
    client.get(ipAddress+":"+field+":Field", function (err, reply) {

        var numberInHash = reply;
        //numberInHash = reply;
        console.log("The Field: "+reply);

        console.log("ALARM TYPE: "+alarmType);
        if(alarmType == 1) //Alarm Start value is 1
        {
            fullCurrKey = "Current:" + key;
            fullField = field + ":" + generationNumber + ":" + numberInHash;
            console.log("The FULL Field: "+fullField);
            client.hsetnx(fullCurrKey, fullField, value, function (err, status) {
                if(status == 1)
                {
                    console.log("ADDED to Current!");
                    client.incr(ipAddress+":"+field+":Field", redis.print);
                }
            })
    } else //If Alarm Start value is 0 or 2 
        {
            fullCurrKey = "Current:" + key;
            fullHistKey = "History:" + key;
            console.log("Loop generationNumber: "+generationNumber);
            console.log("Loop numberInHash: "+numberInHash);
            loop1:
            for(var i=1;i<=generationNumber;i++)
            {
                loop2:
                for(var j=1;j<=numberInHash;j++)
                {
                    fullField = field + ":" + i + ":" + j;
                    console.log("Alarm 0 or 2 fullField: "+fullField);
                    client.hget(fullCurrKey, fullField, function (err, reply) {
                        var theField = fullField;
                        if(reply == null)
                        {
                            console.log("Null hget!");
                        }
                        else
                        {
                            console.log("Adding to history!");
                            console.log("The loop hget reply: "+reply);
                            console.log("The loop hget fullField: "+fullField);
                            console.log("The loop hget theField: "+theField);
                            client.hset(fullHistKey, theField, reply, redis.print);
                            //break loop1;
                        }
                    });
                }
            }
            client.set(ipAddress+":"+field+":Field", 1, function (err, reply) {
                client.incr(ipAddress+":"+field+":Gen", function (err, reply) {
                    client.hset(fullHistKey, field+":"+generationNumber+":"+(numberInHash+1), value, function (err, reply) {
                        if (err)
                        {
                            console.log("Fail :( "+err);
                        }
                    });
                }); 
            });
                    }
    });
});
}

以下是我当前的一些输出。

1) "Composite Gamut Error:1:1"
2) "Composite Gamut Error( -c ) 1345493146"
3) "Luma Gamut Error:1:1"
4) "Luma Gamut Error( -l ) 1345493146"
5) "Jitter1 Level:1:1"
6) "Jitter1 Level 1345493146"
7) "RGB Gamut Error:1:1"
8) "RGB Gamut Error( Rr-gBb ) 1345493146"
9) "Composite Gamut Error:1:2"
10) "Composite Gamut Error( Cc ) 1345493147"
11) "Luma Gamut Error:1:2"
12) "Luma Gamut Error( Ll ) 1345493147"
13) "RGB Gamut Error:1:2"
14) "RGB Gamut Error( R--gBb ) 1345493147"
15) "SDI Input Signal Lock:1:1"
16) "SDI Input Signal Lock( Unlocked ) 1345493147"
17) "Y Anc Checksum Error:3:1"
18) "Y Anc Checksum Error( Error ) 1345493147"
19) "SDI Input Signal Lock:2:1"
20) "SDI Input Signal Lock( Unlocked ) 1345493147"
21) "Line Length Error:4:1"
22) "Line Length Error( Error ) 1345493147"
23) "SAV Place Error:4:1"
24) "SAV Place Error( Error ) 1345493147"
25) "AP CRC Error:3:1"
26) "AP CRC Error( Invalid ) 1345493147"
27) "FF CRC Error:3:1"
28) "FF CRC Error( Invalid ) 1345493147"
29) "EDH Error:3:1"
30) "EDH Error( Invalid ) 1345493147"

以下是我的一些历史输出。

1) "Line Length Error:1:11"
2) "Line Length Error( Error ) 1345493147"
3) "EAV Place Error:1:11"
4) "EAV Place Error( Error ) 1345493147"
5) "SAV Place Error:1:11"
6) "SAV Place Error( Error ) 1345493147"
7) "Composite Gamut Error:1:3"
8) "Composite Gamut Error( Cc ) 1345493147"
9) "Composite Gamut Error:1:31"
10) "Composite Gamut Error 1345493147"
11) "Luma Gamut Error:1:3"
12) "Luma Gamut Error( Ll ) 1345493147"
13) "Luma Gamut Error:1:31"
14) "Luma Gamut Error 1345493147"
15) "Y Anc Checksum Error:1:11"
16) "Y Anc Checksum Error( Error ) 1345493147"
17) "RGB Gamut Error:1:3"
18) "RGB Gamut Error( R--gBb ) 1345493147"
19) "RGB Gamut Error:1:31"
20) "RGB Gamut Error 1345493147"
21) "Y Anc Checksum Error:2:11"
22) "Y Anc Checksum Error( Error ) 1345493147"
23) "Line Length Error:2:11"
24) "Line Length Error( Error ) 1345493147"
25) "Field Length Error:1:11"
26) "Field Length Error( Error ) 1345493147"
27) "SAV Place Error:2:11"
28) "SAV Place Error( Error ) 1345493147"
29) "AP CRC Error:1:11"
30) "AP CRC Error( Invalid ) 1345493147"
31) "FF CRC Error:1:11"
32) "FF CRC Error( Invalid ) 1345493147"
33) "EDH Error:1:11"
34) "EDH Error( Invalid ) 1345493147"

1 个答案:

答案 0 :(得分:2)

我强烈推荐async waterfall。它将为您的代码带来更多的结构和控制,并减少嵌套的回调。

async.waterfall([
  function(callback){
    redis.get('abc', function(error, result) {
      callback(error, result);
    });
  },
  function(firstResult, callback){
    redis.set('abc', firstResult, function(error, result) {
      callback(error, result);
    });
  }
], function (err, result) {
   // do something to finish operations
});