Kanishka Panamaldeniya Kanishka Panamaldeniya - 3 months ago 14
Javascript Question

Is it possible to update a already created job in kue node js

Hi i am creating jobs using Kue .

jobs.create('myQueue', { 'title':'test', 'job_id': id ,'params': params } )
.delay(milliseconds)
.removeOnComplete( true )
.save(function(err) {
if (err) {
console.log( 'jobs.create.err', err );
}

});


every job has a delay time ,normally it is 3 hours .

Now i will to check every incoming request that wants to create a new job and get the id .

As you can see from the above code , when i am creating job i will add the job id to the job .

so now i want to check the incoming id with the existing jobs' job_id s in the queue and update that existing job with the new params if a matching id
found .

so my job queue will have unique job_id every time :).

is it possible ? , I have searched a lot but no help found , I checked the kue json API . but it only can create and get retrieve jobs , can not update existing records .

Thanks in advance .

Answer

This is not mentioned in the documentation and examples, but there is an update method for a job.

You can update your jobs by job_id this way:

// you have the job_id
var job_id_to_update = 1;
// get delayed jobs
jobs.delayed( function( err, ids ) {
  ids.forEach( function( id ) {
    kue.Job.get( id, function( err, job ) {
      // check if this is job we want
      if (job.data.job_id === job_id_to_update) {
          // change job properties
          job.data.title = 'set another title';
          // save changes
          job.update();
      }
    });
  });
});

The full example is here.

Update: you can also consider using the "native" job ID, which is known for kue. You can get the job id when you create the job:

var myjob = jobs.create('myQueue', ...
    .save(function(err) {
        if (err) {
            console.log( 'jobs.create.err', err );
        }
        var job_id = myjob.id;
        // you can send job_id back to the client
});

Now you can directly modify the job without looping over the list:

kue.Job.get( id, function( err, job ) {
  // change job properties
  job.data.title = 'set another title';
  // save changes
  job.update();
});
Comments