[英]Firebase Function Firebase Error: deadline-exceeded when it hasn't timed out yet
I have a firebase v2 function set to timeout in 540 seconds however httpsCallableFromURL
catches with an error saying Firebase Error: deadline-exceeded
before the function finishes.我有一个 firebase v2 function 设置为在 540 秒内超时,但是
httpsCallableFromURL
捕获并显示错误Firebase Error: deadline-exceeded
。
Here's the template of my firebase function:这是我的 firebase function 的模板:
exports.functionname = functions.https.onCall(
{
timeoutSeconds: 540,
cpu: 2,
},
async (req) => {
const getOutput = async (id: string) => {
return new Promise((resolve, reject) => {
return axios.get('ur'l)
.then((res) => {
resolve(res.data)
})
.catch((e) => {
resolve(false)
})
})
}
let output: any
const recurse = async () => {
return new Promise(async (resolve) => {
// first try getting output
output = await getOutput(id).then((res) => {
return res
})
// check for output
if (output.output) {
return resolve(true)
} else if (output.error) {
return resolve(false)
} else {
setTimeout(() => {
return resolve(recurse())
}, 2000)
}
})
}
return recurse()
.then(async () => {
const imageUrl = output.output[0]
function getBase64(url: string) {
return axios
.get(url, {
responseType: 'arraybuffer',
})
.then((response) =>
Buffer.from(response.data, 'binary').toString('base64'),
)
}
const base64Image = await getBase64(imageUrl)
const data = {
type: 'image',
predictionId,
result: base64Image,
}
return data
}
})
.catch((e) => {
return false
})
},
)
If I monitor the function logs it ends up finishing within the 540 seconds but my client never receives the return value since the httpsCallableFromURL
errored out.如果我监控 function 日志,它最终会在 540 秒内完成,但我的客户端从未收到返回值,因为
httpsCallableFromURL
出错了。
This issue involves some debugging with calculating execution time for each Promise because your Error is related to the timeout that is specified in the function which is 540 sec means all of your promises should be resolved or rejected in the given 540 secs.此问题涉及一些调试,计算每个 Promise 的执行时间,因为您的错误与 function 中指定的超时相关,即 540 秒意味着您的所有承诺都应在给定的 540 秒内解决或拒绝。
So I will recommend you to place some console.time(“Promise 1”)
and console.timeEnd(“Promise 1”)
around each Promises so we can get expected execution time for each promise and compare the total execution time of function and with this data you can modify the the corresponding promise which takes a lot of time.所以我会建议你在每个 Promise 周围放置一些
console.time(“Promise 1”)
和console.timeEnd(“Promise 1”)
这样我们就可以得到每个 promise 的预期执行时间并将 function 的总执行时间与这个数据你可以修改对应的 promise 这需要很多时间。 For more about this topic go through this thread有关此主题的更多信息 go 通过此线程
And In the end If above all doesn't work then you can just increase the timeout to 1800 sec means 30 mins to complete all the execution within the given timeout, but this should be avoided as a best practice.最后,如果以上都不起作用,那么您可以将超时增加到 1800 秒,这意味着 30 分钟可以在给定的超时内完成所有执行,但作为最佳实践,应该避免这种情况。
Changing the memory limit to 16GiB and cpu to 4 seems to have solved this issue:将 memory 限制更改为 16GiB 并将 cpu 更改为 4 似乎已经解决了这个问题:
exports.functionname = functions.https.onCall(
{
timeoutSeconds: 540,
cpu: 4,
memory: '16GiB',
},
async (req) => {
},
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.