简体   繁体   English

带有 pubsub 触发主题的云 Function 不起作用

[英]Cloud Function with pubsub trigger topic is not working

I have wrote a simple code to print data and context from a pubsub trigger cloud function.我编写了一个简单的代码来打印来自 pubsub 触发器云 function 的数据和上下文。

def main(event, context): """Background Cloud Function to be triggered by Pub/Sub. Args: event (dict): The dictionary with data specific to this type of event. The data field contains the PubsubMessage message. The attributes field will contain custom attributes if there are any. context (google.cloud.functions.Context): The Cloud Functions event metadata. The event_id field contains the Pub/Sub message ID. The timestamp field contains the publish time. """ import base64 def main(event, context): """Background Cloud Function 由 Pub/Sub 触发。args: event (dict): 包含特定于此类事件数据的字典。 data字段包含 PubsubMessage 消息。 attributes字段将包含自定义属性(如果有)。context (google.cloud.functions.Context):Cloud Functions 事件元数据event_id字段包含 Pub/Sub 消息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))

Cloud function is deployed successfully but whenever I publish a message to the trigger topic in logs I cannot see any function execution statement.云 function 部署成功,但每当我在日志中向触发器主题发布消息时,我都看不到任何 function 执行语句。

I have already verified that I am calling main function only and publishing to a right pubsub topic.我已经验证我只调用主 function 并发布到正确的 pubsub 主题。

I cannot see any error statement so I am not able to debug.我看不到任何错误语句,因此无法调试。

Any Suggestion will be helpful任何建议都会有所帮助

I tested your code function in python 3.8 runtime and all works fine, are you using the same pub/sub topic for push new messages?我在python 3.8运行时测试了您的代码 function 并且一切正常,您是否使用相同的发布/订阅主题来推送新消息?

This the code that I used on my computer to send pubsub messages.这是我在计算机上用来发送 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要求.txt

google-cloud-pubsub

This is full function code这是完整的 function 代码

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))

Expected output预期 output

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

The logs may appears with a delay of 10-30 secs on stackdriver日志可能会在堆栈驱动程序上延迟 10-30 秒出现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 pubsub 推送触发器运行云 function - Running a cloud function with a pubsub push trigger 订阅PubSub主题的速率限制/限制Google云端功能 - Rate limit / throttle google cloud function that subscribes to a PubSub topic 在PubSub主题上使用Cloud Run - Using Cloud Run on a PubSub topic 将向 PubSub 发布消息的 Cloud Function(由 HTTP 触发) - Cloud Function (trigger by HTTP) that would publish a message to PubSub 在谷歌云中使用 PubSub function - Consume PubSub in Google cloud function 在一个项目中触发 Google Cloud function 从另一个项目发布到 Google PubSub - Trigger Google Cloud function in one project on publish in Google PubSub from another project 使用 gcloud 命令部署事件弧触发第二代云 function 时如何指定 pubsub 主题 - How to specify pubsub topic when deploying event arc triggered 2nd gen cloud function using gcloud command 超出速率限制:将数据从 pubsub 主题上传到 BQ 时,云 Function 中此表的表更新操作过多 - Exceeded rate limits: too many table update operations for this table in Cloud Function while uploading the data from pubsub topic to BQ 有没有办法为spring-cloud-stream-pubsub中的主题定义TTL? - Is there a way to define a TTL for an topic in spring-cloud-stream-pubsub? 在Compute Engine中使用Google Cloud Pubsub发布到主题 - Publish to a topic using Google Cloud Pubsub within Compute Engine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM