且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

请求完成后如何继续运行Firebase Cloud Function

更新时间:2022-01-28 07:34:54

如果 generateZipWithDocuments()不是异步的 res.sendStatus(),将在其后立即执行,并且 Cloud Function将终止(并且 generateZipWithDocuments()完成的工作将不会完成).有关更多详细信息,请参见文档此处.

If generateZipWithDocuments() is not asynchronous res.sendStatus() will be immediately executed after it, and the Cloud Function will be terminated (and the job done by generateZipWithDocuments() will not be completed). See the doc here for more details.

您有两种可能性:

  1. 您将其设为异步,然后等待其工作完成后再发送响应.通常,您通常使用 async/await .请注意,Cloud Function的最大执行时间为9分钟.
  2. 您将长时间执行的工作委派给另一个Cloud Function,然后然后,发送响应.要将任务委派给另一个Cloud Function,您应该使用Pub/Sub.请参见发布/订阅触发器此SO线程,以获取有关如何实现该操作的更多详细信息.在发布/订阅触发功能中,完成工作后,您可以通过电子邮件通知用户
  1. You make it asynchronous and you wait its job is completed before sending the response. You would typically use async/await for that. Note that the maximum execution time for a Cloud Function is 9 minutes.
  2. You delegate the long time execution job to another Cloud Function and, then, you send the response. For delegating the job to another Cloud Function, you should use Pub/Sub. See Pub/Sub triggers, the sample quickstart, and this SO thread for more details on how to implement that. In the Pub/Sub triggered Function, when the job is done you can inform the user via an email, a notification, the update of a Firestore document on which you have set a listener, etc... If generateZipWithDocuments() takes a long time, it is clearly the most user friendly option.