【问题标题】:Firebase functions updating / deleting document after a certain timeFirebase 功能在一段时间后更新/删除文档
【发布时间】:2020-12-14 21:59:30
【问题描述】:

我想创建一个 firebase 功能,该功能会在 24 小时后删除或更新文档,例如 Instagram 快拍。这种实现会奏效吗?

// 24 hours in miliseconds
const time = 24 * 60 * 60 * 1000;

exports.updateUser = functions.firestore
  .document('users/{id}')
  .onUpdate((change, context) => {
    setTimeout(() => {
      // Delete or update doc
    }, time);
  });

【问题讨论】:

    标签: firebase google-cloud-platform google-cloud-firestore google-cloud-functions


    【解决方案1】:

    如果你问是否可以使用 setTimeout 在 Cloud Functions 中实现一小时的延迟,答案是否定的,它不会起作用。 Cloud Functions 最多可以运行 9 分钟(默认为 60 秒)。之后,该函数将关闭,并且您的超时回调将被取消。

    如果您想为特定的未来时间安排一些工作,您应该改用Cloud Tasks。要么这样,要么使用scheduled function 定期扫描要删除的文档。

    另见:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-24
      • 1970-01-01
      • 2022-01-05
      • 2020-09-28
      • 1970-01-01
      • 2015-07-30
      相关资源
      最近更新 更多