Is it possible to update an already created job in Kue Node.js?
Asked Answered
L

2

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.

Lovieloving answered 21/1, 2016 at 9:28 Comment(2)
Have you tried the update method ? github.com/Automattic/kue/blob/master/lib/queue/job.js#L807Scotch
Use the json API to GET /job/<id> before creating the job. If it exists (status 200), update the job. If it doesn't exist (status 404), create the job.Monahon
N
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();
});
Nasser answered 3/2, 2016 at 21:8 Comment(1)
does it update 'delay' period? below is my code, before and after the update, delay remains the sameUproot
U
1

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

Uproot answered 24/8, 2016 at 4:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.