How to schedule a job once every Thursday using Kue?

↘锁芯ラ 提交于 2019-12-03 03:11:56

I had a similar question and I basically came up with the following. If anyone else has a different solution I would love to see some other ideas.

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

Kue has minimal documentation, but the source is well commented and easy to read

Take a look at kue-scheduler. I'm pretty sure that you should be able to do something like this:

var kue = require('kue-scheduler');
var Queue = kue.createQueue();

//create a job instance
var job = Queue
            .createJob('every', data)
            .attempts(3)
            .backoff(backoff)
            .priority('normal');

//schedule it to run every Thursday at 00:00:00 
var thursday = '0 0 0 * * 4';
Queue.every(thursday, job);


//somewhere process your scheduled jobs
Queue.process('every', function(job, done) {
    ...
    done();
});

kue-scheduler docs: https://github.com/lykmapipo/kue-scheduler; link in their docs to cron stuff: https://github.com/kelektiv/node-cron;

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!