Wednesday, 26 April 2017

Why is firebase cloud function taking 25 seconds?

Edit: For clarity I have other cloud functions that all run intermittently (i.e from 'cold' in around 2-6 seconds, and all use the same boilerplate set up of importing an admin instance and exporting the function as a module)

I've seen other similar posts but this is really bugging me. I have a cloud function like so:

const admin = require('../AdminConfig');
const { reportError } = require('../ReportError');

module.exports = (event) => {
  const uid = event.params.uid;
  const snapshot = event.data;

  if (snapshot._newData === null ) {
   return null;
   }

  console.log('Create org begin running: ', Date.now());
  const organisation = event.data.val();
  const rootRef = admin.database().ref();
  const ref = rootRef.child('/organisations').push();
  const oid = ref.key;

  const userData = {
    level: 'owner',
    name: organisation.name,
   };

  const orgShiftInfo = {
    name: organisation.name,
    startDay: organisation.startDay || 'Monday',
   };

   const updatedData = {};
   updatedData[`/users/${uid}/currentOrg`] = oid;
   updatedData[`/users/${uid}/organisations/${oid}`] = userData;
   updatedData[`/organisations/${oid}`] = organisation;
   updatedData[`/org_shift_info/${oid}`] = orgShiftInfo;
   rootRef.update(updatedData, (err) => {

     if (err) {
       return rootRef.child(`/users/${uid}/addOrgStatus`).set({ error: true })
      .then(() => {
    console.log(`error adding organisation for ${uid}: `, err);
    return reportError(err, { uid });
    });
    }

   console.log('Create org wrote succesfully: ', Date.now());
    return rootRef.child(`/users/${uid}/addOrgStatus`).set({ success: true });
 });
}

I understand the 'cold start' thing but I think something is seriously wrong that it's taking 25 seconds... The logs don't return any error and are as so:

enter image description here

Is there some deeper way I can debug this to try and figure out why it's taking so long? It's unusable at the moment. Thanks a lot.



via Sam Matthews

No comments:

Post a Comment