0

I am working on a personal web application project. I have an arrow function that is being triggered every time user updates document inside movieIndex collection. Whenever my app launches it updates isBeingReviewed field of a movie document from False to True. I want to create a cloud function which sets the value of isBeingReviewed to false after 15 minutes. How can I access the updated value and set field to false? Is timeout a correct approach?

admin.initializeApp();
    exports.newMessage = functions.firestore
      .document("movieIndex/{movie}")
      .onUpdate(async (change, context) => {
    
        const changedDoc = change.after.data();
    
        if (changedDoc.isBeingReviewed == true)
        {
            console.log("The current status of isBeingReviewed is equal to TRUE, set it to false after 15 minutes ")
            setTimeout( access changed value and set it to false ,1000 * 60 * 15)
        }
        else 
        {
            console.log("The current status of isBeingReviewed is equal to FALSE")
    
        }
       
      });
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
S.Rucinski
  • 109
  • 1
  • 8

1 Answers1

0

It's not possible for a Cloud Function to run for more than 9 minutes. The default timeout is 60s. You won't be able to use JavaScript setTimeout to schedule some work for later, because Cloud Functions will shut down the code after the configured timeout, and the callback from setTimeout will be completely lost.

You will either have to use Cloud Tasks to schedule another function invocation for later, or you will have to make do with writing a scheduled function to periodically check for changes to make. In either case, it will require a fair amount of extra work - there is no real "easy" scheduling of future work.

See also:

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441