-
Notifications
You must be signed in to change notification settings - Fork 16
Delayed Job
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.
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.
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.
- Introduction
- Tutorial
- Queue Constructor
- Queue Connection
- Queue Options
- Queue PubSub
- Queue Master
- Queue Events
- State Document
- Job Processing
- Job Options
- Job Status
- Job Retry
- Job Repeat
- Job Logging
- Job Editing
- Job Schema
- Job Name
- Complex Job
- Delayed Job
- Cancel Job
- Error Handling
- Queue.createJob
- Queue.addJob
- Queue.getJob
- Queue.findJob
- Queue.findJobByName
- Queue.containsJobByName
- Queue.cancelJob
- Queue.reanimateJob
- Queue.removeJob
- Queue.process
- Queue.review
- Queue.summary
- Queue.ready
- Queue.pause
- Queue.resume
- Queue.reset
- Queue.stop
- Queue.drop
- Queue.Job
- Queue.host
- Queue.port
- Queue.db
- Queue.name
- Queue.r
- Queue.id
- Queue.jobOptions [R/W]
- Queue.changeFeed
- Queue.master
- Queue.masterInterval
- Queue.removeFinishedJobs
- Queue.running
- Queue.concurrency [R/W]
- Queue.paused
- Queue.idle
- Event.ready
- Event.added
- Event.updated
- Event.active
- Event.processing
- Event.progress
- Event.log
- Event.pausing
- Event.paused
- Event.resumed
- Event.completed
- Event.cancelled
- Event.failed
- Event.terminated
- Event.reanimated
- Event.removed
- Event.idle
- Event.reset
- Event.error
- Event.reviewed
- Event.detached
- Event.stopping
- Event.stopped
- Event.dropped
- Job.setName
- Job.setPriority
- Job.setTimeout
- Job.setDateEnable
- Job.setRetryMax
- Job.setRetryDelay
- Job.setRepeat
- Job.setRepeatDelay
- Job.updateProgress
- Job.update
- Job.getCleanCopy
- Job.addLog
- Job.getLastLog