Firebase Cloud Functions:尽管结果成功,但错误500

时间:2020-04-23 05:39:26

标签: node.js python-3.x axios google-cloud-functions

我正在Firebase Cloud Functions上构建webscraping应用程序的后端,它包含两个功能:

  1. 使用BeautifulSoup用Python3编写的WEBSCRAPER FUNCTION,可从多个页面(轻松地约500页)中抓取内容,并将抓取的内容作为json返回
  2. 用Node.js编写的计划功能,每隔一小时就会调用第1项中的网络抓取工具,并使用抓取的内容更新Firestore。

计划功能使用Axios执行获取请求。

问题我一直在不定期地收到计划功能中的以下错误。这种情况有时会在每次调用时发生,有时甚至不会发生一次,因此很难调试。在开发,登台和产品环境中也会出现不规则现象:

//err.response
{ status: 500,
  statusText: 'Internal Server Error',
  headers: 
   { 'x-cloud-trace-context': '2f22d7fc6a9044dcfc31acafc2d50e54',
     date: 'Thu, 23 Apr 2020 04:45:39 GMT',
     'content-type': 'text/html; charset=UTF-8',
     server: 'Google Frontend',
     'content-length': '323',
     'alt-svc': 'quic=":443"; ma=2592000; v="46,43",h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,h3-T050=":443"; ma=2592000',
     connection: 'close' },
  config: 
   { url: 'CLOUD_FUNCTIONS_URL',
     method: 'get',
     params: {//params}
}

无论何时在“预定功能”中发生此类错误,实际上在WEBSCRAPER FUNCTION中均不会发生任何错误,即Firebase Cloud Function控制台为WEBSCRAPER FUNCTION打印Function execution took XXXX ms, finished with status code: 200

我还检查了WEBSCRAPER FUNCTION,以status code: 200结尾时,正确返回了有效结果

我的代码部分如下:

//In the SCHEDULED FUNCTION where I call the WEBSCRAPER FUNCTION
function getContentPromise(param1, param2) {
    let url = //WEBSCRAPER FUNCTION's url
    let parameters = {param1: param1, param2: param2};
    let config = {
        params: parameters,
    }
    return axios.get(url, config)
    .then((response) => {
        return response.data
    })
    .catch((err) => {
        console.log('Retrying...', err.response) //Error thrown here
        return getContentPromise(param1, param2)
    })
}

//In the WEBSCRAPER FUNCTION

//At main.py
def scrape(request):
    response_object = scrape(request)
    return jsonify(response_object)

//At the scrape function
def scrape(request):
    param1 = request.args.get('param1')
    param2 = request.args.get('param2')

    //Some scraping stuff happening here using beautifulsoup

    response_object = {}
    response_object[constants.status_code_key] = 200
    response_object['contents'] = contents //contents in json

    return response_object

1 个答案:

答案 0 :(得分:0)

您正在执行哪个火力计划?仅在Blaze plan中允许对非Google服务的出站网络请求。

如果应用程序可以在本地环境中运行,但可以在生产环境中提供500个响应代码,则可以确定是否属于这种情况。