Cancelling jobs
How It Works
import { Worker } from 'bullmq';
const worker = new Worker('myQueue', async (job, token, signal) => {
// The signal parameter is optional and provides cancellation support
// Your job processing logic here
});Cancelling Jobs
// Cancel a specific job by ID
const cancelled = worker.cancelJob('job-id-123');
console.log('Job cancelled:', cancelled); // true if job was active, false otherwise
// Cancel with a reason (useful for debugging)
worker.cancelJob('job-id-456', 'User requested cancellation');
// Cancel all active jobs
worker.cancelAllJobs();
// Cancel all with a reason
worker.cancelAllJobs('System shutdown');
// Get list of active jobs from queue
const activeJobs = await queue.getActive();
console.log(
'Active jobs:',
activeJobs.map(j => j.id),
);Cancellation Reasons
Handling Cancellation (Recommended Pattern)
Why Event-Based?
Using with Native APIs (Recommended)
APIs That Support AbortSignal
Cancelling Custom Operations
Async Cleanup on Cancellation
Alternative: Polling Pattern
Job State After Cancellation
With Regular Error (Will Retry)
With UnrecoverableError (No Retry)
Handling Lock Renewal Failures
Why This Pattern Works
Multi-Phase Work with Cancellation
Backward Compatibility
Best Practices
Read more:
Last updated
Was this helpful?