1
votes

I have a Google cloud function that is being triggered by a Pubsub push subscription.

I wish to know the current delivery attempt of the given message.

In pull subscription it works by setting dead letter topic, however I am not able to get the delivery attempt in a push subscription message attributes. Tried to configure a dead letter topic and delivery_attempt attribute is not in message attributes.

Is there a way to get the delivery attempt parameter in a push subscription?

3
How many time the message is presented to the functions? Only one? Several time?guillaume blaquiere

3 Answers

1
votes

For push subscriptions, use deliveryAttempt, not delivery_attempt. The documentation calls out this here:

When Pub/Sub forwards undeliverable messages from a push subscription, every message you receive from the subscription includes the deliveryAttempt field.

0
votes

I am not sure it is possible to get that data explicitly... but I can suggest an idea of a workaround for consideration.

Every time a cloud function is invoked, you can (in your code) create/update a firestore document with some unique id (either a message event id, or some unique business related identifier). That document may have attributes, one of which - is the time and/or the attempt number.

0
votes

in java you can use a static method of Subscriber class.

for a PubsubMessage message;

com.google.cloud.pubsub.v1.Subscriber.getDeliveryAttempt(message);

As de documentation sais Returns the delivery attempt count for a received PubsubMessage.

Integer maxAttemps = 5;
     private void killMessage(Event<String> event, Exception en,
      BasicAcknowledgeablePubsubMessage acknowledgeable) {
    Integer attemps = Subscriber.getDeliveryAttempt(acknowledgeable.getPubsubMessage());
    if(attemps.intValue() < maxAttemps) {
      acknowledgeable.nack();
      return;
    }
    sendToErrorQueue(event, en, acknowledgeable);
  }

https://googleapis.dev/java/google-cloud-pubsub/latest/com/google/cloud/pubsub/v1/Subscriber.html