Tuesday 23 May 2017

Prevent Job Processing to stop when server and/or script stops

I am using KUE library along with a simple script to test features of Kue jobs and processing. Here is my code index.js i.e. main file

'use strict';
let Job = require('./process.js');

for (let i=0;i<12;i++){
  Job.addJob();
}

and process.js job file

'use strict';
/*globals console,require,module*/
let queue = require('kue').createQueue();

class Job{
  constructor(){

  }
static addJob(){
    queue.create("jobs",{"hel":"hello"}).save(err=>{
        if(err){
            console.log("job not saved");
        }
        else{
            console.log("job saved");
        }
    })
}
};
let processJob = (job,callback) => {
  setTimeout(function(){ console.log("job done");callback(); }, 3000);
};
queue.process("jobs",(job,done)=>{
  processJob(job,done);
});
module.exports = Job;

Now when i run index.js everything works fine as expected, but I have two question

  1. When I stop my script after all 12 jobs are saved, they don't process until my script is started again, so How do I achieve that ?
  2. when I leave the script running for ample time for jobs to finish, script does not stop until I hit Ctrl+c. Any reason that may be preventing the script to stop after all jobs are processed ?


via Rishabh Jain

No comments:

Post a Comment