0

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?

Dharmaraj
  • 29,469
  • 5
  • 26
  • 55
Neha
  • 49
  • 6

1 Answers1

2

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

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

Nabel
  • 1,445
  • 12
  • 19
  • Thank you. I used setTimeOut because I wanted it to run only after 5 minutes and it works, it turns "offline" after 5 minutes, I didn't use cloud tasks or firebase scheduler, will that cause any problems in the future? – Neha Jan 31 '22 at 08:39
  • 1
    If you go beyond the free tier, you will be paying for 5 minutes of server time. Also, the max timeout for cloud functions is 9 minutes. See (https://firebase.google.com/docs/functions/manage-functions#set_timeout_and_memory_allocation) – Nabel Jan 31 '22 at 11:54