【问题标题】:Firebase Cloud Functions: Error 500 despite successful resultsFirebase Cloud Functions:尽管结果成功,但仍出现错误 500
【发布时间】:2020-08-06 08:32:30
【问题描述】:

我正在 Firebase Cloud Functions 上构建网页抓取应用程序的后端,包含两个函数:

  1. WEBSCRAPER FUNCTION,Python3使用BeautifulSoup编写,从多个页面(大概500页左右)抓取内容,并将抓取的内容返回为json
  2. 调度函数,用 Nodejs 编写,每隔一小时调用第 1 项中的网络爬虫,并使用爬取的内容更新 Firestore。

调度函数使用 Axios 执行 get 请求。

问题我一直在计划的函数中不定期地收到以下错误。有时每次调用都会发生这种情况,有时甚至不会发生一次,这使得调试变得非常困难。违规行为也发生在开发、登台、生产环境中:

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

只要在 SCHEDULED FUNCTION 中发生此类错误,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

【问题讨论】:

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


    【解决方案1】:

    您采用的是哪个 Firebase 计划?仅在 Blaze plan 中允许对非 Google 服务的出站网络请求。

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

    【讨论】:

    • 我的三个环境都在燃烧。而且我在生产中也遇到了上述错误。它不规则地发生,有时在开发和登台中发生,但在生产中却没有。有时在生产和开发中,但不在登台阶段。
    • 从我的角度来看,很难提出适当的修复建议。也许来自 firebase 的人会出现并提供更好的见解。但与此同时,您可以通过重新配置计划间隔并将抓取的数据存储到容器或临时存储中,直到下一次成功获取来使用解决方法。然后,您可以将该缓冲区作为 catch 块的备份进行管道传输。签出managing firebase resources 以确保您不会用完磁盘存储,因为您正在抓取数百页。
    猜你喜欢
    • 1970-01-01
    • 2015-08-10
    • 2014-01-08
    • 2018-06-11
    • 2022-08-19
    • 1970-01-01
    • 2021-08-14
    • 2017-07-29
    • 1970-01-01
    相关资源
    最近更新 更多