使用Cloud Function在Firestore上写入数据突然失败



我在Cloud function中有5个计划函数,几个月来一切都很好,但今天它似乎突然失败了,或者没有等到Firestore完成更新。这是超时还是服务停机?目前,所有5个计划函数都调用updateFunction()来更新Firestore文档中的某个字段,但只是这次更新部分没有成功。

'use-strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
/*
* 'onCreate' triggered when a document is written to for the first time.
* 'onDelete' triggered when a document with data is deleted.
* 'OnWrite' works as 'addValueEventListener' or 'addSnapshotListener' for android.
* It will fire the function every time there is some item added, removed or changed 
* from the provided 'database.ref'.
*
*/
const settingsConfig = { timestampsInSnapshots: true };
const db = admin.firestore();
db.settings(settingsConfig);

//Message priority must be one of normal or high.
const options = { priority: 'high' };
exports.schedTask3 = functions.pubsub.schedule('0 11 * * 1')
.timeZone('Asia/Manila') // Users can choose timezone - default is America/Los_Angeles
.onRun((context) => {
//This will be run every Monday at 11:00 AM
updateFunction();
});
//THIS FUNCTION IS BEING USE BY 5 SCHEDULE FUNCTION (schedTask1 to schedTask5)
function updateFunction() {
const dateFormat = require('dateformat');
dateFormat.i18n = {
dayNames: [
'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat',
'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'
],
monthNames: [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec',
'Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember'
],
timeNames: [
'a', 'p', 'am', 'pm', 'A', 'P', 'AM', 'PM'
]
};
//SUDDENLY DID NOT REACH THEN RESULT FOR THE FIRST TIME AFTER MONTHS OF WORKING
return db
.collection("Lorem")
.doc("Lorem")
.set({
date: dateFormat(new Date().getTime(), "dd mmmm yyyy"),
value: "Lorem"
})
.then(result => {
const payload = {
data: {
title: "Lorem",
description: "Lorem",
bigImage: "Lorem",
link: "Lorem",
environment: "release",
}
};
pushNotif(payload);
console.log("Updating prediction is success.");
}).catch(error => {
return console.error("Failed to send notification.", error);
})
}

function pushNotif(payload) {
return admin.messaging().sendToTopic("Announcement", payload, options)
.then(val => {
return console.log("Success!n" + JSON.stringify(payload), val);
})
.catch(error => {
return console.error("Failed to send notification.", error);
});
}

这是因为您错误地管理了云功能的生命周期。

重要的是,在调用异步方法的后台云函数中返回Promise(或值(,以便告诉云函数平台等待Promise完成或被拒绝后再清理函数。看见https://firebase.google.com/docs/functions/terminate-functions了解更多详细信息。

在您的情况下,会发生以下情况:有时,云功能平台不会立即清理您的云功能,并且云功能可以完成。但在其他情况下,由于您没有返回承诺,云功能平台会在完成之前对其进行清理。

以下更改(请参阅,除其他外,添加了几个return(应该可以做到这一点(未经测试(:

'use-strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
/*
* 'onCreate' triggered when a document is written to for the first time.
* 'onDelete' triggered when a document with data is deleted.
* 'OnWrite' works as 'addValueEventListener' or 'addSnapshotListener' for android.
* It will fire the function every time there is some item added, removed or changed 
* from the provided 'database.ref'.
*
*/
const settingsConfig = { timestampsInSnapshots: true };
const db = admin.firestore();
db.settings(settingsConfig);

//Message priority must be one of normal or high.
const options = { priority: 'high' };
exports.schedTask3 = functions.pubsub.schedule('0 11 * * 1')
.timeZone('Asia/Manila') // Users can choose timezone - default is America/Los_Angeles
.onRun((context) => {
//This will be run every Monday at 11:00 AM
return updateFunction()
.catch(error => {
console.log(error);
return null;
})
});
//THIS FUNCTION IS BEING USE BY 4 SCHEDULE FUNCTION (schedTask1 to schedTask5)
function updateFunction() {
const dateFormat = require('dateformat');
dateFormat.i18n = {
dayNames: [
'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat',
'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'
],
monthNames: [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec',
'Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember'
],
timeNames: [
'a', 'p', 'am', 'pm', 'A', 'P', 'AM', 'PM'
]
};
//SUDDENLY DID NOT REACH THEN RESULT FOR THE FIRST TIME AFTER MONTHS OF WORKING
return db
.collection("Lorem")
.doc("Lorem")
.set({
date: dateFormat(new Date().getTime(), "dd mmmm yyyy"),
value: "Lorem"
})
.then(result => {
const payload = {
data: {
title: "Lorem",
description: "Lorem",
bigImage: "Lorem",
link: "Lorem",
environment: "release",
}
};
return pushNotif(payload);

}).catch(error => {
// throw an error e.g. throw new Error('....'), see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
})
}

function pushNotif(payload) {
return admin.messaging().sendToTopic("Announcement", payload, options)
.then(val => {
console.log("Success!n" + JSON.stringify(payload), val);
return null;
})
.catch(error => {
// throw an error e.g. throw new Error('....'), see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
});
}

我建议您观看关于";JavaScript承诺";来自Firebase系列视频:https://firebase.google.com/docs/functions/video-series/.

最新更新