'Firebase Functions code return (Deadline exceeded, code 16) error, while same code is working on local
I have below source code, the cronjob runs on scheduled time, it checks the price of a specific item from another collection and when it meets the criteria it will send an email to the notifyee perosn.
The source code is working properly on my local, but when I put this into a firebase function it return below errors:
exports.notNotifiedSendEmail = functions.pubsub.schedule("15 * * * *").onRun(() => {
cloudDB
.collection("notifications")
.get()
.then(function (querySnapshot) {
for (const doc of querySnapshot.docs) {
data = doc.data().notifyee;
for (const el of data) {
const status = el.status;
if (status === "not_notified") {
price = el.price;
email = el.email;
symbol = doc.data().symbol;
if (symbol.includes("-")) {
checkMultiLegPrice(symbol, price, email);
} else {
checkPrice(symbol, price, email);
}
}
}
}
})
.catch((error) => {
console.log("Error getting documents: ", error);
});
function checkMultiLegPrice(symbols, price, email) {
console.log("Found: " + symbol);
function isBetween(n, a, b) {
return (n - a) * (n - b) <= 0;
}
const splitedSymbol = symbols.split("-");
const sizeOf = splitedSymbol.length;
if (sizeOf == 2) {
// Spread
for (let i = 0; i < sizeOf - 1; i++) {
firstSplitedSymbol = splitedSymbol[i];
secondSplitedSymbol = splitedSymbol[i + 1];
const [firstLeg, secondLeg] = Promise.all([
getSymbolsDetials(firstSplitedSymbol),
getSymbolsDetials(secondSplitedSymbol),
]);
const firstLegHigh = firstLeg.high;
const secondLegHigh = secondLeg.high;
const finalHigh = firstLegHigh - secondLegHigh;
const firstLegLow = firstLeg.low;
const secondLegLow = secondLeg.low;
const finalLow = firstLegLow - secondLegLow;
firstLegSymbol = firstLeg.symbol;
secondLegSymbol = secondLeg.symbol;
if (
firstSplitedSymbol === firstLegSymbol &&
secondSplitedSymbol === secondLegSymbol &&
isBetween(price, finalLow, finalHigh)
) {
changeStatus(symbol, email, price);
emailTemplate(email, symbols, price);
}
}
}
}
function checkPrice(symbol, price, email) {
cloudDB
.collection("prices")
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
data = doc.data();
if (
symbol === data.symbol &&
price >= data.low &&
price <= data.high
) {
changeStatus(symbol, email, price);
emailTemplate(email, symbol, price);
}
});
})
.catch((error) => {
console.log("Error getting documents: ", error);
});
}
function emailTemplate(email, symbol, price) {
cloudDB
.collection("Email")
.add({
to: email,
message: {
subject: "Hello " + email,
text: "Dear,",
html:
"Please be inform the price of " +
symbol +
" is now meet your requirement. <br> you interested price is: " +
price,
},
})
.then(() => {
console.log("Queued email for delivery! (", symbol + " " + email + " " + price + ")");
});
}
function changeStatus(docID, email, price) {
const newStatus = "notified";
console.log("Notified: ", docID, email);
const notRef = cloudDB.collection("notifications").doc(docID);
try {
cloudDB
.collection("notifications")
.get()
.then(function (querySnapshot) {
for (const doc of querySnapshot.docs) {
const data = doc.data().notifyee;
const symbol = doc.data().symbol;
if (docID === symbol) {
for (let i = 0; i < data.length; i++) {
console.log("Inside loop: ", data[i]);
if (
data[i].email === email &&
symbol === docID &&
data[i].price == price &&
data[i].status === "not_notified"
) {
notRef.update({
notifyee: admin.firestore.FieldValue.arrayRemove(
data[i]
),
});
const v = {
symbol: symbol,
email: data[i].email,
price: data[i].price,
status: newStatus,
notified_at:
admin.firestore.FieldValue.serverTimestamp(),
};
notRef.collection("notified").add(v);
console.log("Status changed");
} else {
console.log("Status Not changed!");
}
}
}
}
})
.catch((error) => {
console.log("Error getting documents: ", error);
});
} catch (e) {
console.log("changeStatus Function failure:", e);
}
}
function getSymbolsDetials(docID) {
var docRef = cloudDB.collection("prices").doc(docID);
return docRef
.get()
.then((doc) => {
if (doc.exists) {
allData = doc.data();
return allData;
} else {
console.log("No such document!: " + docID);
}
})
.catch((error) => {
console.log("Error getting document:", error);
});
}
return null;
})
Errors:
Error: 4 DEADLINE_EXCEEDED: Deadline exceeded
at Object.callErrorFromStatus (/workspace/node_modules/@grpc/grpc-js/build/src/call.js:31:26)
at Object.onReceiveStatus (/workspace/node_modules/@grpc/grpc-js/build/src/client.js:180:52)
at Object.onReceiveStatus (/workspace/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:336:141)
at Object.onReceiveStatus (/workspace/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:299:181)
at /workspace/node_modules/@grpc/grpc-js/build/src/call-stream.js:160:78
at processTicksAndRejections (internal/process/task_queues.js:77:11)
at runNextTicks (internal/process/task_queues.js:64:3)
at processTimers (internal/timers.js:497:9)
Error: Process exited with code 16
at process.<anonymous> (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:92:22)
at process.emit (events.js:400:28)
at process.emit (domain.js:475:12)
at process.exit (internal/process/per_thread.js:173:15)
at sendCrashResponse (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/logger.js:44:9)
at process.<anonymous> (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:88:44)
at process.emit (events.js:400:28)
at process.emit (domain.js:475:12)
at processPromiseRejections (internal/process/promises.js:245:33)
at processTicksAndRejections (internal/process/task_queues.js:96:32)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
