postwait / node-amqp

[UNMAINTAINED] node-amqp is an AMQP client for nodejs
MIT License
1.69k stars 357 forks source link

publish() not calling callback during/after reconnect #373

Open tcort opened 9 years ago

tcort commented 9 years ago

I have some code that looks something like this. It publishes 1 message per second. It waits until the prior message has been published before publishing a new message.

(function sendMessage() {
        var msg = msgs.shift();

        console.log('Sending %d', msg.id);
        exchange.publish(routingKey, msg, options, function (err) {
                if (err) {
                        console.log('Failed to send %d, requeueing', msg.id);
                        msgs.unshift(msg);
                } else {
                        console.log('Sent %d', msg.id);
                }
                setTimeout(sendMessage, 1000);
        });
})();

The exchange is durable, non-autoDelete, topic type with confirm turned on. When I run the above code and I send while the connection is down, I never get a callback.

Connected
Sending 0
Sent 0
Sending 1
Sent 1
Error: CONNECTION_FORCED - broker forced connection closure with reason 'shutdown', will retry
Error: connect ECONNREFUSED
Sending 2
Error: connect ECONNREFUSED
Error: connect ECONNREFUSED
Error: connect ECONNREFUSED
Connected

I am expecting the callback to be called when the connection is reestablished and the message is sent -- either that or I would expect a publication error (because the connection is down). Instead, there is no callback. With the debugging output turned on, I see that it does a basicPublish and gets a basicAck after the reconnect, but the callback isn't called.

Environments:

YEXINGZHE54 commented 9 years ago

the same problem, I wish to be notified if I pubilish a message when connection is broken. Instead, connection.on('error') seems to catch the problem, but, how could I know which message is not sent. Or, can publish task restarted after connection reestablished?

eguzki commented 9 years ago

Same issue. Looking forward to having it fixed. I would expect that if connection is closed (as close event notifies), publish method's callback is called with err set to true. Or at least, being able to check connection status.

barshow commented 9 years ago

This is solved on https://github.com/dropbox/amqp-coffee which is based off node-amqp but has a slightly different api

andre1810 commented 9 years ago

+1

iBorodai commented 9 years ago

And me too! Somebody, are you here? We all need help about this issue :)

iBorodai commented 9 years ago

@tcort in your case, you should pass "confirm" option while declaring exchange https://github.com/postwait/node-amqp#exchange so, somthing like this:

connection.exchange( '< someName> ', { autoDelete:false, ... , confirm:true }, function onExchange(){...} );

I think, it should hуlp:)

skysteve commented 9 years ago

+1