Skip to content

Queue.addJob

Grant Carthew edited this page Oct 16, 2016 · 11 revisions

Method Signature

Queue.addJob(Job)

Parameter: Job

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 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 Queue.createJob and Queue.addJob?

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

This allows you to create large numbers of jobs 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

Note: The example below does not include Queue.process so the jobs will be committed to the database successfully however they will never be processed.

Multiple Jobs with Custom Options

This example creates 100 Job objects and adds them to the Queue with one call to Queue.addJob(jobs).

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

const root = '\mnt\Store\ProcessFile'
const jobs = []

for (let i = 0; i < 100; i++) {
  jobs.push(q.createJob({ path: root + i })
             .setPriority('highest')
             .setRetryMax(2))
}

// jobs is an array of 100 Job objects
// This is the only time the database is touched inserting 100 jobs
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