I've created a Cloud Function from Pub/Sub topic, shall I expect retry a delivery of message from pub/sub if function fails?

Gre*_*gTk 3 publish-subscribe google-cloud-platform google-cloud-pubsub google-cloud-functions

I have a function with a simple test code like:

    exports.helloPubSub = (event, context) => {
      const message = event.data
         ? Buffer.from(event.data, 'base64').toString()
         : 'Hello, World';
      console.log(context);
      throw new Error("Fail");
    };
Run Code Online (Sandbox Code Playgroud)

When I publish a message to Pub/sub the function fails and I expect that it will be called again with the same message after 600 seconds since the pub/sub subscription has Acknowledgement deadline set to 600 Seconds. But it does not work as expected looks like it acks the message immediately despite the failure in a cloud function.

According to the doc: Cloud Functions acks the message internally upon successful function execution.

Kam*_*osn 6

您必须确保已启用该功能的重试。来自“重试后台功能”文档(重点是我的):

Cloud Functions 确保对于事件源发出的每个事件至少执行一次后台函数。但是,默认情况下,如果函数调用因错误而终止,则不会再次调用该函数,并且事件将被删除。当您对后台函数启用重试时,Cloud Functions 将重试失败的函数调用,直到成功完成或重试窗口(默认为 7 天)到期。

--retry您可以通过在通过 gcloud 命令行工具创建函数时提供选项或在通过 Cloud Console 创建函数时选中“失败重试”框来启用重试。

当云功能失败并启用重试时,它会确认该消息,这使得该消息成为重新传递的候选者。要延迟重新传递 nacked 消息,您可以在订阅上设置 RetryPolicy 要更新订阅以使用重试策略,请在 Cloud Console Pub/Sub 部分中找到 Cloud Functions 创建的订阅的名称。然后,例如,您可以发出以下gcloud命令将最小重试延迟设置为 1 秒,最大延迟设置为 60 秒:

gcloud pubsub subscriptions update <subscription name> --min-retry-delay=1s --max-retry-delay=60s
Run Code Online (Sandbox Code Playgroud)