首页 > 解决方案 > 如何取消预定的 Firebase 功能?

问题描述

我正在开发一个 NodeJS 应用程序,在 Firebase 上运行,我需要安排一些电子邮件发送,我打算为此使用 functions.pubsub.schedule。

事实证明,我需要在需要时取消这些工作,并且我想知道一些方法来识别它们以最终可能取消,并且以某种方式有效地取消它们。

有什么办法可以做到这一点?提前感谢

标签: node.jsfirebasegoogle-cloud-functionsschedule

解决方案


当您使用以下内容创建云函数时:

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

以上只是设置了函数需要运行的时间表,并没有为 Cloud Function 的每次运行创建单独的任务。


要完全取消 Cloud Function,您可以从 shell 运行以下命令:

firebase functions:delete scheduledFunction

请注意,这将在您下次运行时重新部署您的 Cloud Function firebase deploy


如果您想在特定时间段内跳过发送电子邮件,则应将cron 计划更改为在该时间间隔不活动,或跳过Cloud Function 代码中的时间间隔。

在看起来像这样的伪代码中:

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;
});

推荐阅读