Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node-amqp - rejecting message after X attempts

How do I implement mechanism which reject message after few configurable requeue attempts?

In other words, if I'm subscribing to a queue I want to guaranty that same message does not redelivered more then X times.

My code sample:

q.subscribe({ack: true}, function(data,headers,deliveryInfo,message) {
  try{
    doSomething(data);
  } catch(e) {
   message.reject(true);
  }
}
like image 343
shaiis.com Avatar asked Feb 13 '14 16:02

shaiis.com


1 Answers

In my opinion the best solution is to handle these errors in your application and reject them when app has decided that it can't process the message.

If you don't want to lose information, the app should reject the message only after it sent the same message to an error queue.

code is not tested:

q.subscribe({ack: true}, function () {
  var numOfRetries = 0;
  var args = arguments;
  var self = this;
  var promise = doWork.apply(self, args);
  for (var numOfRetries = 0; numOfRetries < MAX_RETRIES; numOfRetries++) {
    promise = promise.fail(function () { return doWork.apply(self, args); });
  }

  promise.fail(function () {
    sendMessageToErrorQueue.apply(self, args);
    rejectMessage.apply(self, args);
  })
})
like image 53
Noam Avatar answered Sep 22 '22 14:09

Noam