2
votes

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

enter image description here

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. enter image description here

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.

1

1 Answers

5
votes

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

enter image description here

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:

  • Your Cloud Functions can't ack the messages in the pull subscription, because it is not connected to it
  • Your Cloud Functions process and ack messages published in its own subscription.