-
Notifications
You must be signed in to change notification settings - Fork 16
Cancel Job
For any number of reasons you may need to cancel a job currently in the queue. There are two ways you can do this; using Queue.cancelJob, or by calling next() passing a customized Error
object.
This document explains the differences and when to use them.
When a job is no longer valid and has not been completed
, cancelled
, or terminated
. See the Job Status for more detail on these status values.
In a distributed processing environment this will cancel the job for all Queue objects connected to the queue. If another Queue object is currently processing this job, depending on the user defined onCancel(callback)
function, it will gracefully stop the job processing.
The Queue.cancelJob
method can be called while a job is in the queue with a waiting
, failed
, or active
status. By calling the Queue.cancelJob method you will cause the job to change status to cancelled
which will permanently prevent the job from being picked up for processing.
Note: You can reanimate jobs. See the Job Editing document for more detail.
If you are using rethinkdb-job-queue
in a distributed environment and you have the change feed option enabled for your worker Queue objects, calling Queue.cancelJob
will be received by all Queue objects. If you cancel the job from a Queue object that is not processing the job, and the job is being processed by another Queue object, a Queue.process callback called onCancel
will be invoked. This will allow the Queue object processing the job to gracefully stop the processing of the job.
The next()
function is presented to your job processing code when you call Queue.process. Cancel the job by calling next(err)
with a customized Error object if the job processing fails and will never be able to be retried.
This method for cancelling a job is called whilst the job is being processed. It will change the job status to cancelled
in the queue, permanently preventing further processing.
Calling Queue.cancelJob(job)
above is a great way of cancelling a job however it is achieved from outside the processing function for the job.
If you have a need to cancel a job from within the processing handler function, then you can pass an Error object to the next()
function with a custom property on the Error object.
Here is how you would construct the Error object.
let err = new Error('Something went wrong')
err.cancelJob = 'Processing error occurred'
next(err)
The next()
function checks for the Error.cancelJob
property and if it exists will cancel the job in the queue. You would place this code within your jobs processing error handling function.
For documentation purposes, the value of the Error.cancelJob
property being a string, number, or object, will be logged against the job in the database. See Job.log for more detail.
Note: The onCancel
function does not get invoked if you cancel a job from within the processing code.
This example shows cancelling a job in the queue using Queue.cancelJob
. Also within this code you can see there is a catch block which will call next(err)
with the cancelJob
property.
const warpEngine = require('warpengine')
const Queue = require('rethinkdb-job-queue')
const q = new Queue()
let job = q.createJob()
job.warpEngineParameters = [254,387,984]
q.addJob(job).catch(err => console.error(err))
q.process((job, next, onCancel) => {
// Process your job here
warpEngine.engage(job.warpEngineParameters).then(() => {
// Call next() when done passing null for the error
next(null, 'Warp factor 9')
}).catch(err => {
// This will cancel the job preventing retries
err.cancelJob = 'Warp engine failure'
return next(err)
})
// The following callback will only be invoked
// if the job is cancelled from outside this function
onCancel(job, () => {
// Gracefully stop your job here
warpEngine.shutdown()
})
})
// Elsewhere in your code, cancel jobs.
// This can be on another process or host as long
// as the changeFeed option is enabled.
q.cancelJob(job).catch(err => console.error(err))
- 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