我在這里讀到未處理的錯誤可能會導致冷啟動。我正在實作一個觸發功能,如下:
exports.detectPostLanguage = functions
.region("us-central1")
.runWith({ memory: "2GB", timeoutSeconds: "540" })
.firestore.document("posts/{userId}/userPosts/{postId}")
.onCreate(async (snap, context) => {
// ...
const language = await google.detectLanguage(post.text);
// ... more stuff if no error with the async operation
});
我是否需要捕獲 google.detectLanguage() 方法錯誤以避免冷啟動?
如果是,我應該怎么做(A 或 B)?
A:
const language = await google.detectLanguage(post.text)
.catch(err => {
functions.logger.error(err);
throw err; // <---- Will still cause the cold start?
});
乙:
try {
var language = await google.detectLanguage(post.text)
} catch(err) {
functions.logger.error(err);
return null; // <----
}
更新
基于弗蘭克解決方案:
exports.detectPostLanguage = functions
.region("us-central1")
.runWith({ memory: "2GB", timeoutSeconds: "540" })
.firestore.document("posts/{userId}/userPosts/{postId}")
.onCreate(async (snap, context) => {
try {
// ...
const language = await google.detectLanguage(post.text);
// ... more stuff if no error with the async operation
} catch(err) {
return Promise.reject(err);
}
return null; // or return Promise.resolve();
});
uj5u.com熱心網友回復:
如果 Cloud Function 主體中出現任何例外,運行時會假定容器處于不穩定狀態,并且不再安排它處理事件。
為防止這種情況發生,請確保您的代碼中沒有例外逃逸。您可以不回傳任何值,也可以回傳指示代碼中的任何異步呼叫何時完成的承諾。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/388274.html
標籤:javascript 火力基地 异步等待 谷歌云功能 冷启动
上一篇:引數型別'List<Todo>?Function(QuerySnapshot<Object?>)'不能分配給引數型別'List<Todo&g
