Skip to content

Queue.addJob

Grant Carthew edited this page Jul 28, 2016 · 11 revisions

Method Signature

Queue.addJob(Job)

Parameter: Job - A single Job object or an Array of Job objects from Queue.createJob.

Returns: Promise => Array - An array of one or more Job objects.

Example:

q.addJob(jobs).then((savedJobs) => {
  // savedJobs is an array of one or more Jobs
}).catch((err) => {
  console.error(err)
})

Description

To use rethinkdb-job-queue you need to add Job objects to the Queue. Job objects can be created by using the Queue.createJob method.

After calling Queue.createJob you will have a valid Job object which is only stored in local memory. This Job object can now be populated with any data you will need to process carry out the task of the job.

At this point in time, the Job is not in the Queue and will not be processed. Calling Queue.addJob(job) will save the job into the backing database table and expose the job to any node available to process it.

Why separate createJob and addJob?

To reduce database touch points the createJob Queue method only creates Job objects locally in the node the Queue process is on. If you do not add the Job objects to the Queue by calling addJob they will never be saved into the backing database.

This allows you to create multiple jobs, many millions of job object, without causing inserts into the database. Once you have configured the multiple jobs with the job data, simply add all the jobs to the Queue at once. By passing an array of jobs to Queue.addJob(jobs) they will all be saved to the database with one insert.

Examples

Multiple Jobs with Custom Options

const Queue = require('rethinkdb-job-queue')
const q = new Queue()
const options = {
  priority: 'highest',
  retryMax: 2
}
const jobs = q.createJob(options, 100)

jobs.map((j, i) => {
  j.path = '\mnt\Store\ProcessFile' + i
})

q.addJob(jobs).then((savedJobs) => {
  // savedJobs is an array of valid job objects
  console.dir(savedJobs[0].path) // Logs { path: '\mnt\Store\ProcessFile0' }
}).catch((err) => {
  console.error(err)
})

Main

How It Works

Contributing

API

Queue Methods

Queue Properties

Queue Events

Job Methods

Job Properties

Documentation

Clone this wiki locally