【问题标题】:Firebase Cloud function listener stop after 1-2 hoursFirebase Cloud 功能侦听器在 1-2 小时后停止
【发布时间】:2017-10-19 21:44:45
【问题描述】:
var admin = require("firebase-admin");

var serviceAccount = require(__dirname+"/myserviceaccount.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://myproject.firebaseio.com"
});

db.ref('myref').on("child_changed", function(snapshot) {
   ...
});

package.json

{
  "name": "listener",
  "version": "0.0.1",
  "dependencies": {
    "firebase-admin": "^5.2.1"
  }
}

直到 1-2 小时后它才能正常工作,并且没有错误日志。谁能解决这个问题?

【问题讨论】:

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


    【解决方案1】:

    据我所知,您共享的代码并未启动任何云功能。我对它的部署感到惊讶,但它肯定不会在 Cloud Functions for Firebase 中启动可靠的侦听器。

    要编写在 Cloud Functions 环境中正确运行的代码,请务必遵循此处的说明:https://firebase.google.com/docs/functions/get-started

    具体来说:set up code in Cloud Functions that is triggered by updates to a database path 的正确语法是:

    exports.listenToMyRef = functions.database.ref('/myref/{pushId}')
        .onUpdate(event => {
          // Log the current value that was written.
          console.log(event.data.val();
          return true; 
        });
    

    【讨论】:

    • 云功能正常吗?我测试其他函数只是使用 setInterval 每 1 小时写入一次日志,但如果长时间不活动 HttpTrigger,则此 setInterval 停止工作。
    • cloud.google.com/functions/quotas 函数在被强制终止前可以运行的最长时间 540 秒。
    猜你喜欢
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多