Skip to content

Queue.findJobByName

Grant Carthew edited this page Feb 10, 2017 · 5 revisions

Method Signature

Queue.findJobByName(name, raw)

Parameter: name String

  • name can be any string value.

Parameter: raw Boolean

  • True to return raw database data rather than a Job object

Returns: Promise => Array

  • An Array of Job objects or an empty Array.
  • If you set raw, an Array of generic JavaScript objects.

Example:

q.findJobByName('superman').then((jobs) => {
  // jobs will either be an empty array
  // or an array of Job objects that have an
  // name property equal to 'superman'
}).catch(err => console.error(err))

Description

To find generic jobs within the queue you can use the Queue.findJob method. It has one major drawback though, it doesn't use database indexes. This is why the Job.name property and Job.setName methods were added to rethinkdb-job-queue in v2.3.0.

When you assign names to jobs you give yourself a fast method of being able to retrieve those jobs based on a custom string value. By calling Queue.findJobByName you are searching the jobs in your queue table using a database index. This makes Queue.findJobByName very fast with minimum load placed on the RethinkDB database.

Warning: All jobs with the same name value will be returned by the Queue.findJobByName method. This means you may get either an empty array [], an array with one job [job], or an array with many jobs, [job1, job2, job3].

If you are after a unique signature for your jobs, use the automatically generated Job.id. Ids are guaranteed to be unique.

Examples

This example creates a job with a name value of 'superman'. It then finds that job.

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

let job = q.createJob().setName('superman')
// job.name === 'superman'

// findJobByName will return any job that has job.name === 'superman'
q.findJobByName('superman').then((supermen) => {
  if (supermen.length === 1) {
    // Do something with superman
  } else {
    // Is this an error?
  }
}).catch(err => console.error(err))

Related Documents

Main

How It Works

Contributing

API

Queue Methods

Queue Properties

Queue Events

Job Methods

Job Properties

Documentation

Clone this wiki locally