用于 Firebase 错误处理的 Cloud Functions 函数

ano*_*dev 5 node.js firebase google-cloud-functions

我想知道当我链接了 Promise 并且如果出现问题我需要更新实时数据库时,编写 node.js 代码的正确方法是什么?

这是代码:

export const testErrorHandling = functions.database
      .ref('/workqueue/{pushId}/something').onWrite(event => {

  // Exit when the data is deleted.
  if (!event.data.exists()) {
    return;
  }

  //This is the retry count, give up if more than 5 times have been retried.
  const data = event.data.val()
  if (data.count >= 5) {
    return
  }

  return event.data.ref.root.child(data.fulluri).once('value').then(snapshot => {
    //Process all, if ok, delete the work queue entry
    return event.data.ref.remove()  
  }).catch(exception => {
    console.log('Error!: ' + exception)

    //Log error, increase retry count by one an write to that 
    //location to trigger a retry

    //Is the line below OK?
    //return event.data.ref.child('count').set(data.count + 1)
  })

})
Run Code Online (Sandbox Code Playgroud)

我猜这是在许多情况下的常见要求,但找不到示例,因为所有示例似乎只是写入 console.error 并完成。(这在现实世界中很少见。)

Tho*_*din 2

[Firebase Cloud Functions 开发人员] 你的想法非常聪明。它可以工作很多次,但不会捕获较低级别的问题,例如数据库不可用或应用程序超时(尽管可以通过排队重试的 Promise.race 来修复)。

我们正在努力向核心产品添加重试功能。既然您提出了这个问题,我很乐意征求一些客户的意见。作为开发人员,您需要/期望重试策略具有哪些功能?您认为什么是合理的默认设置?您希望如何覆盖这些默认设置?