【问题标题】:How to schedule function based on condition in firebase如何根据firebase中的条件安排功能
【发布时间】:2022-01-28 19:03:15
【问题描述】:

我在云函数中有这种情况。条件是,如果该值为“true”,那么我需要在 5 分钟后将其安排为 false。状态将更新为 true,但 5 分钟后仍保持为 true。

if(status == true){
    let usersUpdate = {};
    usersUpdate[`machines.${on}.status`] = true;
    db.collection('customers').doc(doc.id)
       .update(usersUpdate);
    //wait for 5 minutes
    //change status to false
    schedule = functions.pubsub.schedule('every 5 minute').onRun((context) => {
      console.log('This will be run every 5 minutes!');
      let usersUpdate = {};
      usersUpdate[`machines.${on}.status`] = false;
      db.collection('customers').doc(doc.id)
         .update(usersUpdate);
          })}
    

但这不起作用。我应该导出 pubsub 函数吗?

【问题讨论】:

标签: node.js firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

它在 Firebase 之外,但 Cloud Tasks 正是您想要的。超级简单。

https://stackoverflow.com/a/65297948/8698374

【讨论】:

  • 谢谢。我使用 setTimeOut 是因为我希望它仅在 5 分钟后运行并且它可以工作,它在 5 分钟后变为“离线”,我没有使用云任务或 firebase 调度程序,这会在未来造成任何问题吗?
  • 如果您超出免费层级,您将支付 5 分钟的服务器时间。此外,云功能的最大超时为 9 分钟。见 (firebase.google.com/docs/functions/…)
猜你喜欢
  • 2020-10-10
  • 2018-12-28
  • 2020-06-09
  • 2017-01-04
  • 1970-01-01
  • 1970-01-01
  • 2023-01-08
  • 1970-01-01
相关资源
最近更新 更多