简体   繁体   English

Google Cloud Async Processing返回200

[英]Google Cloud Async Processing return 200

I am using a google cloud function as a webhook to receive a payload from a 3rd party service. 我使用Google Cloud函数作为Webhook,以接收来自第三方服务的有效负载。 Typically when such services make requests to webhooks they expect a HTTP 200 as acknowledgement. 通常,当此类服务向Webhook发出请求时,它们期望将HTTP 200作为确认。 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. 但是,使用下面的云功能设置(并由Google推荐)时,正在进行处理时,请求服务将返回408。

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. 在这种情况下,即使成功处理了请求,第三方服务(在这种情况下,它的cloudmailin也是适用于我尝试与之集成的任何Webhook)将重试该请求。

My question is: how can I get the cloud function to return a 200 and still continue the async processing? 我的问题是:如何获取云函数以返回200并继续进行异步处理?

//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);
    });
});

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. 由于HTTP触发器在发送响应后会被有效终止并清除,因此发送响应后,您将无法继续执行该函数中的异步工作。 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. 您要做的就是让HTTP函数委托工作到其他一些在HTTP请求范围之外的后台函数。

One way to do that is to use a pubsub trigger . 一种方法是使用pubsub触发器 The HTTP trigger can broadcast a pubsub message, which then causes the pubsub trigger to execute. HTTP触发器可以广播pubsub消息,然后使pubsub触发器执行。 Pubsub requires billing to be enabled for the project. Pubsub要求为该项目启用计费。

Another way is to write to some location in Realtime Database or Firestore and have another function trigger in response to that write. 另一种方法是写到Realtime Database或Firestore中的某个位置,并有另一个函数触发器来响应该写操作。 The other function will probably want to delete the written data before it terminates. 另一个函数可能要在终止写入数据之前将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM