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

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

问题描述

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

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.

这是我的情况

  1. 我正在我的应用程序中运行引荐广告系列,引荐最多5个人的用户可以享受折扣订阅

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

用户推荐了5个人后,只要他在24小时内付款,就可以大幅降低订阅费用.否则,费用将恢复为原始值

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

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

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

现在有问题了

  1. 我想在特定时间发送提醒电子邮件,例如他提到5个人时,剩余时间例如3小时等.

  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.

如果我不考虑用户而只是安排一个功能,则可能必须定期设置cron作业(例如1分钟),以始终检查数据库中的用户是否符合条件,然后向他们发送电子邮件.这就构成了一个严重的问题,无论是否有合格的用户,都将运行云功能.

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.

另一个问题是,使用上述实现,我无法在特定/确切的时间向用户发送电子邮件,除非我将任务安排为大约每1秒钟,这当然会价格飞涨,因为云功能将在每天高达86400次(86400秒)

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

我建议的解决方案

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

  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

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

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.

因此,与其说我需要安排一个从现在开始运行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).

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

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