在Firebase Cloud Functions中为每个用户运行调度程序? [英] Running a scheduler for each user in Firebase Cloud Functions?

查看:74
本文介绍了在Firebase Cloud Functions中为每个用户运行调度程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望运行一个根据我的云功能中的每个用户运行的调度程序,而不是一个针对所有用户同时运行的通用调度程序.

I wish to run a scheduler which runs according to each user in my cloud functions and not universal scheduler which runs at the same time for all users.

例如,如果UserA执行特定操作,则仅针对userA而非其他用户启动调度程序(例如,每30分钟). 但是随后,UserB也执行相同的操作,并在不同的时间为自己启动调度程序.

For example, if a UserA performs a certain action the scheduler(eg. every 30 mins) is initiated for userA only and not other users. But then UserB also performs that same action and initiates the scheduler for himself at a different time.

现在UserA和UserB的调度程序可以在不同的时间运行,但间隔相同(30分钟)

Now UserA’s and UserB’s scheduler could be running at different times but same intervals(30 mins)

如何创建这样的功能? 帮助将不胜感激.

How can I create such a function? Help would be very much appreciated.

推荐答案

仅Cloud Functions不能为每个用户独立地调度函数执行.您只能按照功能配置中提供的时间表来安排单个功能.显然,它无法扩展为每个用户部署新功能.

Cloud Functions alone won't be able to schedule function executions indepently for each user. You can only schedule a single function on a schedule that you provide in the function configuration. Obviously, it won't scale for you to deploy a new function for each user.

您有两种选择:

  1. 部署一个预定功能,该程序应尽可能频繁地运行(每1分钟).让它查询数据库以确定每个用户需要运行哪些工作,并安排它们执行.根据工作量,您可能希望通过pubsub将这些负载卸载到其他函数调用中.

  1. Deploy a scheduled function that runs as frequently as possible (every 1 minute). Have it query a database to figure out which items of work need to be run per-user, and arrange for them to execute. Based on the volume of work, you might want to offload those to other function invocations via pubsub.

基于每个用户,使用 Cloud Tasks 安排第一项应该为用户完成的工作(可以实现为调用HTTP触发器).任务的有效负载应描述要完成的工作(至少应包含其代表的用户的ID).当该任务完成时,HTTP触发器将必须确定下一次定期调用的时间,并照此计划.您实际上将在队列挂起"执行中始终只有一个任务.如果您的功能因某种原因无法建立下一个任务,则周期性将中断,您将不得不以某种方式找出如何重新启动它的方法.

On a per-user basis, use Cloud Tasks to schedule the first item of work that should be done for the user (which could be implemented to invoke an HTTP trigger). The payload of the task should describe the work to be done (minimally it should contain the id of the user it represents). When that task completes, the HTTP trigger will have to figure out when the next periodic invocation should be, and schedule it as such. You will effectively always have one task in a queue "pending" execution. If somehow your function fails to establish the next task, then the periodic nature will break, and you will have to somehow have to figure out how to start it up again.

选项1,IMO,更容易实现,但是如果调用需要精确地在分钟的精确时间进行,则您将失去定时的粒度.您还需要维护一个数据库或其他商店以进行查询,以弄清楚如何告诉每分钟功能应为任何给定用户做更多的工作.

Option 1 is easier to implement, IMO, but you lose the granularity of timing if the invocation needs to happen precisely on the exact second of the minute. You will also need to maintain a database or some other store to query to figure out how to tell the per-minute function should do more work for any given user.

选项2很难正确实现,需要使用带有新API的全新产品,但是您将能够以更精细的粒度安排任务.

Option 2 is much harder to implement correctly, requires the use of a whole new product with a new API, but you will be able to schedule tasks with much finer granularity.

这篇关于在Firebase Cloud Functions中为每个用户运行调度程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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