且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Firebase中以编程方式创建Cron作业

更新时间:2023-11-17 14:42:04

与其尝试动态创建预定函数,不如考虑使其具有按固定时间表运行的单个函数,然后在该函数内部实现计时逻辑.

Instead of trying to dynamically creating scheduled function, consider having a single function that runs on a fixed schedule, and then implementing your timing logic inside of that function.

因此,与其说我需要安排一个从现在开始运行3个小时的功能并向这5个人发送消息",不如说它是可以写入数据库的任务:在x:yz send发送给这5个人的电子邮件",然后使用定期的Cloud Function(云功能)检查其需要执行的任务.

So instead of saying "I need to schedule a function that runs 3 hours from now and send a message to these 5 people", think of it as a task that you can write into a database: "at x:yz send an email to these 5 people", and then have a periodic Cloud Function that checks what tasks it needs to execute.

另请参见延迟Google Cloud Function 或者,您可以使用 Cloud Scheduler 为要执行的特定操作创建任务,然后通过PubSub将其发布到Cloud Functions.

Alternatively you can use Cloud Scheduler to create a task for the specific action you want to perform, and then have it post to Cloud Functions via PubSub.

作为更新的替代方案:使用具有API的单独计划程序服务来创建计划,例如Cloud Tasks.道格在

As an even newer alternative: Use a separate scheduler service that has an API to create schedules, like Cloud Tasks. Doug wrote a great article about that in How to schedule a Cloud Function to run in the future with Cloud Tasks (to build a Firestore document TTL).