0
votes

I have a function with a simple test code like:

    exports.helloPubSub = (event, context) => {
      const message = event.data
         ? Buffer.from(event.data, 'base64').toString()
         : 'Hello, World';
      console.log(context);
      throw new Error("Fail");
    };

When I publish a message to Pub/sub the function fails and I expect that it will be called again with the same message after 600 seconds since the pub/sub subscription has Acknowledgement deadline set to 600 Seconds. But it does not work as expected looks like it acks the message immediately despite the failure in a cloud function.

According to the doc: Cloud Functions acks the message internally upon successful function execution.

1

1 Answers

4
votes

You must make sure that you have enabled retries on the function. From the "Retrying Background Functions" documentation (emphasis mine):

Cloud Functions guarantees at-least-once execution of a background function for each event emitted by an event source. However, by default, if a function invocation terminates with an error, the function will not be invoked again, and the event will be dropped. When you enable retries on a background function, Cloud Functions will retry a failed function invocation until it completes successfully, or the retry window (by default, 7 days) expires.

You can enable retries by providing the --retry option when creating the function via the gcloud command-line tool or checking the "Retry on failure" box when creating via the Cloud Console.

When a Cloud Function fails and retry is enabled, it nacks the message, which makes the message a candidate for redelivery. To delay the redelivery of nacked messages, you can set up a RetryPolicy on the subscription. To update the subscription to use a retry policy, find the name of the subscription created by Cloud Functions in the Cloud Console Pub/Sub section. Then, for example, you can issue this gcloud command to set the minimum retry delay to 1 second and the maximum delay to 60 seconds:

gcloud pubsub subscriptions update <subscription name> --min-retry-delay=1s --max-retry-delay=60s