GGizmos
GGizmos

Reputation: 3775

How to stop a scheduled function in firebase?

From the docs I see I can create a cloud function which runs on a schedule by deploying something like this:

exports.scheduledFunction = functions.pubsub.schedule('every 5 minutes').onRun((context) => {
  console.log('This will be run every 5 minutes!');
  return null;
});

In my application I need to use something like this to check if a series of transactions on the blockchain have been completed which may take an unpredictable amount of time.

So I am using firestore to maintain a list of "things to check", and every time the scheduled function is run it will query the blockchain for the status of transactions to see if any have either succeeded or failed, at which point the entry will be removed from the list.

What I don't see in the documentation is how to stop the schedule function once its deployed since I would like to stop the schedule function once the list of things to check is empty becomes empty. Maybe its not critical to stop it since checking a firestore collection and finding it empty is likely not too expensive, but still, still it seems wasteful to check a known empty queue every minute of every day.

Thus my question -- how to stop one of these schedule function once deployed, and how to restart it, when a new item is added to the (formerly) empty queue?

Upvotes: 2

Views: 706

Answers (1)

Vaidehi Jamankar
Vaidehi Jamankar

Reputation: 1346

When you create a Cloud Function like this one that you created it basically sets that the function needs to run every 5 minutes, it does not create an individual task or context for each run of the Cloud Function.

To cancel the Cloud Function completely, you can run the following command from a shell:

firebase functions:delete scheduledFunction

Note :This will redeploy your Cloud Function the next time you run firebase deploy. If you want to instead skip running this function a certain time, you should either change the cron schedule to not be active during that interval, or skip the interval inside your Cloud Function's code.

If you want to setup specific future invocations of the function so as to call only when there is a new entry in the “things to check ” list as per your implementation you can use the Cloud Firestore triggers and Realtime Database triggerswith the specifications as you want You should also have a look at the Cloud functions trigger with the future callback enabled,check this blog post

Upvotes: 1

Related Questions