简体   繁体   中英

Proper placement of callback with multiple async redis calls in node.js

I'm building a job queue system with node.js and redis and am trying to figure out how best to implement the callback into this function.

In the code I have cb() called 3 times simply for emphasis of the redis calls I'm talking about.

Obviously I could just nest the three calls (rpush -> sadd -> sadd), but given they aren't dependent on each other, that kind of defeats the purpose of async processing, does it not?

Queue.prototype.pushJob = function(job, cb) {
    var that = this;
    cb = cb || function(err, res) {};
    if (job.name) {
        that.Database.incr("id:jobs", function(id) { //Increment redis variable id:jobs
            that.Database.client.hmset('job:' + id, { //Callback of incr, set the hash of job:incr
                "name": job.name,
                "status": job.status,
                "payload": job.payload,
                "priority": job.priority
            }, function() { //Callback of hmset, add incr to jobs list
                that.Database.client.rpush('jobs', id, function(err, res) { //Add id to jobs array
                    cb(err, res); //Callback
                });

                that.Database.client.sadd('jobs.status.' + job.status, id, function(err, res) { //Add status to type array
                    cb(err, res); //Callback
                });

                that.Database.client.sadd('jobs.name.' + job.name, id, function(err, res) { //Add status to type array
                    cb(err, res); //Callback
                });
            });
        });
        return true;
    }
    console.log("Invalid data passed to Job");
    cb(null);
    return false;
};

Really what it comes down to is this is my first time using redis and am still working on wrapping my brain around some of its functionality.

In order for the hmset, rpush, and sadd functions to know which ID they are pushing to, they have to be within the callback of the incr call, but after that, I'm not sure what the best way to structure my code is. Any help would be apreciated!

I think the easiest would be to use a Multi object:

that.Database.incr("id:jobs", function(id) { 
  var multi = that.Database.client.multi();
  multi.hmset('job:' + id, { ... });
  multi.rpush('jobs', id);
  multi.sadd('jobs.status.' + job.status, id);
  multi.sadd('jobs.name.' + job.name, id);
  multi.exec(cb);
  // or a bit less explicit:
  //
  // that.Database.client.multi()
  //                     .hmset('job:' + id, { ... })
  //                     .rpush('jobs', id)
  //                     .sadd('jobs.status.' + job.status, id)
  //                     .sadd('jobs.name.' + job.name, id)
  //                     .exec(cb);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM