I have a cloud function project where I use Firestore as the database. I have a function that works as a RESTapi. I do also have a cloud function trigger which will run on document create.
Scenario
- The frontend sends a request to the RESTapi which creates a one document
- onCreate trigger get called
When the onCreate trigger get call, I send a request to a 3rd party API. That API returns an ID which I will set back to the recently created document in Firestore.
// Runs inside onCreate trigger "some-collection/{someId}"
const result = await fetchIdFromA3rdPartyAPI(query);
if (result) {
try {
await firestore()
.collection("some-collection")
.doc(docId)
.set({
someId: parseInt(result)
}, { merge: true });
} catch (err) {
// log to gcp Cloud Logging
console.log(err);
}
}
The problem
If this onCreate get invoked after a cold start it takes upto 4 minute to update the document with the someId
field.
If I split the update call as below,
const doc = await firestore().collection("some-collection").doc(docId);
// console.log('reached here', doc);
await doc.set({...})
Now I can see the console log “reached here”, which means it takes 4 min to resolve set()
call ( Also tried with update()
as well ).
If I trigger the onCreate
immediately after this resolved, it works without any delay. So it seems like happens on cold start only.
All functions runs with following configurations
timeouteSeconds: 540
memory: 4GB
maxInstance: 1
version: v1
Also nothing throws. I cannot see any errors on the log as well.
What could possibly be the reason for this ?