How to schedule a job once every Thursday using Kue?
Asked Answered
B

2

8

Using Kue, how do I schedule a job to be executed once every Thursday? The Kue readme mentions that I can delay a Job, but what about repeatedly executing the Job at a specific time?

I can do what I want with a cron job, but I like Kue's features.

What I want is to process a Job once anytime on Thursday, but only once.

Branks answered 31/5, 2013 at 22:36 Comment(0)
G
16

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

Gur answered 24/6, 2013 at 20:20 Comment(0)
O
1

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;

Oralla answered 7/6, 2017 at 21:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.