Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

236
Visualizações
Slow firebase function response from background activity

The function shown below puzzles me for two reasons:

  • the function execution terminates before all output is given
  • the function execution takes more than 3 minutes; a very long time (so long, that it might not be because of the "cold starts" issue only).

When searching for bestpractices I found a hint, that background acitivities are slowed down after function execution is terminated (https://cloud.google.com/functions/docs/bestpractices/tips#do_not_start_background_activities).

How can I create a function, that terminates after all output is created and avoids background activity?
Is there any way how to speed up the get() processing?

screenshot of firebase functions dashboard

screensthot of firestore showing the document created to trigger the function

Please have a look on the code:

// 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)
    });
  }    
}
  });
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You have asynchronous calls in your code, but are not telling the Cloud Functions runtime about that (through the return value). It is very likely that your database get() calls don't even complete at this stage.

To fix that problem, you can use await inside the loop or 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))
  }    
}
});

There may be more problems with your code, so I recommend reading the documentation on sync, async and promises, and how to create a minimal, complete, verifiable example for future questions.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda