Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pubsub triggered cloud function not ack the message, even after func executes successfully

I have this simple python function where i am just taking the input from pubsub topic and then print it.

import base64,json

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.
    """
    pubsub_message = base64.b64decode(event['data'])
    data           = json.loads(pubsub_message)
    for i in data:
         for k,v in i.items():
              print(k,v)

If i had used the pubsub_v1 library, there i could do following.

subscriber = pubsub_v1.SubscriberClient()

def callback(message):
        message.ack()

subscriber.subscribe(subscription_path, callback=callback)

How do i ack the message in pubsub triggered function?

like image 415
akrsha Avatar asked Feb 28 '26 06:02

akrsha


1 Answers

Following your latest message, I understood the (common) mistake. With Pubsub, you have

  • a topic, and the publishers can publish messages in it
  • (push or pull) subscriptions. All the messages published in the topic are duplicated in each subscription. The message queue belong to each subscription.

Now, if you look closely to your subscriptions on your topic, you will have at least 2.

  • The pull subscription that you have created.
  • A push subscription created automatically when you deployed your Cloud Function on the topic.

The messages of the push subscription is correctly processed and acknowledge. However those of pull subscription aren't, because the Cloud Function don't consume and acknowledge them; the subscription are independent.

So, your Cloud Function code is correct!

like image 182
guillaume blaquiere Avatar answered Mar 03 '26 09:03

guillaume blaquiere