Skip to content

Queue.createJob

Grant Carthew edited this page Sep 27, 2016 · 27 revisions

Method Signature

Queue.createJob()

Returns: Object

  • A single JavaScript Job object.

Example:

let job = q.createJob()

Queue.createJob(Options)

Parameter: Options Object

  • A JavaScript object to customize the options for the job.

Returns: Object

  • A single JavaScript Job object.

Example:

const options = {
  priority: 'high'
}
let job = q.createJob(options)

Queue.createJob(Number)

Parameter: Number Integer

  • An integer used to create a batch of Job objects.
  • A valid Number will be an integer > 0. There is no upper limit.

Returns: [Object]

  • An array of JavaScript Job objects.

Example:

let jobs = q.createJob(100)

Queue.createJob(Options, Number)

Parameter: Options Object

  • A JavaScript object to customize the options for the jobs.

Parameter: Number Integer

  • An Integer used to create a batch of Job objects.
  • A valid Number will be an integer > 0. There is no upper limit.

Returns: [Object]

  • An array of JavaScript Job objects.

Example:

const options = {
  priority: 'high'
}
let jobs = q.createJob(options, 100)

Description

To add jobs to the queue you need to have the basic object structure required for rethinkdb-job-queue to be able to work with the data.

You use the Queue.createJob() method to produce a job object that looks like the following:

{
  q: [object Object], // This is a reference to the Queue that created the job
  id: '07290a09-269b-4211-9698-ea40f69962b2', // This will be the jobs id in the database
  priority: 'normal',
  timeout: 300000,
  retryDelay: 600000,
  retryMax: 3,
  retryCount: 0,
  progress: 0,
  status: 'created',
  log: [],
  dateCreated: 2016-07-27T22:54:45.811Z,
  dateEnable: 2016-07-27T22:54:45.811Z,
  queueId: 'WebDev:rjqJobQueue:rjqJobList:11761:89c2d49e-5a04-4d9b-b08e-48908625ea64'
}

You will notice there are no properties to hold your job data. You can decorate the job object with any properties you like.

After populating the job object with your job details you will be required to call Queue.addJob to add the job to the queue. Until you call Queue.addJob the job does not exist in the database and will not be queued for processing.

Examples

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

Single Job with Custom Properties

Here is an example of creating a single job and adding it to the Queue using custom job properties:

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

job.recipient = '[email protected]'
job.subject = 'Registration for superheros.com'
job.body = 'Click this link to activate your account on superheros.com'

q.addJob(job).then((savedJobs) => {
  // savedJobs is an array of one item which is a valid job object
  console.log(savedJobs[0].recipient) // Logs '[email protected]'
}).catch((err) => {
  console.error(err)
})

Single Job with Payload Property

Here is another example using the Job.setPayload method. This will create a Job.payload property containing the custom job data:

const Queue = require('rethinkdb-job-queue')
const q = new Queue()
const job = q.createJob().setPayload({ path: '\mnt\Videos\xyz.vid' })

q.addJob(job).then((savedJobs) => {
  // savedJobs is an array of one item which is a valid job object
  console.dir(savedJobs[0].payload) // Logs { path: '\mnt\Videos\xyz.vid' }
}).catch((err) => {
  console.error(err)
})

Single Job with Custom Options

This example will customize the created job with different options than the default:

const Queue = require('rethinkdb-job-queue')
const q = new Queue()
const options = {
  priority: 'highest',
  retryMax: 2
}
const job = q.createJob(options).setPayload({ path: '\mnt\Videos\xyz.vid' })

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

Multiple Jobs with Default Options

Here we are using the Number parameter to create multiple jobs:

const Queue = require('rethinkdb-job-queue')
const q = new Queue()
const jobs = q.createJob(100)

for (let j of jobs) {
  j.data = 'information' // Any job data or payload
}

q.addJob(jobs).then((savedJobs) => {
  // savedJobs is an array of valid job objects
  console.dir(savedJobs[0].data) // Logs 'information'
}).catch((err) => {
  console.error(err)
})

Multiple Jobs with Custom Options

This example will create multiple jobs with different options than the default:

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