hunterloftis / jackrabbit

Simple AMQP / RabbitMQ job queues for node based on amqplib
293 stars 65 forks source link

Is there anyway to convert queue.publish() into a promise? #45

Closed naartjie closed 8 years ago

naartjie commented 8 years ago

I was wondering if there is any way of knowing if the publish() fails/succeeds. I had an issue where I deployed to production and a queue hadn't been created yet (I created it on the consumer), and it was failing silently.

This might be related to #3, but I just wanted to check if anything has changed on this?

stephengardner commented 8 years ago

I ran into wanting to promisify more of the Queue functions, modularize them, and allow them to be re-used. While this might not be the best answer to your question I created individual modules for my Queues around the jackrabbit instance. Heres a very cliffnotesey idea of what I did.

        var QueueConnection = // the jackrabbit connection,
        key = // the key;
    var Queue = {
        queueConnection : QueueConnection ,
        key : key,
        attach : function() {
            return new Promise(function(resolve, reject){
                Queue.queueConnection.create(Queue.key, {prefetch : 1}, function(){
                    resolve(true);
                }.bind(Queue));
            }.bind(Queue));
        },
                publish : function() {
                ......
                }
        }

etc, etc.

The attach method will help your prior problem. And you can wrap the synchronous .publish() method in a promise too if you'd like, there's nothing wrong with that

naartjie commented 8 years ago

Thanks for sharing this @stephengardner, I haven't had time to try it out. I got pulled off onto other things, and since my initial implementation seems to be solid, I haven't been pulled back to improve on it.

I'm going to close the issue for now. It would be nice to have a promisified version to get delivery guarantees, but I don't know enough about AMQP to contribute and help out here.