Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

235
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda