简体   繁体   English

Firebase 云功能:如何在不导致冷启动的情况下使可重试事件失败?

[英]Firebase Cloud Function: How to fail a retryable event without causing a cold start?

Given a Firebase Cloud Function, what's the proper way to fail an event (with the intention of retrying it) without causing a cold start ?给定 Firebase 云函数,在不导致冷启动的情况下使事件失败(旨在重试)的正确方法是什么?

This documentation suggests that unhandled errors cause cold starts. 该文档表明未处理的错误会导致冷启动。 When using async functionality I'm assuming this applies to a rejected Promise as well.使用async功能时,我假设这也适用于被拒绝的Promise

throw new Error('I failed you'); throw new Error('我让你失望了'); // Will cause a cold start if not caught // 如果没有被捕获会导致冷启动

Given that functions will often rely on third-party services that may not be 100% reliable, I don't want my functions to incur further penalties from cold starts due to transient downstream errors.鉴于函数通常依赖于可能不是 100% 可靠的第三方服务,我不希望我的函数因瞬时下游错误而因冷启动而受到进一步的惩罚。

Is this even a recommended strategy?这甚至是推荐的策略吗? I'm using Firestore so I've considered storing retryable events in a temp collection that is scraped by a scheduled cloud function, but this seems unnecessarily complex.我正在使用 Firestore,因此我考虑将可重试事件存储在由计划的云函数抓取的临时集合中,但这似乎不必要地复杂。

A rejected promise on its own is not an "unhandled error".被拒绝的承诺本身并不是“未处理的错误”。 What you're showing is a thrown exception and doesn't have anything directly to do with promises.你展示的是一个抛出的异常,与承诺没有任何直接关系。 A thrown exception that's escapes the function callback is considered unhandled.逃避函数回调的抛出异常被认为是未处理的。 If you need to knowingly fail the function invocation, just return a rejected promise , don't throw anything as your quoted example code is doing now.如果您需要故意使函数调用失败,只需返回一个被拒绝的 promise ,不要像您引用的示例代码现在所做的那样抛出任何内容。

A simple rejected promise can be created if necessary using Promise.reject() :如有必要,可以使用Promise.reject()创建一个简单的拒绝承诺:

return Promise.reject(new Error('fail'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM