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
  • 47,845
  • 8
  • 52
  • 84
Nvv
  • 79
  • 10

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,767
  • 14
  • 23
  • 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? – Nvv 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