Google Cloud Function完成执行但从未输入回叫 [英] Google Cloud Function Finishes Execution But Never Enters Callbacks

查看:91
本文介绍了Google Cloud Function完成执行但从未输入回叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做25个请求来获取一些数据并将响应保存到单独的文件中。
我使用npm请求模块以基本的谷歌云功能发出请求。每个请求可能需要0.5到5秒的时间才能返回响应。当脚本设置为少于10个请求时,一切都按预期工作,并将所有内容保存到正确的文件中。然而,除此之外,我没有得到任何请求的回应。 (i = 0; i< popIDS.length; i ++){
for(j = 0; j fetchAndSaveToFile(popIDS [i],genreIDs [j],date);


fetchAndSaveToFile 发出npm请求并使用pako将压缩后的响应保存到Google云端存储中的文件中。这最初是一个提出约5个请求的问题,但是我增加了该函数的超时时间,并且它开始工作,但是我将该函数的超时最大化为9分钟。所有函数目前都在一个文件中。



另外值得注意的是,在回调中不使用popIDS [i],genreIDs [j]的值,'i'和'j'不需要被捕获。

我需要以不同的方式创建异步请求吗?我看到有异步请求模块处理这种事情。是否有需要配置的功能设置?或者我需要将其分成单独的文件,以便为每个请求设置9分钟的超时时间?

解决方案

我通过更新使用 Promise.all()并使用 new Promise 将请求调用映射到promise对象。返回 Promise.all()到主要的Google Cloud Function主体让我们知道函数在退出函数之前等待所有承诺完成。实现回调而不是承诺可能会导致Google云功能在所有回调完成之前过早退出。


I am trying to make ~25 requests to fetch some data and save the response to separate files. I am using the npm request module to make the requests with a basic google cloud function. Each request can take anywhere from 0.5 - 5 seconds to return a response. When the script is setup to make less than 10 requests, everything works as expected and saves everything to the proper files. However any more than that and I don't get a response from any request.

for (i = 0; i < popIDS.length; i++) {
    for (j = 0; j < genreIDs.length; j++) {
        fetchAndSaveToFile(popIDS[i], genreIDs[j], date);            
    }
}  

fetchAndSaveToFile makes the npm request and uses pako to save the compressed response to a file in Google Cloud Storage. This was originally an issue making ~5 requests but I increased the timeout for the function and it started working, however I maxed out the timeout for the function to 9 minutes. All functions are currently in one file.

Also it's worth noting that the value of popIDS[i], genreIDs[j] are not used inside the callback and 'i' and 'j' do not need to be captured.

Do I need to make async requests differently for this to work? I see there is async request module that handles this sort of thing. Are there function settings that I need to configure? Or do I need to split this out into separate files so that I can set a 9 minute timeout for each request?

解决方案

I was able to resolve by updating to use Promise.all() and mapping the request calls to promise objects using new Promise. Returning Promise.all() to the main Google Cloud Function body let's the function know to wait for all promises to finish before exiting the function. Implementing callbacks instead of promises may cause the Google cloud function to exit prematurely before all callbacks are finished.

这篇关于Google Cloud Function完成执行但从未输入回叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆