0

i started to use kue/node.js . I checked a tutorial and it had this code

var kue = require('kue')
    , queue = kue.createQueue();

function newJob (){
    var job = queue.create('new_job');
    job.save();
}

queue.process('new_job', function (job, done){
    console.log('Job', job.id, 'is done');
    done && done();
})


setInterval(newJob, 3000);

So When i run this from my terminal - node test.js , i will output "job { job id } is done" every 3 seconds .

Now i changed the code to following

    var kue = require('kue')
        , queue = kue.createQueue();

   var job = queue.create('new_job');
   job.save();

    queue.process('new_job', function (job, done){
        console.log('Job', job.id, 'is done');
        done && done();
    })

I removed the newJob function and the setInterval , I expected the code would run once , because i create the queue and save the job first and then i will process it below . but when i run the code from terminal nothing happens , no output .

Why i am not getting my expected result ? what is the logic behind this . please explain . thanks in advance .

Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193

1 Answers1

0

This is because 'new_job' is created before an process listener 'new_job' is created. If you create queue after creating a process listener, it should work like such:

var kue = require('kue')
    , queue = kue.createQueue();

queue.process('new_job', function (job, done){
    console.log('Job', job.id, 'is done');
    done && done();
})

var job = queue.create('new_job');
job.save();
Taku
  • 5,639
  • 2
  • 42
  • 31
  • mmm , i tried with your code still the same result :( – Kanishka Panamaldeniya Jan 08 '16 at 03:56
  • Adding the process listener queue.process may be asynchronous so queue.create('new_job') may not be registered. What if you wrap the last two lines like this: setTimeout(function(){ var job = queue.create('new_job'); job.save(); }) – Taku Jan 08 '16 at 03:57