I'm trying to run a scheduled cloud function on firebase. I see this error in the logs when it runs:
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:237:22)
at process.emit (events.js:400:28)
at process.emit (domain.js:470:12)
at process.exit (internal/process/per_thread.js:169:15)
at Object.sendCrashResponse (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/logger.js:38:9)
at process.<anonymous> (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:233:22)
at process.emit (events.js:400:28)
at process.emit (domain.js:470:12)
at processPromiseRejections (internal/process/promises.js:245:33)
at processTicksAndRejections (internal/process/task_queues.js:96:32)
here is my function:
exports.scheduledFunction = functions.pubsub
.schedule("every 5 minutes")
.onRun((context) => {
console.log("This will be run every 5 minutes!");
const tsToMillis = admin.firestore.Timestamp.now().toMillis();
// const compareDate = new Date(tsToMillis - 24 * 60 * 60 * 1000);
const compareDate = new Date(tsToMillis - 5 * 60 * 1000);
admin
.firestore()
.collection("properties").where("status", "==", "Reserved")
.where("reservationDate", "<", compareDate)
.where("paymentIsDone", "==", false)
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
admin.firestore().collection("properties").doc(doc.id).update({
agent: "",
reservedBy: "",
reservedByPhoneNumber: "",
reservationDate: null,
status: "Remaining",
});
console.log("canceling reservations");
});
});
return null;
});
I want to cancel reservations on properties that have not completed payment within 24 hours (for testing 5 min) after reservation.
This is how I reserve the property:
exports.reserveApt = functions.https.onCall((data, context) => {
if (!context.auth) {
return {
result: "failed",
msg: "you are not authenticated",
};
}
const doc = admin.firestore().collection("properties").doc(data.aptId);
// get apt
return doc.get().then((apt) => {
apt = apt.data();
// check apt is not already reserved or sold
// console.log(apt);
if (apt.status !== "Remaining") {
return {
result: "failed",
msg: "Appartment is already reserved or sold",
};
}
// reserve apt with username and phone and changes status to reserved
return doc
.update({
agent: data.agent,
reservedBy: data.reserveeName,
reservedByPhoneNumber: data.reserveePhone,
reservationDate: admin.database.ServerValue.TIMESTAMP,
status: "Reserved",
paymentIsDone: false,
})
.then(() => {
console.log("apt reserved");
return {result: "success"};
});
});
});
Is the problem with the timestamps or what? I don't understand.