Error: Deadline exceeded on background function execution
Related issues
[REQUIRED] Version info
node: 8
firebase-functions: ^3.2.0
firebase-tools: 7.0.2
firebase-admin: ^8.5.0
[REQUIRED] Test case
We are working in a project with google cloud functions for Firebase. We have the blaze plan but we are facing some execution problems with background functions when it comes to deal with read/write and iterate a good deal of documents (+200) from/to Firebase.
The functions just throw some errors after some time of execution or do not complete the task at all.
I have tried to scale up the RAM assigned to the function (2 GB) and the wait time (540 seconds) but it didn´t seem to work out. The function is programmed to run on an onUpdate event.
[REQUIRED] Steps to reproduce
Read a large collections and iterate over the snapshot. Then perform set and updates on firestore based on the retrieved data.
The code:
Index.js:
exports.createRDTCode = functions.runWith({ timeoutSeconds: 540,
memory: '2GB'}).firestore.document("xxxxx/{project}").onUpdate((change, context, callback) => {
return createRDTCode.handler(change,context,callback)
})
createRDTCode.js:
const admin = require("firebase-admin");
const firestore = admin.firestore();
exports.handler = async (change, context) => {
const newValue = change.after.data();
const previousValue = change.before.data();
let barsConsecutivo = newValue.barras;
if (
newValue.writeCode != previousValue.writeCode &&
newValue.writeCode == true
) {
const drillingRDTRef = admin
.firestore()
.collection("xxxxxxx")
.doc(context.params.project);
drillingRDTRef.update({
writeCode: false
});
const project = context.params.project.split("_", 2)[0];
const company = context.params.project.split("_", 2)[1];
const projectRef = admin
.firestore()
.collection("xxxxxxx")
.doc(context.params.project);
const barsRef = admin
.firestore()
.collection("xxxxx")
.doc(context.params.project)
.collection("xxxxx")
.where("idRDT", "==", null);
barsRef.get().then(snapshot => {
snapshot.forEach(rdtDoc => {
let barCode = (
company.substr(0, 2) +
project.substr(0, 2) +
"BA" +
barsConsecutivo.toString()
).toUpperCase();
admin
.firestore()
.collection("xxxxxx")
.doc(context.params.project)
.collection("xxxxx")
.doc(rdtDoc.id)
.update({
idRDT: barCode
});
projectRef.update({
drillingRDT: admin.firestore.FieldValue.arrayUnion(barCode),
inventory: admin.firestore.FieldValue.arrayUnion(barCode)
});
admin
.firestore()
.collection("xxxxxxx")
.doc(context.params.project)
.collection(rdtDoc.data().typeInventory.toLowerCase())
.doc(barCode)
.set({
typeRDT: rdtDoc.data().tipoRDT,
codeRDT: barCode,
statusRDT: rdtDoc.data().statusRDT,
rigArray: rdtDoc.data().rigArray,
typeInventory: rdtDoc.data().typeInventory.toLowerCase(),
numberHoles: rdtDoc.data().numberHoles,
lengthHole: rdtDoc.data().lengthHole
});
barsConsecutivo += 1;
});
const drillingRDTRef = admin
.firestore()
.collection("xxxxxxx")
.doc(context.params.project);
drillingRDTRef.update({
barras: barsConsecutivo
});
});
}
};
[REQUIRED] Expected behavior
Do not get deadline error and perform the update and write tasks.
[REQUIRED] Actual behavior
Error: Deadline exceeded at Http2CallStream.call.on (/srv/node_modules/@grpc/grpc-js/build/src/client.js:96:45) at emitOne (events.js:121:20) at Http2CallStream.emit (events.js:211:7) at process.nextTick (/srv/node_modules/@grpc/grpc-js/build/src/call-stream.js:71:22) at _combinedTickCallback (internal/process/next_tick.js:132:7) at process._tickDomainCallback (internal/process/next_tick.js:219:9)
Were you able to successfully deploy your functions?
Yes
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
Duplicate of #557
Will close this so we can track in #557 since that was moved.