来自后台的 Firebase 函数响应缓慢 activity
Slow firebase function response from background activity
下面显示的函数让我感到困惑有两个原因:
- 函数执行在给出所有输出之前终止
- 函数执行时间超过3分钟;很长一段时间(很长,可能不仅仅是因为“冷启动”问题)。
在搜索最佳实践时,我发现了一个提示,即函数执行终止后后台活动会变慢 (https://cloud.google.com/functions/docs/bestpractices/tips#do_not_start_background_activities)。
我如何创建一个函数,在创建所有输出后终止并避免后台 activity?
有什么方法可以加快 get() 处理速度吗?
screenshot of firebase functions dashboard
screensthot of firestore showing the document created to trigger the function
请看代码:
// The Cloud Functions for Firebase SDK to create Cloud Functions .
const functions = require("firebase-functions");
// The Firebase Admin SDK to access Firestore.
const admin = require("firebase-admin");
admin.initializeApp();
const db = admin.firestore();
exports.evaluateScore = functions
.region('europe-west1')
.firestore
.document('organizations/{orgId}/persons/{personId}')
.onWrite(async (snap, context) => {
const newDocument = snap.after.exists ? snap.after.data() : null;
const oldDocument = snap.before.exists ? snap.before.data() : null;
console.log(`lastName: '${newDocument.personLastName}'; id: '${snap.after.id}'`);
// if only newDocument exists
if (newDocument != null && oldDocument == null ) {
const arrayNameSplit = snap.after.ref.path.split('/');
var orgId = arrayNameSplit[arrayNameSplit.length -3];
var listOfProfiles = newDocument.listOfProfiles;
console.log(`listOfProfiles: `, JSON.stringify(listOfProfiles));
for (let i = 0; i < listOfProfiles.length; i++) {
db.collection('organizations').doc(orgId).collection('profiles').doc(listOfProfiles[i]).get()
.then(docRef => {
const profile = docRef.data();
console.log(i, ' profileTitle:', JSON.stringify(profile.profileTitle))
}).catch(e => {
console.error('something went wrong', e)
});
}
}
});
您的代码中有异步调用,但没有将此告知 Cloud Functions 运行时(通过 return 值)。您的数据库 get()
调用很可能在此阶段甚至未完成。
要解决该问题,您可以在循环内使用 await
或 Promise.all
:
exports.evaluateScore = functions
.region('europe-west1')
.firestore
.document('organizations/{orgId}/persons/{personId}')
.onWrite(async (snap, context) => {
const newDocument = snap.after.exists ? snap.after.data() : null;
const oldDocument = snap.before.exists ? snap.before.data() : null;
console.log(`lastName: '${newDocument.personLastName}'; id: '${snap.after.id}'`);
// if only newDocument exists
if (newDocument != null && oldDocument == null ) {
const arrayNameSplit = snap.after.ref.path.split('/');
var orgId = arrayNameSplit[arrayNameSplit.length -3];
var listOfProfiles = newDocument.listOfProfiles;
console.log(`listOfProfiles: `, JSON.stringify(listOfProfiles));
for (let i = 0; i < listOfProfiles.length; i++) {
const docRef = await db.collection('organizations').doc(orgId).collection('profiles').doc(listOfProfiles[i]).get();
const profile = docRef.data();
console.log(i, ' profileTitle:', JSON.stringify(profile.profileTitle))
}
}
});
您的代码可能存在更多问题,因此我建议您阅读 sync, async and promises, and how to create a minimal, complete, verifiable example 上的文档以备将来使用。
下面显示的函数让我感到困惑有两个原因:
- 函数执行在给出所有输出之前终止
- 函数执行时间超过3分钟;很长一段时间(很长,可能不仅仅是因为“冷启动”问题)。
在搜索最佳实践时,我发现了一个提示,即函数执行终止后后台活动会变慢 (https://cloud.google.com/functions/docs/bestpractices/tips#do_not_start_background_activities)。
我如何创建一个函数,在创建所有输出后终止并避免后台 activity?
有什么方法可以加快 get() 处理速度吗?
screenshot of firebase functions dashboard
screensthot of firestore showing the document created to trigger the function
请看代码:
// The Cloud Functions for Firebase SDK to create Cloud Functions .
const functions = require("firebase-functions");
// The Firebase Admin SDK to access Firestore.
const admin = require("firebase-admin");
admin.initializeApp();
const db = admin.firestore();
exports.evaluateScore = functions
.region('europe-west1')
.firestore
.document('organizations/{orgId}/persons/{personId}')
.onWrite(async (snap, context) => {
const newDocument = snap.after.exists ? snap.after.data() : null;
const oldDocument = snap.before.exists ? snap.before.data() : null;
console.log(`lastName: '${newDocument.personLastName}'; id: '${snap.after.id}'`);
// if only newDocument exists
if (newDocument != null && oldDocument == null ) {
const arrayNameSplit = snap.after.ref.path.split('/');
var orgId = arrayNameSplit[arrayNameSplit.length -3];
var listOfProfiles = newDocument.listOfProfiles;
console.log(`listOfProfiles: `, JSON.stringify(listOfProfiles));
for (let i = 0; i < listOfProfiles.length; i++) {
db.collection('organizations').doc(orgId).collection('profiles').doc(listOfProfiles[i]).get()
.then(docRef => {
const profile = docRef.data();
console.log(i, ' profileTitle:', JSON.stringify(profile.profileTitle))
}).catch(e => {
console.error('something went wrong', e)
});
}
}
});
您的代码中有异步调用,但没有将此告知 Cloud Functions 运行时(通过 return 值)。您的数据库 get()
调用很可能在此阶段甚至未完成。
要解决该问题,您可以在循环内使用 await
或 Promise.all
:
exports.evaluateScore = functions
.region('europe-west1')
.firestore
.document('organizations/{orgId}/persons/{personId}')
.onWrite(async (snap, context) => {
const newDocument = snap.after.exists ? snap.after.data() : null;
const oldDocument = snap.before.exists ? snap.before.data() : null;
console.log(`lastName: '${newDocument.personLastName}'; id: '${snap.after.id}'`);
// if only newDocument exists
if (newDocument != null && oldDocument == null ) {
const arrayNameSplit = snap.after.ref.path.split('/');
var orgId = arrayNameSplit[arrayNameSplit.length -3];
var listOfProfiles = newDocument.listOfProfiles;
console.log(`listOfProfiles: `, JSON.stringify(listOfProfiles));
for (let i = 0; i < listOfProfiles.length; i++) {
const docRef = await db.collection('organizations').doc(orgId).collection('profiles').doc(listOfProfiles[i]).get();
const profile = docRef.data();
console.log(i, ' profileTitle:', JSON.stringify(profile.profileTitle))
}
}
});
您的代码可能存在更多问题,因此我建议您阅读 sync, async and promises, and how to create a minimal, complete, verifiable example 上的文档以备将来使用。