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.
Here’s the template of my 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.
2
Answers
Changing the memory limit to 16GiB and cpu to 4 seems to have solved this issue:
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.
So I will recommend you to place some
console.time(“Promise 1”)
andconsole.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. For more about this topic go through this threadAnd 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.