Skip to content

Job Processing

Grant Carthew edited this page Feb 7, 2017 · 7 revisions

Description

To process jobs within rethinkdb-job-queue you need to supply a handler function to Queue.process.

This handler function should look something like this:

const worldPeace = require('world-peace')

function saveTheWorld (job, next, onCancel) {

  // job will hold the values you need to process the job.
  worldPeace.addSuperHero(job.hero)

  // onCancel will be called by the Queue object if the job
  // is cancelled in the queue.
  onCancel(job, () => {
    // Gracefully cancel your job.
    return worldPeace.cancel()
  })

  worldPeace.process().then((success) => {
    // If the job processing succeeds, call next with the results.
    // Pass null for the error.
    return next(null, success)
  }).catch((err) => {
    // If the job processing fails, call next with an error.
    return next(err)
  })

}

To make use of the above handler function you need to add it to the Queue object as follows:

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

q.process(saveTheWorld)

Now the Queue object is ready to process jobs. Simply add a job to the queue:

let job = q.createJob({ hero: 'Spider-Man' })
q.addJob(job).catch(err => console.error(err))

The Queue object will now retrieve the job from the database and process it. If all goes well there will be world peace.

Related Documents

Main

How It Works

Contributing

API

Queue Methods

Queue Properties

Queue Events

Job Methods

Job Properties

Documentation

Clone this wiki locally