Skip to content

Delayed Job

Grant Carthew edited this page Nov 21, 2016 · 17 revisions

What is dateEnable?

One of the extremely important properties of the rethinkdb-job-queue Job objects is the dateEnable property. Every job in the queue will have a dateEnable property and be populated with a date time value.

When the Queue object is processing jobs in the queue backing Table it runs an internal function called getNextJob(). This function queries the RethinkDB database for jobs that need processing. If the dateEnable property of a job in the queue has a date that is set to some time in the future, the job will not be retrieved.

This gives us the flexibility to be able to set the date and time we want the job to be processed prior to adding the job to the queue.

Warning: Jobs that are delayed in a relatively inactive queue will only be processed at the Queue Master review time interval. Because of this your delayed jobs will only run after the dateEnable date and on the next Queue Master review process. This will not be an issue if the queue is very busy. Please read the Queue Master document for a better understanding of this process.

Examples

Without a custom dateEnable value

This example is to show you what a job looks like without changing any properties of the job.

Firstly we need to create a job and add it to the queue.

const Queue = require('rethinkdb-job-queue')
const q = new Queue()
let job = q.createJob({ data: 'foo' })

q.addJob(job).catch(err => console.error(err))

The above code will add a job to the queue without changing any of the jobs properties. Notice the job has a custom data property set to "foo".

Here is the job data that is stored in the queue database.

{
  "dateCreated": Sat Aug 20 2016 21:26:32 GMT+00:00 ,
  "dateEnable": Sat Aug 20 2016 21:26:32 GMT+00:00 ,
  "data": "foo",
  "id":  "2250b4fc-5167-49ae-a139-eff48b7b1580" ,
  "log": [{
    "data": null ,
    "date": Sat Aug 20 2016 21:27:03 GMT+00:00 ,
    "message":  "Job added to the queue" ,
    "processCount": 0 ,
    "queueId":  "WebDev:rjqJobQueue:rjqJobList:18532:93ea5586-f840-4b03-9acc-ef3fc9260858" ,
    "retryCount": 0 ,
    "status":  "waiting" ,
    "type":  "information"
  }] ,
  "priority": 40 ,
  "processCount": 0 ,
  "progress": 0 ,
  "queueId":  "WebDev:rjqJobQueue:rjqJobList:18532:93ea5586-f840-4b03-9acc-ef3fc9260858" ,
  "repeat": false ,
  "repeatDelay": 300000 ,
  "retryCount": 0 ,
  "retryDelay": 600000 ,
  "retryMax": 3 ,
  "status":  "waiting" ,
  "timeout": 300000
}

With the above job you will notice the dateEnable property that is set to the same value as the dateCreated property. This is due to the Job object constructor that sets both properties to the current date time.

The above job will be processed immediately.

Delaying a job with a custom dateEnable value

This example changes the dateEnable property value after the Job object is created to delay the job start by three days. Note the extra function called addDays. If you are going to do lots of date manipulation consider the moment.js library.

function addDays (days) {
    var result = new Date();
    result.setDate(result.getDate() + days * 86400000);
    return result;
}

const Queue = require('rethinkdb-job-queue')
const q = new Queue()
let job = q.createJob({ data: 'foo' })

job.dateEnable = addDays(3)
q.addJob(job).catch(err => console.error(err))

The above code will create a Job object, set the job custom data to "foo", and set the dateEnable property to three days from now.

Here is what the job data will look like.

{
  "dateCreated": Sat Aug 20 2016 21:26:32 GMT+00:00 ,
  "dateEnable": Sat Aug 23 2016 21:26:32 GMT+00:00 ,
  "data": "foo",
  "id":  "2250b4fc-5167-49ae-a139-eff48b7b1580" ,
  "log": [{
    "date": Sat Aug 20 2016 21:27:03 GMT+00:00 ,
    "message":  "Job added to the queue" ,
    "processCount": 0 ,
    "queueId":  "WebDev:rjqJobQueue:rjqJobList:18532:93ea5586-f840-4b03-9acc-ef3fc9260858" ,
    "retryCount": 0 ,
    "status":  "waiting" ,
    "type":  "information"
  }] ,
  "priority": 40 ,
  "processCount": 0 ,
  "progress": 0 ,
  "queueId":  "WebDev:rjqJobQueue:rjqJobList:18532:93ea5586-f840-4b03-9acc-ef3fc9260858" ,
  "repeat": false ,
  "repeatDelay": 300000 ,
  "retryCount": 0 ,
  "retryDelay": 600000 ,
  "retryMax": 3 ,
  "status":  "waiting" ,
  "timeout": 300000
}

You will notice in the above example job data that the dateCreated property is set to the 20th of August while the dateEnable property is set to the 23rd of August.

This job example will not be processed until after the 23rd of August 21:26:32. This does not mean the job will be processed immediately. If the queue is very busy, then the job will be started very close to the dateEnable date time. If the queue is not busy, the job will be processed on the next Queue Master review interval.

Main

How It Works

Contributing

API

Queue Methods

Queue Properties

Queue Events

Job Methods

Job Properties

Documentation

Clone this wiki locally