以官方文档为准
我尝试使用“PubSub Pull Subscription”触发器创建云函数
import base64
def hello_pubsub(event, context):
"""Triggered from a message on a Cloud Pub/Sub topic.
Args:
event (dict): Event payload.
context (google.cloud.functions.Context): Metadata for the event.
"""
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')
print('"{}" received!'.format(name))
if 'attributes' in event:
print(event['attributes'])
if '@type' in event:
print(event['@type'])
然后找到一篇文章说“cloud function will send ACK on its invocation”,和官方文档是一致的。
但是,当云函数完成对 PubSub 消息的处理后,“Unacked message count”并没有减少(如上图所示)
因此,我在本地尝试google-cloud-pubsub
subscription_path = subscriber.subscription_path(PROJECT, SUBSCRIPTION)
response = subscriber.pull(subscription_path, max_messages=5)
for msg in response.received_messages:
print("Received message:", msg.message.data)
ack_ids = [msg.ack_id for msg in response.received_messages]
subscriber.acknowledge(subscription_path, ack_ids)
这样,消息计数成功减少。
我的问题是:
我的云函数脚本中是否缺少某些内容?
我怎样才能在我的云函数中实际“使用”PubSub 消息?
任何建议表示赞赏,谢谢。
互换的青春
相关分类