Base on official document
I try to create a cloud function with "PubSub Pull Subscription" trigger
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'])
Then I find an article says that "cloud function will send ACK on its invocation", which is consistent with the official document.
However, when the cloud function finishes processing the PubSub message, "Unacked message count" do not decrease (as shown in the image above)
Hence, I try google-cloud-pubsub on local
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)
In this way, the number of message count decrease successfully.
My questions are:
- Am I missing something in my cloud function script?
- How can I actually "consume" the PubSub message in my cloud function?
Any suggestion is appreciated, thank you.