Skip to content

Queue.createJob

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

Method Signature

Queue.createJob()

Returns: Object

  • A single JavaScript Job object.

Example:

let job = q.createJob()

Queue.createJob(data)

Parameter: Job data AND/OR Job Options Object or Value

  • A JavaScript object to populate the job data, customize the options for the job, or both.

Returns: Object

  • A single JavaScript Job object.

Example:

const optAndData = {
  priority: 'high',
  data: 'Job Processing Information'
}
let job = q.createJob(optAndData)

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 by passing in a data value or object, or adding properties.

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.

Create Job with Custom Data and 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()

// job.recipient will be set to 'flash@fast.net'
const job = q.createJob({ recipient: 'flash@fast.net' })

// You can also add properties directly.
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 'flash@fast.net'
  console.log(savedJobs[0].subject) // Logs 'Registration for superheros.com'
}).catch(err => console.error(err))

Create Job with Custom Data and Options

This example will add data to the job and customize it with different options than the default. See the Queue.jobOptions property for how to set default options.

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

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

Create Job with Custom Data as a Value

Here is an example of creating a job by using a string. If you pass a value type to the Queue.createJob method, the value will be set against the Job.data property. By default the Job.data property does not exist. It is only created if you pass a value type to the Queue.createJob method.

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

// job.data will be set to 'flash@fast.net'
const job = q.createJob('flash@fast.net')

q.addJob(job).then((savedJobs) => {
  // savedJobs is an array of one item which is a valid job object
  console.log(savedJobs[0].data) // Logs 'flash@fast.net'
}).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