How job cancellation works
mainYou can attempt to cancel a job using cancel(id). The behavior depends on the current state of the job:
- Not Found: Returns
{ status: 'not_found' }. - Completed: Returns
{ status: 'completed' }(cannot cancel a finished job). - Processing: Returns
{ status: 'processing' }. Jobs currently being handled by a worker cannot be cancelled mid-processing. - Queued: The job is cancelled. The library performs an atomic
MULTIblock to:- Delete the job entry from the
{prefix}:jobshash usingHDEL. - Publish a
{id}:cancelledevent to{prefix}:events.
- Delete the job entry from the
Note on Implementation: The library does not perform an $O(n)$ removal from the Redis LIST directly. Instead, it deletes the job from the jobs hash. When a worker eventually picks up the job from the queue, it checks the jobs hash; if the entry is missing, the worker skips the job and removes it from its processing queue.
// Example cancellation
const status = await cancel('job-id');
if (status.status === 'cancelled') {
console.log('Job was successfully cancelled');
}