gpt4 book ai didi

javascript - 运行一堆Firebase查询时,Cloud Function for Firebase在60秒后超时

转载 作者:行者123 更新时间:2023-12-01 15:38:55 26 4
gpt4 key购买 nike

我正在将Firebase用于群组协作应用程序(例如Whatsapp),并且正在使用Cloud Function来找出哪些电话联系人也在使用我的应用程序(再次类似于Whatsapp)。在我开始在“功能日志”中看到以下日志以进行某些调用之前,Cloud Function运行良好。
Function execution took 60023 ms, finished with status: 'timeout'
我进行了一些调试,发现对于这个特定用户,他的手机通讯录中有很多联系人,因此很显然,弄清楚其中哪些联系人正在使用该应用程序所需的工作也增加到了60秒以下是Cloud Function的代码

      // contactsData is an array of contacts on the user's phone
// Each contact can contain one more phone numbers which are
// present in the phoneNumbers array. So, essentially, we need
// to query over all the phone numbers in the user's contact book
contactsData.forEach((contact) => {
contact.phoneNumbers.forEach((phoneNumber) => {
// Find if user with this phoneNumber is using the app
// Check against mobileNumber and mobileNumberWithCC
promises.push(ref.child('users').orderByChild("mobileNumber").
equalTo(phoneNumber.number).once("value").then(usersSnapshot => {
// usersSnapshot should contain just one entry assuming
// that the phoneNumber will be unique to the user
if(!usersSnapshot.exists()) {
return null
}
var user = null
usersSnapshot.forEach(userSnapshot => {
user = userSnapshot.val()
})
return {
name: contact.name,
mobileNumber: phoneNumber.number,
id: user.id
}
}))
promises.push(ref.child('users').orderByChild("mobileNumberWithCC").
equalTo(phoneNumber.number).once("value").then(usersSnapshot => {
// usersSnapshot should contain just one entry assuming
// that the phoneNumber will be unique to the user
if(!usersSnapshot.exists()) {
return null
}
var user = null
usersSnapshot.forEach(userSnapshot => {
user = userSnapshot.val()
})
return {
name: contact.name,
mobileNumber: phoneNumber.number,
id: user.id
}
}))
});
});
return Promise.all(promises)
}).then(allContacts => {
// allContacts is an array of nulls and contacts using the app
// Get rid of null and any duplicate entries in the returned array
currentContacts = arrayCompact(allContacts)

// Create contactsObj which will the user's contacts that are using the app
currentContacts.forEach(contact => {
contactsObj[contact.id] = contact
})
// Return the currently present contacts
return ref.child('userInfos').child(uid).child('contacts').once('value')
}).then((contactsSnapshot) => {
if(contactsSnapshot.exists()) {
contactsSnapshot.forEach((contactSnapshot) => {
previousContacts.push(contactSnapshot.val())
})
}
// Update the contacts on firease asap after reading the previous contacts
ref.child('userInfos').child(uid).child('contacts').set(contactsObj)

// Figure out the new, deleted and renamed contacts
newContacts = arrayDifferenceWith(currentContacts, previousContacts,
(obj1, obj2) => (obj1.id === obj2.id))
deletedContacts = arrayDifferenceWith(previousContacts, currentContacts,
(obj1, obj2) => (obj1.id === obj2.id))
renamedContacts = arrayIntersectionWith(currentContacts, previousContacts,
(obj1, obj2) => (obj1.id === obj2.id && obj1.name !== obj2.name))
// Create the deletedContactsObj to store on firebase
deletedContacts.forEach((deletedContact) => {
deletedContactsObj[deletedContact.id] = deletedContact
})
// Get the deleted contacts
return ref.child('userInfos').child(uid).child('deletedContacts').once('value')
}).then((deletedContactsSnapshot) => {
if(deletedContactsSnapshot.exists()) {
deletedContactsSnapshot.forEach((deletedContactSnapshot) => {
previouslyDeletedContacts.push(deletedContactSnapshot.val())
})
}
// Contacts that were previously deleted but now added again
restoredContacts = arrayIntersectionWith(newContacts, previouslyDeletedContacts,
(obj1, obj2) => (obj1.id === obj2.id))
// Removed the restored contacts from the deletedContacts
restoredContacts.forEach((restoredContact) => {
deletedContactsObj[restoredContact.id] = null
})
// Update groups using any of the deleted, new or renamed contacts
return ContactsHelper.processContactsData(uid, deletedContacts, newContacts, renamedContacts)
}).then(() => {
// Set after retrieving the previously deletedContacts
return ref.child('userInfos').child(uid).child('deletedContacts').update(deletedContactsObj)
})

以下是一些示例数据
// This is a sample contactsData
[
{
"phoneNumbers": [
{
"number": "12324312321",
"label": "home"
},
{
"number": "2322412132",
"label": "work"
}
],
"givenName": "blah5",
"familyName": "",
"middleName": ""
},
{
"phoneNumbers": [
{
"number": "1231221221",
"label": "mobile"
}
],
"givenName": "blah3",
"familyName": "blah4",
"middleName": ""
},
{
"phoneNumbers": [
{
"number": "1234567890",
"label": "mobile"
}
],
"givenName": "blah1",
"familyName": "blah2",
"middleName": ""
}
]



// This is how users are stored on Firebase. This could a lot of users
"users": {
"id1" : {
"countryCode" : "91",
"id" : "id1",
"mobileNumber" : "1231211232",
"mobileNumberWithCC" : "911231211232",
"name" : "Varun"
},
"id2" : {
"countryCode" : "1",
"id" : "id2",
"mobileNumber" : "2342112133",
"mobileNumberWithCC" : "12342112133",
"name" : "Ashish"
},
"id3" : {
"countryCode" : "1",
"id" : "id3",
"mobileNumber" : "123213421",
"mobileNumberWithCC" : "1123213421",
"name" : "Pradeep Singh"
}
}

在这种特殊情况下, contactsData包含 1046条目,对于其中某些条目,有两个 phoneNumbers。因此,假设我需要检查总共 1500电话号码。我正在创建查询以针对数据库中的用户与 mobileNumbermobileNumberWithCC进行比较。因此,该函数将在 promise 完成之前进行总共 3000查询,我猜测完成所有这些查询需要60秒钟以上的时间,因此Cloud Function超时。

我的几个问题是:
  • 是否所有这些查询都需要60秒以上的时间?鉴于它正在Firebase基础架构中运行,我期望它能更快完成。
  • 是否可以增加函数的超时限制?我目前正在执行Blaze计划。

  • 我也将赞赏上述功能的任何替代实现建议,以缓解该问题。谢谢!

    最佳答案

    如果无法避免查询太多数据,则可以使用左侧的Functions产品在项目的Cloud Console中更改函数的超时。当前,您将必须在每次新部署时重置超时。

    关于javascript - 运行一堆Firebase查询时,Cloud Function for Firebase在60秒后超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43223834/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com