Search code examples
javascriptnode.jsexpressqueuekue

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


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.


Solution

  • 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();
    });