首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在数据库中存储调度cron引用,以便将来在节点js中停止cron?

如何在数据库中存储调度cron引用,以便将来在节点js中停止cron?
EN

Stack Overflow用户
提问于 2021-02-27 16:29:00
回答 2查看 43关注 0票数 0

我正在使用cron安排推送通知。我正在从用户那里收集推送通知数据和预定时间,以便在预定时间发送推送通知。

下面是我如何创建一个cron作业来调度推送通知:

代码语言:javascript
复制
const CronJob = require("cron").CronJob;
...
...
const schedulePushNotification = async (req, res) => {
  const job = new CronJob(
    new Date(req.body.scheduledTime),
    async () => {
      // my code to send push notification
      // code to store push notification data
      // Here I write a code to store push notification data to 
      // show on the dashboard like a list of notification
    },
    null,
    true,
    "America/Los_Angeles"
  );
  
  job.start();
  return res.status(200).json({
    success: true,
    message: "Notification has been scheduled successfully !",
    data: null,
  });
};

现在的要求是,如果用户想要取消预定的推送通知,他们可以通过单击cancel来完成。

那么,我该如何销毁正在运行的cron呢?

EN

回答 2

Stack Overflow用户

发布于 2021-02-27 16:34:14

您可以通过在函数中添加一个if语句来实现这一点

您可以检查用户是否激活了通知,但首先必须将用户是否激活通知存储在数据库中,然后再发送通知

代码语言:javascript
复制
const CronJob = require("cron").CronJob;
...
...
const schedulePushNotification = async (req, res) => {
  const job = new CronJob(
    new Date(req.body.scheduledTime),
    async () => {



      let userNotificationDetails = getUserNotificationDetails();
      if(userNotificationDetails.isSendNotification){
          job.start();
      } else {
          // don't send notification
      }



      // my code to send push notification
      // code to store push notification data
      // Here I write a code to store push notification data to 
      // show on the dashboard like a list of notification
    },
    null,
    true,
    "America/Los_Angeles"
  );
  
  
  return res.status(200).json({
    success: true,
    message: "Notification has been scheduled successfully !",
    data: null,
  });
};

cron编辑(另一种解决方案:在启动之前检查是否isNotification ):

代码语言:javascript
复制
const CronJob = require("cron").CronJob;
...
...
const schedulePushNotification = async (req, res) => {
  const job = new CronJob(
    new Date(req.body.scheduledTime),
    async () => {
      // my code to send push notification
      // code to store push notification data
      // Here I write a code to store push notification data to 
      // show on the dashboard like a list of notification
    },
    null,
    true,
    "America/Los_Angeles"
  );



  let userNotificationDetails = getUserNotificationDetails();
  if(userNotificationDetails.isSendNotification){
      job.start();
  } else {
      // don't send notification
  }
  



  return res.status(200).json({
    success: true,
    message: "Notification has been scheduled successfully !",
    data: null,
  });
};
票数 0
EN

Stack Overflow用户

发布于 2021-02-27 17:54:23

您可以通过在函数内添加一条if语句来完成此操作

您可以检查用户是否激活了通知,但首先必须在数据库中存储用户是否激活了通知,然后发送通知

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66396937

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档