我是Firebase的新手,我完全不知道我应该使用什么。这是我的流程。
我在firebase上有一个集合score
,它有值
- start_time
- count
- max_count
现在,当start_time
与当前时间匹配时,我需要每隔5秒递增一次count
,直到它将max_count
与数据库匹配为止。这应该在后端。现在我搞糊涂了。什么可以适用于此?
关于云任务和发布/订阅的文档太多了。
如果我从Pub/Sub调用firebase函数来每5秒更新一次计数,那么我将为调用函数而支付未使用的计算时间。
我不知道更多关于云任务的信息,它是否符合我的要求?有谁能给我指路吗?
发布于 2021-03-10 17:28:33
Cloud Tasks和Pub/Sub都不是解决这个问题的正确方案,我不建议使用cron类型的服务来完成这样一个次要的任务。
相反,请考虑将增量逻辑移动到客户端,并仅在数据库中存储start_time
和max_count
。下面是一个例子:
// Let's set a start_time 10 seconds in the future and pretend this was in the database
const start_time = Math.floor((new Date()).getTime() / 1000) + 10;
// Pretend this came from the database, we only want to iterate 10 times
const max_count = 10;
let prev_count = 0;
document.write("Waiting 10 seconds before starting<br />");
// Let's iterate once a second until we reach the start_time
let interval = setInterval(() => {
const now = Math.floor((new Date()).getTime() / 1000);
// If it's not start time, exit
if (now < start_time) return;
// Determine the count by dividing by 5 seconds
let count = Math.floor((now - start_time) / 5);
if (count > prev_count) {
document.write(`Tick: ${count}<br />`);
}
prev_count = count;
if (count >= max_count) {
clearInterval(interval);
}
}, 1000);
如果您需要将计数存储在数据库中,请让它在每次递增时更新数据库中的count
值。
https://stackoverflow.com/questions/66559470
复制相似问题