How to cancel a scheduled firebase function?

余生长醉 提交于 2021-01-29 14:09:46

问题


I'm developing a NodeJS application, running on Firebase, where I need to schedule some email sendings, for which I intend to use functions.pubsub.schedule.

Turns out that I need to cancel those jobs when needed, and I'd like to know some way to identify them for eventual possible cancellation, and some way yo effectively cancel them.

Any way to do this? Thanx in advance


回答1:


When you create a Cloud Function with something like this:

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

The above merely sets up a table of when the function needs to run, it does not create an individual task 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 that this will redeploy your Cloud Function the next time you run firebase deploy.


If you want to instead skip sending emails during a certain time period, you should either change the cron schedule to not be active during that interval, or skip the interval inside your Cloud Function's code.

In pseudo-code that'd look something like:

exports.scheduledFunction = functions.pubsub.schedule('every 5 minutes').onRun((context) => {
  console.log('This will be run every 5 minutes!');
  if (new Date().getHours() !== 2) {
    console.log('This will be run every 5 minutes, except between 2 and three AM!');
    ...
  }
  return null;
});


来源:https://stackoverflow.com/questions/59116635/how-to-cancel-a-scheduled-firebase-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!