Skip to content

Queue.findJob

Grant Carthew edited this page Sep 29, 2016 · 11 revisions

Method Signature

Queue.findJob(predicate)

Parameter: predicate Object or Function

  • predicate can be a simple object or a complex function.
  • If a function is passed you are restricted to using RethinkDBs ReQL expressions.

Returns: Array

  • If your predicate is successful, an Array of Job objects will be returned. If not and empty Array is returned.

Example:

q.findJob({ email: 'batman@batcave.org' }).then((jobs) => {
  // jobs will either be an empty array
  // or an array of Job objects that have an
  // email property equal to 'batman@batcave.org'
}).catch(err => console.error(err))

Description

One of the most flexible methods on the Queue objects is the Queue.findJob. This method exposes the full and rich filter functionality of the RethinkDB ReQL (RethinkDB Query Language).

Using the Queue.findJob method you can extract information about the jobs in the queue. Here is a quick list of information you could obtain using Queue.findJob.

  • Confirm a job has not already been added to the queue (ensure unique job).
  • Get all jobs related to a common task.
  • Get all failed or terminated jobs.
  • Produce reports on any job data.

Examples

This example shows how to ensure a job has not already been added to the queue before adding it. You will need some unique identification value assigned to the jobs to be able to find them. In this example there is a Job.hash property however you can use any key/value you like. If it is not a unique property then multiple results will be returned.

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

let job = q.createJob()
job.hash = '12345678' // Some know hash value

q.findJob({ hash: '12345678' }).then((exists) => {
  if (exists.length < 1) {
    return q.addJob(job)
  }
  return
}).catch(err => console.error(err))

This example shows how to use a function as the predicate. The function will return all jobs that completed between January 1st, 2012 (included) and January 1st, 2013 (excluded).

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

const predicate = function (job) {
  return job('dateCompleted').during(
    r.time(2012, 1, 1, 'Z'), r.time(2013, 1, 1, 'Z'))
}

q.findJob(predicate).then((jobs) => {
  // Zero or more jobs in an array.
}).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