我已经从Pub/Sub主题创建了Cloud Function,如果函数失败,是否应该重试从pub/sub传递消息? [英] I've created a Cloud Function from Pub/Sub topic, shall I expect retry a delivery of message from pub/sub if function fails?

本文介绍了我已经从Pub/Sub主题创建了Cloud Function,如果函数失败,是否应该重试从pub/sub传递消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有简单测试代码的函数,例如:

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");
    };

当我向发布/订阅发布消息时,该函数失败,并且由于发布/订阅订阅的确认截止时间设置为600秒,因此我希望它在600秒后再次以相同的消息被调用.但是,尽管云功能失败,它仍无法像预期的那样立即响应消息.

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.

根据文档:云函数在函数成功执行后会在内部对消息进行确认.

推荐答案

您必须确保已启用该函数的重试.从"重试背景函数"文档(重点是我的):

You must make sure that you have enabled retries on the function. From the "Retrying Background Functions" documentation (emphasis mine):

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

Cloud Functions guarantees at-least-once execution of a background function for each event emitted by an event source. However, by default, if a function invocation terminates with an error, the function will not be invoked again, and the event will be dropped. When you enable retries on a background function, Cloud Functions will retry a failed function invocation until it completes successfully, or the retry window (by default, 7 days) expires.

在通过gcloud命令行工具创建功能或检查失败时重试"功能时,您可以通过提供-retry 选项来启用重试.通过云控制台创建"框.

You can enable retries by providing the --retry option when creating the function via the gcloud command-line tool or checking the "Retry on failure" box when creating via the Cloud Console.

当Cloud Function失败并启用重试时,它将对消息进行锁定,从而使消息成为重新交付的候选者.要延迟重新发送被拒绝的邮件,您可以设置 RetryPolicy .要将订阅更新为使用重试策略,请在Cloud Console发布/订阅"部分中找到由Cloud Functions创建的订阅的名称.然后,例如,您可以发出以下 gcloud 命令将最小重试延迟设置为1秒,最大延迟设置为60秒:

When a Cloud Function fails and retry is enabled, it nacks the message, which makes the message a candidate for redelivery. To delay the redelivery of nacked messages, you can set up a RetryPolicy on the subscription. To update the subscription to use a retry policy, find the name of the subscription created by Cloud Functions in the Cloud Console Pub/Sub section. Then, for example, you can issue this gcloud command to set the minimum retry delay to 1 second and the maximum delay to 60 seconds:

gcloud pubsub subscriptions update <subscription name> --min-retry-delay=1s --max-retry-delay=60s

这篇关于我已经从Pub/Sub主题创建了Cloud Function,如果函数失败,是否应该重试从pub/sub传递消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆