Activate retry in firebase cloud function programmatically

怎甘沉沦 提交于 2021-01-19 05:55:58

问题


i'm deploying firebase cloud functions to listen on changes in our firestore via continuous deployment.

I can only find a way to activate retries manually. Which goes contrary to our cd approach.

Looking at the normal gcp cloud functions the retry flag can be given when deploying. But i cannot find a similar option in the firebase-cli or the firebase-functions interface (2.1.0)

Any hints on how to solve this? Carsten


回答1:


There is currently no similar option for deployment using the Firebase CLI.

This is something being worked on by the Firebase team, so stay tuned for updates.




回答2:


You can enable retries in Firebase Functions using GCloud Console, manually by hand. Programmatically retrying trigger-based functions was added in firebase-functions 3.10.0 (see changelog and associated pull request).

Since it's not entirely obvious from the PR or the docs, here's a quick example of the syntax:

export const myFirebaseFunc = functions
  .runWith({
    failurePolicy: {
      retry: {},
    },
    memory: '512MB',
    timeoutSeconds: 60,
  })
  .firestore.document('/path/to/some/doc')
  .onCreate(async (snap, context) => {
   /* do stuff */
 })

At the time of writing this, it looks like the failure policy is simply on or off. Thus, this is equivalent

export const myFirebaseFunc = functions
  .runWith({
    failurePolicy: true,
    memory: '512MB',
    timeoutSeconds: 60,
  })
  .firestore.document('/path/to/some/doc')
  .onCreate(async (snap, context) => {
   /* do stuff */
 })

Some caveats:

  • You'll also have to deploy with --force

  • You can enable retries only on triggered functions, not http-called functions.

  • It would be idiotic not to build in some safeguards. Retry policy maxes out at 7 days, and bills just like any other function invocation, so if you have some unhandled error, it could repeatedly run for a full week. You can use context.eventTimestamp to know when the first attempt roughly started.

Read this: https://firebase.google.com/docs/functions/retries and make sure your function in idempotent.

It was also difficult to discover what to return to force a retry or avoid a retry. Triggered Firebase functions must return a Promise. (See this vid)

A retry-enabled Firebase function will retry if:

  • it returns a rejected promise
  • throws an exception
  • or the function times out

That means that if you run into an error that you know won't eventually resolve itself with a retry (i.e. you want to stop the function execution and not retry), you can return Promise.resolve({message: 'some message'});



来源:https://stackoverflow.com/questions/55606808/activate-retry-in-firebase-cloud-function-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!