-1

这是我安排任务的代码。我使用了单独的路线来启动、停止和更改时间,如下所示。请告诉我它是否正确。而且我在更改时间频率时遇到错误。请帮助我。

const cronjob=require('cron').CronJob

var first='* * * * * *'

var task=function() {

    console.log('last'+job.lastDate()+' next '+job.nextDates(1));
  }

  var complete=function(){
      console.log('the end.........')
  }

var job = new cronjob(first,task, complete, false, 'Asia/Kolkata');

app.get('/start',(req,res)=>{

job.start()

console.log('is job running? ', job.running);

res.send('cron started')

})


app.get('/set',(req,res)=>{

    var time=req.headers.time    /* input taken from user for changing the frequency*/

    time='30 5 1-31 0-11 1-7'  /*hard-coded temporary for testing*/

    console.log(time)

    job.setTime(time)

})

/set api 抛出错误/错误:时间必须是 CronTime 的实例。/

4

1 回答 1

2

我们必须使用 cronTime 的实例,即

const CronTime = require('cron').CronTime
.
.
.
var time=req.query.time
//time='*/5 * * * * *'
job.setTime(new CronTime(time))
res.send('time changes')
于 2020-08-03T09:17:12.480 回答