node-amqp-connection-manager icon indicating copy to clipboard operation
node-amqp-connection-manager copied to clipboard

Way of checking if all message are sent out

Open ssukienn opened this issue 4 years ago • 2 comments

Hi.

I wonder if there is some approach using this lib to check whether all messages published on the channel/connection were already sent out. In other words, if message queues are empty. And if we can await it.

Thanks

ssukienn avatar Feb 16 '22 12:02 ssukienn

publish() returns a promise which will resolve when the message is sent, so if you could do something like:

const promises = [];
promises.push(chan.publish(...));
promises.push(chan.publish(...));
promises.push(chan.publish(...));

// Wait until all messages are sent, or one message has failed
await Promise.all(promises);

jwalton avatar Feb 16 '22 14:02 jwalton

Oh, I get that, I could describe context better. I wonder if that would be possible from the library side to expose its promises or some behavior that would expose it more like executor queue?

My scenario is that I could emit messages from different not interconnected places so it would be hard for me to capture all of msg promises. But I would have access to connection/channel. Let's assume that the application is handling lots of "parallel" logic during which it publishes messages to the connection manager. Then there comes a point in time that I decide that I am not expecting to send any more messages (or they are not relevant for me): a) and the first time the manager queue is empty I would like to block on it or be notified (ofc it might never be empty, so I would block indefinitely or with some timeout?). b) more advanced mechanism would be not allowing the new messages to be published to queue but I am still very much interested to send all that are still there without rejecting them

It is a little similar concept to ThreadExecutor in Java.

Personally, I am more interested with a) as I actually know that I won't block indefinitely in my scenario but probably something more versatile would be more useful.

ssukienn avatar Feb 16 '22 16:02 ssukienn