Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to schedule a job once every Thursday using Kue?

Tags:

node.js

jobs

kue

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.

like image 461
Sam Avatar asked May 31 '13 22:05

Sam


2 Answers

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

like image 177
jwags Avatar answered Nov 14 '22 15:11

jwags


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;

like image 40
Stucco Avatar answered Nov 14 '22 17:11

Stucco