如何处理firebase触发函数的执行顺序 [英] How to deal with firebase trigger function execution order

查看:22
本文介绍了如何处理firebase触发函数的执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 firebase http 函数,可以将数据附加到 firebase 数据库列表中.第二个函数被配置为在列表更改时处理列表,然后更新一些汇总数据.就我而言,这些更新是突发的.我正在使用 node.js firebase 函数.

I have a firebase http function which appends data to a firebase database list. A second function is configured to process the list when it changes and then update some summary data. In my case, these updates come in bursts. I'm using node.js firebase functions.

查看 firebase 日志,当我从一个空列表开始时看到这个序列:

Looking at the firebase logs, I see this sequence when starting with an empty list:

  1. 从 http 添加到列表 - 列表有 1 个元素
  2. 从 http 添加到列表 - 列表有 2 个元素
  3. 从 http 添加到列表 - 列表有 3 个元素
  4. 从 http 添加到列表 - 列表有 4 个元素
  5. 用 1 个元素总结列表
  6. 用 3 个元素总结列表
  7. 用 4 个元素总结列表
  8. 用 2 个元素总结列表

我的问题是摘要只包含 2 个元素而不是 4 个.

My problem is the summary only includes 2 elements instead of 4.

看起来汇总触发器函数是并行调用的,而不是顺序调用的,所以当几个触发器非常接近时,最后一个完成的可能是第一个触发的而不是最后一个.

It would appear the summarize trigger functions are invoked in parallel instead of sequential so when several trigger in close proximity, the last one to finish may be be one of the first ones triggered instead of the last.

可以使用哪些方法来确保汇总计算具有所有数据"并且先前运行较慢的汇总计算不会覆盖后面的汇总计算?firebase 函数触发器可以序列化以按照它们启动的顺序执行吗?

What approaches can be used to insure the summary computation has 'all the data' and a prior summary computation running slower doesn't overwrite a later one? Can firebase function triggers be serialized to execute in the order they are initiated?

理想情况下,我想避免在突发事件出现时计算摘要 N 次,因此某些解决方案可以在未来的短时间内安排"摘要,然后在出现新事件时取消并重新安排乖一点.

Ideally I'd like to avoid computing the summary N times when a burst comes in so some solution where the summary could be 'scheduled' for some short time in the future and then canceled and rescheduled if a new event comes in would be nice.

推荐答案

我的解决方法是存储一个 admin.database.ServerValue.TIMESTAMP 和列表 add 并在结果计算器中验证它产生了最新时间戳的结果.如果没有,它会再次尝试.在大多数情况下,它不需要重新计算摘要,因为我的输入源通常是零星的单个列表添加而不是集中添加.我将此实现为一个返回 Promise 的函数,如果需要重新计算,该函数会调用自身.这是顺序:

My workaround is to store a admin.database.ServerValue.TIMESTAMP with the list add and verify in the results calculator that it produced results for the latest timestamp. If not, it tries again. In most cases it will not need to recompute the summary as my input source is normally sporadic single list adds rather than lumped additions. I implemented this as a function returning a Promise that calls itself if necessary to recompute. This is the sequence:

  1. 读取当前列表和时间戳
  2. 计算汇总结果并存储
  3. 再次读取时间戳
  4. 如果时间戳不同,则转到 1,否则完成

代码如下:

/// return a Promise that new summary and detail results will be posted
function updateResults(regattaId, lapdataTS, depth) {
  if (depth > 10) {
    return Promise.reject("Too many recomputes");
  }
  return admin.database().ref('/eventdata/'+regattaId).once('value')
  .then(function (snapshot) {
    const rawdata = snapshot.val(); 

    if (rawdata.lapdataTS === lapdataTS) {
        // console.log("already computed");
        return Promise.resolve();
    }
    lapdataTS = rawdata.lapdataTS ? rawdata.lapdataTS : null;
    const results = regattaCalc.computeResults(rawdata);

    var updates = {};
    updates['results/' + regattaId] = results;
    updates['summary/' + regattaId] = results.regattaInfo;
    return admin.database().ref().update(updates);
  }).then(function () {
    // read last TS and see if it matches our summary
    return admin.database().ref('/eventdata/'+regattaId+'/lapdataTS').once('value');
  }).then(function (snapshot) {
   if (snapshot.val() === lapdataTS) {  
       return Promise.resolve();
   } else {
       //console.log("Need to calc again");
       return updateResults(regattaId, lapdataTS, depth++);
   }
  }).catch((reason) => {
    console.log("Error generating summary: " + reason);
    return Promise.reject(reason);
  });
}

exports.compupteResults = functions.database.ref('/eventdata/{regattaId}').onWrite(event => {
return updateResults(regattaId,null,0);
 });

这篇关于如何处理firebase触发函数的执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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