如何以编程方式在 firebase 中创建 cron 作业 [英] How to create cron jobs in firebase programmatically

本文介绍了如何以编程方式在 firebase 中创建 cron 作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个独特的问题.我实际上需要安排一项任务,在特定时间向符合条件的用户发送电子邮件.我检查并发现调度 cron 作业可以解决问题.但是,就我而言,我需要根据特定用户何时满足特定条件以编程方式安排这些任务.

这是我的场景

  1. 我正在我的应用程序中运行推荐活动,推荐最多 5 人的用户可以获得折扣订阅

  2. 用户推荐5人后,只要在24小时内付款,订阅费用将大幅下降.否则成本返回原值

  3. 我正在使用 Firebase 云功能在特定时间向符合条件的用户发送提醒电子邮件提醒,以便在 24 小时之前付款

问题来了

  1. 我想在特定时间发送提醒电子邮件,例如他推荐 5 个人后立即发送,当他的时间剩余时例如 3 小时等.

  2. 如果我只安排一个函数而不考虑用户,我可能需要定期设置 cron 作业,比如 1 分钟,以始终检查我的数据库中的用户是否符合条件,然后向他们发送电子邮件.这带来了严重的问题,无论是否有符合条件的用户,云功能都会运行.

  3. 另一个问题是,通过上述实现,我无法在特定/确切时间向用户发送电子邮件,除非我每 1 秒安排一次任务,这当然会飞涨,因为云函数将在大约每天大喊 86400 次(86400 秒)

我提出的解决方案

  1. 我只需要一种方法来动态安排 cron 任务,以便我可以为特定用户安排作业.这将解决许多问题,例如在特定时间发送电子邮件、防止云功能在不必要时运行、能够为不同用户设置不同的电子邮件发送时间等

  2. 我计划使用带有用户 ID 请求参数的 http 触发器来调度任务,以便 firebase 可以使用此 ID 将任务仅分配给特定用户,即每个符合条件的用户都有自己的 http 触发器

请问有没有办法实现这个或任何其他适合我的案例的好的解决方案?

解决方案

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

因此,与其说我需要安排一个从现在起运行 3 小时后向这 5 个人发送消息的函数",不如将其视为可以写入数据库的任务:在 x:yz 发送给这 5 个人的电子邮件",然后有一个定期的云函数来检查它需要执行哪些任务.

另见延迟谷歌云函数如何使用 Firebase Cloud Functions 的调度程序和实时数据库/分析触发器?

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

作为更新的替代方案:使用具有 API 的单独调度程序服务来创建调度,例如 Cloud Tasks.Doug 在 如何使用 Cloud Tasks 安排 Cloud Functions 将来运行(以构建 Firestore 文档 TTL)..>

I have a unique problem here. I actually need to schedule a task to send emails to eligible users at specific times. I have checked and found out that scheduling cron jobs can solve the problem. However, in my case, I need to schedule these tasks programmatically based on when specific users meet certain conditions.

Here is my scenario

  1. I am running a referral campaign in my application where users who refer up to 5 persons get a discounted subscription

  2. After the user has referred 5 persons, the cost of subscription is slashed as long as he pays within 24 hours. Otherwise the cost returns to the original value

  3. I'm using firebase cloud functions to send eligible users reminder email reminders at certain times to make payment before the 24 hours elapses

Now the problem

  1. I want to send reminder emails at specific times say as soon as he refers 5 persons, when his time is remaining say 3 hours etc.

  2. If I just schedule a function regardless of the user, I may have to set cron jobs at regular intervals say 1 minute to always check if the users in my db are eligible and then send emails to them. This pose serious issue such that cloud functions are ran whether or not there are eligible users.

  3. Another problem is that with the above implementation, I cannot send emails to users at specific/exact times unless I schedule tasks for like every 1 second which will of course sky rocket price as cloud functions will be called around a whooping 86400 times (86400 seconds) a day

My proposed solution

  1. I just need a way to schedule cron tasks dynamically so that I can schedule a job for particular users. This will solve many problems like sending the emails at particular times, prevent cloud functions from running when not necessary, ability to set different email send times for different users etc

  2. I plan using http triggers with a request parameter of the user ID for scheduling tasks so that firebase can use this ID to assign tasks to only specific users, i.e each eligible user will have his own http trigger

Please is there a way to achieve this or any other good solution for my case?

解决方案

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.

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.

Also see Delay Google Cloud Function, How to use scheduler for Firebase Cloud Functions with Realtime Database/Analytics triggers?

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.

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).

这篇关于如何以编程方式在 firebase 中创建 cron 作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆