Reputation: 429
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:
Any suggestion is appreciated, thank you.
Upvotes: 5
Views: 13299
Reputation: 75990
With PubSub, you have publisher, that publish messages into a topic. The message is duplicated in each existing subscription (created on a topic). At the end, subscribers can listen subscription.
So, here, you have have 1 topic, and 1 pull subscription. You also have a Cloud Functions that you deploy on a topic (in gcloud cli, param --trigger-topic=myTopic
). ON A TOPIC, not on a subscription.
Go back to the subscription page, you should see 2 subscriptions: Your pull subscription, and a push subscription to a strange endpoint
Therefore, your message is published in the 2 subscriptions. If you look your Pull subscription, nothing consume the message in it, except your code on local. The logs in the cloud functions should show the correct message processing.
Is it clearer?
EDIT
To be precise on your case:
Upvotes: 9