Skip to content

Job Editing

Grant Carthew edited this page Oct 10, 2016 · 14 revisions

Description

One of the most powerful and flexible methods within rethinkdb-job-queue is the Job.update method. It allows you, without restriction, to change any value of a job and save those changes back to the queue database.

This document will give you some examples of how you may use the Job.update method to perform advanced changes to jobs in the queue.

Job Reanimation

This example will show how to reanimate or re-run a job that has finished. A job is considered finished if it has either a completed, cancelled, or terminated status. See the Job Status document for more detail.

To start with, here is a JSON object representing a cancelled job.

{
  "dateCreated": Sun Oct 09 2016 22:57:37 GMT+00:00 ,
  "dateEnable": Sun Oct 09 2016 23:02:37 GMT+00:00 ,
  "dateFinished": Sun Oct 09 2016 22:57:37 GMT+00:00 ,
  "dateStarted": Sun Oct 09 2016 22:57:37 GMT+00:00 ,
  "id":  "7b4d91c2-6489-4713-b52b-69ce7f61d7e1" ,
  "log": [
    {
      "data": null ,
      "date": Sun Oct 09 2016 22:57:37 GMT+00:00 ,
      "message":  "Job added to the queue" ,
      "queueId":  "WebDev:rjqJobQueueTests:rjqJobQueueTestJobs:1460:d464d60d-1fff-48ce-9a64-3a988b4a3f6b" ,
      "retryCount": 0 ,
      "status":  "waiting" ,
      "type":  "information"
    } ,
    {
      "date": Sun Oct 09 2016 22:57:37 GMT+00:00 ,
      "message":  "Job retrieved and active" ,
      "queueId":  "WebDev:rjqJobQueueTests:rjqJobQueueTestJobs:1460:d464d60d-1fff-48ce-9a64-3a988b4a3f6b" ,
      "retryCount": 0 ,
      "status":  "active" ,
      "type":  "information"
    } ,
    {
      "date": Sun Oct 09 2016 22:57:37 GMT+00:00 ,
      "message":  "The quick brown fox jumped over the lazy dog" ,
      "queueId":  "WebDev:rjqJobQueueTests:rjqJobQueueTestJobs:1460:d464d60d-1fff-48ce-9a64-3a988b4a3f6b" ,
      "retryCount": 0 ,
      "status":  "cancelled" ,
      "type":  "information"
    }
  ] ,
  "priority": 40 ,
  "progress": 0 ,
  "queueId":  "WebDev:rjqJobQueueTests:rjqJobQueueTestJobs:1460:d464d60d-1fff-48ce-9a64-3a988b4a3f6b" ,
  "retryCount": 0 ,
  "retryDelay": 600000 ,
  "retryMax": 3 ,
  "status":  "cancelled" ,
  "timeout": 300000
}

To re-run this job you will need to change three properties as follows.

  • Status: Change the status from cancelled to waiting.
  • retryCount: Set the retryCount to zero if it is not already zero.
  • dateEnable: Set the dateEnable to now or a future date.

Here is the code to make these changes.

Assumptions:

  • The job is already in the database with the properties above.
  • We have the job id.

All we need to do it get the job and update its properties.

const Queue = require('rethinkdb-job-queue')
const q = new Queue()

q.getJob('5127d082-fe7e-4e88-b1de-7093029695c3').then((savedJobs) => {
  savedJobs[0].status = 'waiting'
  savedJobs[0].retryCount = 0
  savedJobs[0].dateEnable = new Date()
  return savedJobs[0].update(null, 'Job reanimated')
}).catch(err => console.error(err))

Job Data Changes

If for some reason you need to change the data associated with a job you can do so with the following code. For this example we need to change the job.data property from foo to bar.

Assumptions:

  • The job is in the queue.
  • The job has not been processed.
  • The job.data property is set to foo.
  • We have the job id.
const Queue = require('rethinkdb-job-queue')
const q = new Queue()

q.getJob('5127d082-fe7e-4e88-b1de-7093029695c3').then((savedJobs) => {
  savedJobs[0].data = 'bar'
  return savedJobs[0].update(null, 'Job data updated')
}).catch(err => console.error(err))

Removing Job Data

If for some reason you had extra data saved with a job and you need it removed for some security reason. You could do so easily enough as this example shows.

Assumptions:

  • The job is in the queue.
  • The job.secret property is set.
  • We have the job id.
const Queue = require('rethinkdb-job-queue')
const q = new Queue()

q.getJob('5127d082-fe7e-4e88-b1de-7093029695c3').then((savedJobs) => {
  delete savedJobs[0].secret
  return savedJobs[0].update(null, 'Job secret removed')
}).catch(err => console.error(err))

Disable Job

If you had a job waiting for processing and you need to disable the job until further notice, here is one way you could achieve this.

Assumptions:

  • The job is in the queue.
  • The job has not been processed.
  • We have the job id.
const Queue = require('rethinkdb-job-queue')
const q = new Queue()

q.getJob('5127d082-fe7e-4e88-b1de-7093029695c3').then((savedJobs) => {
  delete savedJobs[0].status = 'disabled'
  return savedJobs[0].update(null, 'Job disabled')
}).catch(err => console.error(err))

The example above is setting the Job.status property to disabled which is an invalid status. See the Job Status document for a list of the status values that are valid. By setting the status to an invalid value, the internal getNextJob() method will not retrieve the job; hence it is disabled.

It may be interesting to note that the Queue.summary method will return a list of status values now including disabled.

Note: The getNextJob() internal method uses an index which returns only jobs with a waiting or failed status.

Other ways you could disable a job would be to set the dateEnable property to some ridiculous time in the future, say 200 years.

Main

How It Works

Contributing

API

Queue Methods

Queue Properties

Queue Events

Job Methods

Job Properties

Documentation

Clone this wiki locally