如何在 Firebase 中安排一次性后台任务?

How can I schedule a one-off background task in Firebase?

我正在努力改进使用 Firestore 的应用程序中的代码。我想确保 Firestore 会在交易完成后清除交易的一些位。我可以选择 运行 事务和清理在一个事务中,而不是这样做。但是,我相信如果不进行其他架构更改,这也可能会引入竞争条件。

无论如何,在解决这个问题时,我意识到我不知道有什么方法(除了制作 task queue)在 Firestore.[=16 中分派一次性任务=]

我认为下面的玩具示例将有助于解释我想要做什么。

// push cleanup task to Firestore to execute in 3000ms
await dispatchTransactionCleanup({id : "xxx", inMs : 3000}); // !important

// run the actual transaction, which should not take more than 1000 ms
await runMyTransaction({id : "xxx"});

// also dispatch a cleanup here, to reduce latency over the other cleanup.
// but, importantly this cleanup isn't guaranteed to run, e.g.,
// the server crashes between request to run transaction 
// and this part of the program.
await runManualCleanup({id : "xxx"});

那么,我将如何 dispatchTransactionCleanup 到 运行 将来某个时候的一次性任务?如果没有我自己的任务队列,这可能吗?或者,我需要引入额外的结构吗?

Firestore 没有任何“任务”或“队列”的意义。它只会在您拨打电话时响应您拨打的 API 电话。

如果您想要排队机制,则必须构建它,或许可以使用其他一些软件产品。您已经链接到一个在后端使用 Cloud Functions 的解决方案。

您可能想重新考虑在单个事务中完成所有工作。目前尚不清楚为什么您认为这会有问题。也许你可以 post 第二个问题来更详细地解释你尝试过的方法,但它不会像你期望的那样起作用。