I am using Firebase for a group collaboration app (like Whatsapp) and I am using a Cloud Function to figure out which of the phone contacts are also using my app (again similar to Whatsapp). The Cloud Function ran fine till I started to see the following log in the Functions Log for some invocations.
Function execution took 60023 ms, finished with status: 'timeout'
I did some debugging and found that for this particular user, he has a lot of contacts on his phone's contacts book and so obviously the work required to figure out which of those contacts are using the app also increased to a point that it took more than 60 secs. Below is the code for the 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)
})
Below is some sample data
// 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"
}
}
In this particular case, the contactsData
contained 1046
entries and for some of those entries, there were two phoneNumbers
. So, let's assume there were a total of 1500
phone numbers that I need to check. I am creating queries to compare against the mobileNumber
and mobileNumberWithCC
for the users in the database. So, there are a total of 3000
queries that the function will make before the promise finishes and I am guessing it is taking more than 60 seconds to finish up all those queries and hence the Cloud Function timed out.
My few questions are:
- Is it expected for all those queries to take more than 60 secs? I was expecting it to finish much faster given that it is running within the Firebase infrastructure.
- Is there a way to increase the timeout limit for a function? I am currently on Blaze plan.
I will also appreciate any alternate implementation suggestions for the above function in order alleviate the problem. Thanks!
See Question&Answers more detail:
os