13

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, and 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 a 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 parameters 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193
  • Have you tried the update method ? https://github.com/Automattic/kue/blob/master/lib/queue/job.js#L807 – Hugeen Jan 28 '16 at 08:44
  • Use the json API to GET /job/ before creating the job. If it exists (status 200), update the job. If it doesn't exist (status 404), create the job. – Kyle Pittman Feb 03 '16 at 15:21

2 Answers2

13

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();
});
Borys Serebrov
  • 15,636
  • 2
  • 38
  • 54
  • does it update 'delay' period? below is my code, before and after the update, delay remains the same – Sahas Aug 24 '16 at 03:10
1

Just wanted to post an update. This ticket https://github.com/Automattic/kue/issues/505 has the answer for my question.

Sahas
  • 3,046
  • 6
  • 32
  • 53