Nvv
Nvv

Reputation: 89

How to schedule function based on condition in firebase

I have this condition inside a cloud function. The condition is that if the value is "true", then I need to schedule it to false after 5 minutes. The status will update to true, but after 5 minutes it still remains true.

if(status == true){
    let usersUpdate = {};
    usersUpdate[`machines.${on}.status`] = true;
    db.collection('customers').doc(doc.id)
       .update(usersUpdate);
    //wait for 5 minutes
    //change status to false
    schedule = functions.pubsub.schedule('every 5 minute').onRun((context) => {
      console.log('This will be run every 5 minutes!');
      let usersUpdate = {};
      usersUpdate[`machines.${on}.status`] = false;
      db.collection('customers').doc(doc.id)
         .update(usersUpdate);
          })}
    

But this doesn't work. Should I export the pubsub function?

Upvotes: 0

Views: 557

Answers (1)

Nabel
Nabel

Reputation: 1956

It is outside of Firebase, but Cloud Tasks are what you want for this. Super easy.

https://stackoverflow.com/a/65297948/8698374

Upvotes: 2

Related Questions