如何使用Kue每周四安排一次工作?

Sam*_*Sam 8 jobs node.js kue

使用Kue,如何安排每周四执行一次作业?Kue自述文件提到我可以延迟工作,但是在特定时间重复执行工作呢?

我可以用cron工作做我想做的事,但我喜欢Kue的功能.

我想要的是在周四的任何时候处理一份工作,但只能处理一次.

jwa*_*ags 16

我有一个类似的问题,我基本上想出了以下内容.如果其他人有不同的解决方案,我很乐意看到其他一些想法.

var jobQueue = kue.createQueue();

// Define job processor
jobQueue.process('thursday-jobs', function (job, done) {

  var milisecondsTillThurs = // TODO: Get the time until next thursday.  For this I used moment.js

  // do this job again next Thursday
  jobQueue.create('thursday-jobs').delay(milisecondsTillThurs).save();

  // For Example purpose this job waits then calls done
  setTimeout(function () {
      done();
  }, 10000);


});

// Use some initialization code to check if the job exists yet, and create it otherwise
kue.Job.rangeByType('thursday-jobs','delayed', 0, 10, '', function (err, jobs) {
    if (err) {return handleErr(err);}
    if (!jobs.length) {
        jobQueue.create('thursday-jobs').save();
    }
    // Start checking for delayed jobs.  This defaults to checking every 5 seconds
    jobQueue.promote();
});
Run Code Online (Sandbox Code Playgroud)

Kue的文档很少,但源代码评论很好且易于阅读