如何使用 Google PubSub (@google-cloud/pubsub) 确认

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

使用这个例子:https://cloud.google.com/nodejs/getting-started/using-pub-sub

完全按照示例,消息(成功和失败)都不会被确认,Google 甚至没有在页面上提到 "acknowledge" 这个词。所以他们被重试并且无法扩展。没有文档的完整拦截器。

使用时如何在 Google Pub Sub 中确认消息:

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

我建议您看看这个 quickstart。基本上,它在 messageHandler.

中带有 message.ack()

Google 的 documented example 显示:

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

...从不 acks()。因此 google 示例将始终重新运行所有任务。

更改为:

// 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();
  }
}

...这样做允许处理函数在需要的地方进行 ack()。