I've just started using google cloud functions. And they behave in a strange way (or I'm doing something wrong). I have the following function which recalculates the average rating once user rates some item
exports.onItemRated = functions.database.ref('/votes_history/{rankingId}/current/{itemId}').onWrite(event => {
const ranks = event.data.val();
const itemId = event.params.itemId;
const rankingId = event.params.rankingId;
let avg = 0;
let num = 0;
Object.keys(ranks).forEach( (rankerId) => { //sum up all ratings for the item
avg += ranks[rankerId];
num ++;
});
avg /= num;
return admin.database().ref(`averages/${rankingId}/${itemId}`).transaction( (curVal) => {
return {avg, num};
});
}
I was testing it on a very small amount of data. Like 3 ratings. So, there should be nothing to take long time, I believe. However, here are the logs:
It's true that my "Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions". The thing I want to figure out is - whether there is a problem in my function or it's just restrictions. And also would be great if someone could explain how those restrictions work.
===UPDATE===
Thanks to Cloud Functions for Firebase onWrite timeout managed to solve the timeout problem by wrapping transaction into own Promise. Looks like it's a bug.
However, even now function execution time is very unpredictable. Not as crazy as before but still ranges from 50ms (perfect) up to 2k+ ms. I don't get why there is so huge difference. And what could even be the reason for that function to execute 2000ms?