Firebase函数“错误:超出内存限制。函数调用被中断。”

时间:2018-08-01 08:12:47

标签: javascript firebase firebase-realtime-database request google-cloud-functions

是否有人在使用Firebase函数时遇到以下错误(并且很确定它甚至不应该在quota of 256 MB附近)?

  

错误:超出内存限制。函数调用被中断。

功能

具体来说,这是一个基于CRON的每三周一次的函数,可进行 30次Feedly API调用,以检索每个100条文章的元数据(包括摘要/内容信息)。是从Firebase数据库中拉出并推送到Firebase数据库中的,它告诉我它不应该甚至接近所述限制的一半。当使用“ firebase functions:shell”进行本地测试时,它也对我有用。

以下是该代码的最低版本(约130行):

const rp = require('request-promise');

var exports = module.exports = {};

exports.feedlyPull_A = functions.pubsub.topic('triweekly-tick').onPublish((event) => {
  let ps = [];
  for (var catKey in feedlyList) {
    if (feedlyList.hasOwnProperty(catKey)) {

      let read_match_details = {
        uri: 'http://cloud.feedly.com/v3/streams/contents?streamId=user%2FUSER_KEY_HERE%2Fcategory%2F' + feedlyList[catKey].key + '&count=100',
        method: 'GET',\
        headers : {
          "Authorization" : "OAUTH KEY HERE"
        },
        json: true
      };
      ps.push(rp(read_match_details));
    }
  }

  let artPromise = admin.database().ref('Article').once('value');

  return artPromise.then(results_art => {
    let articleObj = results_art.val();

    let skillPromise = admin.database().ref('Skill').once('value');

    return skillPromise.then(results_ski => {
      console.log('Skills Retrieved!');
      let skillList = results_ski.val();

      return Promise.all(ps)
      .then((results) => {
        for (var i = 0; i < results.length; i++) {
          let articles = results[i].items;

          let pathName = feedlyList[i].name;

          if (articles && Object.keys(articles).length > 0) {
            for (var key in articles) {
              // A BUNCH OF KEYWORD PARSING ON ARTICLE SUMMARY/CONTENT HERE and storing in arrays
            }

            if (artList) {
              admin.database().ref('Article/'+pathName).set(artList, (snapshot) => {
                console.log('Processed & Pushed: ' + pathName);
              });
            }
          } else {
            console.log('Failed (No articles found): ' + pathName);
          }
        }
      }).catch(err => {
        console.log(err);
      });
    });
  });
});

我对云计算非常陌生,所以我对从哪里开始感到迷茫……有谁能够解决/调试像这样的问题,或者有什么建议?

我在考虑是否有某种方法可以测量累积内存(如果我了解256 MB的含义),我将能够基于反复试验来解决,但不知道该怎么做...

非常感谢! (这已经延迟了启动时间,因此我们将不胜感激!)

1 个答案:

答案 0 :(得分:0)

我不知道这是否是您的内存限制错误的根源,但是您应该按照以下方式修改代码,以便在Cloud Functions中返回承诺。

请注意,artList的值不清楚。它可能是在为提高可读性而删除的代码部分中定义的。

const rp = require('request-promise');

var exports = module.exports = {};

exports.feedlyPull_A = functions.pubsub.topic('triweekly-tick').onPublish((event) => {
  let ps = [];
  for (var catKey in feedlyList) {
    if (feedlyList.hasOwnProperty(catKey)) {

      let read_match_details = {
        uri: 'http://cloud.feedly.com/v3/streams/contents?streamId=user%2FUSER_KEY_HERE%2Fcategory%2F' + feedlyList[catKey].key + '&count=100',
        method: 'GET',\
        headers : {
          "Authorization" : "OAUTH KEY HERE"
        },
        json: true
      };
      ps.push(rp(read_match_details));
    }
  }

  let artPromise = admin.database().ref('Article').once('value');

  return artPromise.then(results_art => {
    let articleObj = results_art.val();

    let skillPromise = admin.database().ref('Skill').once('value');

    return skillPromise.then(results_ski => {
      console.log('Skills Retrieved!');
      let skillList = results_ski.val();

      return Promise.all(ps)
      .then((results) => {

        let promises = []; 

        for (var i = 0; i < results.length; i++) {
          let articles = results[i].items;

          let pathName = feedlyList[i].name;

          if (articles && Object.keys(articles).length > 0) {
            for (var key in articles) {
              // A BUNCH OF KEYWORD PARSING ON ARTICLE SUMMARY/CONTENT HERE and storing in arrays
            }

            if (artList) {
               promises.push(admin.database().ref('Article/'+pathName).set(artList));
            } else {
              console.log('Failed (No articles found): ' + pathName);
            }
          }
        }

        return Promise.all(promises);

      }).catch(err => {

        console.log(err);
        return false;   //Important, see the videos I mention below

      });
    });
  });
});

我还建议您查看以下两个必看的Firebase视频,内容涉及云功能和承诺https://www.youtube.com/watch?v=7IkUgCLr5oAhttps://www.youtube.com/watch?v=652XeeKNHSk