1

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

我觉得我的一些 redis 调用比其他调用更早完成,这就是为什么我尝试将所有内容嵌套在它们的回调函数中,但我担心我没有以正确的方式这样做。

我的输出看起来已经接近我想要实现的目标,但是会弹出一些随机问题,例如我的第一个陷阱为 1,下一次为 11 或 111。我所做的只是client.incr对整数执行 redis 函数。

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

这是我的代码。

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"
4

1 回答 1

2

我可以为此强烈推荐异步瀑布。它将为您的代码带来更多结构和控制,并减少嵌套回调。

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
});
于 2012-08-23T08:54:00.517 回答