【问题标题】:How to schedule cron job in nodejs for a time range如何在nodejs中安排一个时间范围内的cron作业
【发布时间】:2020-12-21 23:44:58
【问题描述】:

我正在尝试在上午 9 点到晚上 9 点之间每 15 分钟安排一次 Cron 作业。我可以每 15 分钟安排一次,但不能安排时间。下面是代码sn-p

const CronJob = require('cron').CronJob
const splunkNode = require('./splunk_node')
let job = new CronJob("*/15 08-21 * * * 1-5", function(){
    console.log('ran at ', new Date())
// CRON JOB()
 },function(){
     console.log('Job stopped')
 },true,'America/Los_Angeles')
 job.start()

指定范围的语法必须用括号[]括起来吗?

【问题讨论】:

标签: node.js cron


【解决方案1】:

一个 lil 解决方法是安排一个函数在一定时间后调用自己

function callCronJob (){
const job = new CronJob("*/15 08-21 * * * 1-5", function(){
    // if time range we are in doesn't fall between 9AM and 9PM cancel this job and call // this function again with it's 9AM 
// next part is pseduocode cause I am lazy to write actual code
  if ( 9am >timeRange > 9pm ){
 job.cancel() 
 new cronJob(timeRange+ 12 hours , ()=>{
   callCronJob();
});
 return;
}
// CRON JOB()
 },function(){
     console.log('Job stopped')
 },true,'America/Los_Angeles')
 job.start()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多