具有pubsub触发器主题的Cloud Function无法正常工作

问题描述

我编写了一个简单的代码来从pubsub触发器云函数中打印数据和上下文。

def main(事件,上下文): “ /”要由发布/订阅触发的背景云功能。 精氨酸: 事件(字典):包含特定于此类数据的数据的字典 事件。 data字段包含PubsubMessage消息。的 attributes字段将包含自定义属性(如果有)。 上下文(google.cloud.functions.Context):Cloud Functions事件 元数据。 event_id字段包含发布/订阅消息ID。的 timestamp字段包含发布时间。 “” 导入base64

print("""This Function was triggered by messageId {} published at {}
""".format(context.event_id,context.timestamp))

if 'data' in event:
    name = base64.b64decode(event['data']).decode('utf-8')
else:
    name = 'World'
print('Hello {}!'.format(name))

云功能已成功部署,但是每当我在日志中将消息发布到触发主题时,我都看不到任何功能执行语句。

我已经确认我仅在调用main函数并发布到正确的pubsub主题。

我看不到任何错误声明,因此无法调试。

任何建议都会有帮助

解决方法

我在python 3.8运行时中测试了您的代码功能,并且一切正常,您是否使用相同的发布/订阅主题发送新消息?

这是我在计算机上用来发送pubsub消息的代码。

from google.cloud import pubsub_v1

publisher = pubsub_v1.PublisherClient()
# The `topic_path` method creates a fully qualified identifier
# in the form `projects/{project_id}/topics/{topic_id}`
topic_path = publisher.topic_path("myprojectID","test")

for n in range(1,10):
    data = u"Message number {}".format(n)
    # Data must be a bytestring
    data = data.encode("utf-8")
    # When you publish a message,the client returns a future.
    future = publisher.publish(topic_path,data=data)
    print(future.result())

print("Published messages.")

requirements.txt

google-cloud-pubsub

这是完整的功能代码

import base64

def hello_pubsub(event,context):
     print("""This Function was triggered by messageId {} published at {}
     """.format(context.event_id,context.timestamp))

     if 'data' in event:
          name = base64.b64decode(event['data']).decode('utf-8')
     else:
          name = 'World'
     print('Hello {}!'.format(name))

预期产量

This Function was triggered by messageId 1449686821351887 published at 2020-08-20T21:26:30.600Z

在堆栈驱动程序上,日志可能会出现10到30秒的延迟

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...