Google Cloud Async Processing return 200

220 views Asked by At

I am using a google cloud function as a webhook to receive a payload from a 3rd party service. Typically when such services make requests to webhooks they expect a HTTP 200 as acknowledgement. However with a cloud function setup as below (and as recommended by google) the requesting service is returned a 408 when ongoing processing is happening.

In this situation the 3rd party service (in this case its cloudmailin but the same applies for any webhook I have attempted to integrate with) will retry the request even though it has been handled successfully.

My question is: how can I get the cloud function to return a 200 and still continue the async processing?

//This will return a 408 even though the request is processed successfully
exports.emailIngest = functions.https.onRequest((request, response) => 
{
    //return the promise from the firestore admin SDK as per google docs
    return admin.auth().getUserByEmail(request.body.envelope.from).then((user) => {
      console.log('Successfully fetched user data:', user.toJSON());
    }).catch(function (error) {
      console.log('Error fetching user data:', error);
    });
});
1

There are 1 answers

2
Doug Stevenson On

Since HTTP triggers are effectively terminated and cleaned up after they send a response, you won't be able to continue async work in the function after the response is sent. What you'll have to do is have the HTTP function delegate work to some other background function that continues outside of the scope of the HTTP request.

One way to do that is to use a pubsub trigger. The HTTP trigger can broadcast a pubsub message, which then causes the pubsub trigger to execute. Pubsub requires billing to be enabled for the project.

Another way is to write to some location in Realtime Database or Firestore and have another function trigger in response to that write. The other function will probably want to delete the written data before it terminates.