问题
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