Friday 9 June 2017

amqp callback confirm: true not working

I'm new to NodeJS and we are using amqp 0.2.6. I have psuedocode that looks like

async.waterfall([
    function(callback) {
         // do logic and pass parameters to next method by calling callback
    },

    function(params, callback) {
         // do AMQP logic and pass parameters to next method
            try {       
          bus.publish(routingKey, message, options, function (err) {
                 console.log("got here");
                 if (err) {
                      console.log(err);
                 }
          });
        //callback(null, null);    
    } catch (err) {
        console.log("ERROR: " + err);
        //callback(err, null);
    }


    }
], function() {
        // do logic with all results
});

So I have a couple questions. First, is there something wrong with my callback when I call publish? My message gets sent to the Rabbit properly but I never get to the callback got here message.

If I uncomment the two lines that start with callback, then my second function in async.waterfall gets its callback right away and then calls its callback to pass the results to the final function in async.waterfall. I do understand that this is happening since the callback gets called right after publish. What I'd like to do is ideally call the callback after I know if my message sent to rabbit succeeds with no error or with an error. I don't know how I would do this since I cannot get to the callback of the amqp publish message. I do see in my try/catch block if there is an error, I see my error logged, but this is after my async.waterfall function has already finished. I'd like to pass the results of my publish message (success or failure) to the second function in async.waterfall. Any non-blocking way I can achieve this is fine. So in the end the goal is

async.waterfall([
    function (one) {
         // do logic and pass results to 'two'
    }, 
    function (two) {
         // do AMQP logic, send a message, get the results of that action and pass to last function
    }
], function() {
        // i got my results from 'one' and 'two', do some more logic
});



via Crystal

No comments:

Post a Comment