0

I apologize if I am posting too many questions regarding Cloud Functions on this forum. I am excited about the project and in the process of moving out my firebase-queue workers which I am using in my app, which is also currently in Beta and would be generally available soon, to Cloud Functions one by one and I want to make sure that I am not missing anything. I have a cloud function which is used to generate an authentication token which is then stored on the database again which is then read by the app to authenticate the user. The function is pretty simple and doesn't contain any work which may take varying amount of time to finish.

  exports.generateAuthenticationToken = functions.database.ref('/tasks/authentication/{taskId}')
  .onWrite(event => {
    const taskSnapshot = event.data
    if(!taskSnapshot.exists()) {
      return
    }
    const task = taskSnapshot.val()

    const uid = task.uid

    // Make sure that the user is an app user
    return ref.child('users').child(uid).once('value').then((snapshot) => {
      if(!snapshot.exists()) {
        console.error('Unable to authenticate user: Invalid UID: ', uid)
        return ref.child('taskResults/' + event.params.taskId).set({
          category: 'authentication',
          status: 'failure',
          error: 'Invalid uid',
        })
      } else {
        // Generate a token using 
        return admin.auth().createCustomToken(uid).then((token) => {
          return ref.child('taskResults/' + event.params.taskId).set({
            category: 'authentication',
            status: 'success',
            result: {
              authToken: token
            }
          })
        })
      }
    }).then(() => {
      ref.child('/tasks/authentication/' + event.params.taskId).remove()
    })
  })

On the Firebase Functions console, in the Logs section, I see logs that some invocations of this functions finish in less than 10 ms and some take as long as 4000 ms. I wanted to ask under what circumstances would the run time for a cloud function doing constant kind of vary so much. This particular worker is kind of critical for my app because the user has to wait to be authenticated to be able to use the app and 4 secs + the time taken to read back the token and authenticate makes it too long.

Varun Gupta
  • 2,870
  • 6
  • 33
  • 73

0 Answers0