GCP云功能何时确认发布/订阅消息?

Dan*_*att 5 google-cloud-pubsub google-cloud-functions

我有一个从发布/订阅消息触发的云函数。该函数从不明确确认源代码中的消息。

那么,如果源代码中从未发生过确认,​​那么该函数何时确认发布/订阅消息呢?

更新:当函数崩溃时,我知道不应发生消息确认,但该消息的新函数调用永远不会出现在日志中

可重复的例子

  • 创建一个名为test_topic的 pubsub 主题

  • 创建一个名为test_function的云函数,并触发 test_topic。给它所有默认设置,包括失败时不重试。在代码本身中,将语言设置为python3.7,入口点为hello_pubsub并添加以下代码:

     import base64
     def hello_pubsub(event, context):
         pubsub_message = base64.b64decode(event['data']).decode('utf-8')
         print(pubsub_message)
         raise RuntimeError('error in function')
    
    Run Code Online (Sandbox Code Playgroud)
  • requirements.txt保持空白

  • 进入 test_topic 并发布一条以go作为文本的消息。

  • test_function 日志中将会出现错误。然而,只有一次函数调用会出现错误,并且即使在几天左右之后这种情况仍然会存在。

gui*_*ere 4

如果函数正常完成,则消息被确认。如果函数错误退出,则消息为 NACK。


编辑1

我已经用Go后台功能进行了测试。您需要使用该参数部署云函数,--retry以允许重试错误的消息。否则,不会重试消息。

在 Go 中,执行重试的情况如下:

  • 返回错误(相当于 Java 或 Python 中的异常),日志中状态为“error”
  • 执行log.Fatal()(退出带有特定日志的函数(函数崩溃))日志中的状态“连接错误”
  • 执行显式退出,日志中状态为“连接错误”

这里是代码(如果有兴趣)

type PubSubMessage struct {
    Data []byte `json:"data"`

}

func PubsubError(ctx context.Context, m PubSubMessage) error {

    switch string(m.Data) {
    case "ok":
        return nil
    case "error":
        return errors.New("it's an error")
    case "fatal":
        log.Fatal("crash")
    case "exit":
        os.Exit(1)
    }
    return nil
}
Run Code Online (Sandbox Code Playgroud)

以及我如何部署我的云功能

gcloud beta functions deploy --runtime=go113 --trigger-topic=test-topic \
  --source=function --entry-point=PubsubError --region=us-central1 \
  --retry pubsuberror
Run Code Online (Sandbox Code Playgroud)