Robert Christian
Robert Christian

Reputation: 18310

How to ACK using Google PubSub (@google-cloud/pubsub)

Using this example: https://cloud.google.com/nodejs/getting-started/using-pub-sub

Following the example exactly, messages (both success and fail) are not acknowledged, and Google does not even mention the word "acknowledge" on the page. So they are retried and it's impossible to scale. Complete blocker with no documentation.

How do you ACK messages in Google Pub Sub when using:

const Pubsub = require('@google-cloud/pubsub');

Upvotes: 2

Views: 2768

Answers (2)

Robert Christian
Robert Christian

Reputation: 18310

Google's documented example shows:

// Event handlers
function handleMessage (message) {
  const data = JSON.parse(message.data);
  cb(null, data);
}

... which never acks(). So the google example as is will always rerun all tasks.

Changed to:

// Event handlers
function handleMessage(message) {
  try {
    cb(null, message);   
  } catch (err) {
    console.log('Failed to handle message with data: [' + message.data + '] - Removing.  Err: ' +  err.toString());
    message.ack();
  }
}

... doing it this way allows the processing function to ack() where needed.

Upvotes: 0

Guillem Xercavins
Guillem Xercavins

Reputation: 7058

I would recommend taking a look at this quickstart. Basically, it's with message.ack() in the messageHandler.

Upvotes: 4

Related Questions