Retrying jobs
BullMQ provides a retry method that allows you to programmatically retry jobs that have already completed or failed. This is different from the automatic retry mechanism (configured via the attempts option) - the retry method lets you manually move a job back to the waiting queue at any time.
When to use Job.retry()
The retry method is useful in scenarios such as:
Manual intervention: When a job failed due to a temporary external issue that has been resolved
Re-processing completed jobs: When you need to run a completed job again with the same data
Workflow recovery: When recovering from system failures or bugs that caused jobs to fail incorrectly
Basic Usage
import { Queue, Job } from 'bullmq';
const queue = new Queue('my-queue');
// Get a failed job by ID
const job = await Job.fromId(queue, 'job-id');
// Retry a failed job (default state is 'failed')
await job.retry();
// Retry a completed job
await job.retry('completed');from bullmq import Queue, Job
queue = Queue('my-queue')
# Get a failed job by ID
job = await Job.fromId(queue, 'job-id')
# Retry a failed job (default state is 'failed')
await job.retry()
# Retry a completed job
await job.retry('completed')
await queue.close()Retry Options
The retry method accepts options to reset attempt counters. This is useful when you want the retried job to behave as if it's being processed for the first time.
Reset Attempts Made
The attemptsMade counter tracks how many times a job has been processed. Resetting it allows the job to use its full retry allowance again.
Reset Attempts Started
The attemptsStarted counter tracks how many times a job has been moved to the active state. This can be useful for tracking purposes.
What happens when you retry
When a job is retried, the following occurs:
Job is moved to waiting queue: The job is removed from the completed/failed set and added back to the waiting queue
Properties are cleared: The following job properties are reset:
failedReason/failed_reason→null/nilfinishedOn/finished_on→null/nilprocessedOn/processed_on→null/nilreturnvalue/return_value→null/nil
Events are emitted: A
waitingevent is emitted when the job is successfully movedParent dependencies restored: If the job is a child in a flow, its dependency relationship with the parent is restored
If you retry a job without resetting attemptsMade, and the job has already exhausted its retry attempts, it will fail immediately when processed again.
Error Handling
The retry method can fail in the following cases:
-1
Job does not exist
-3
Job was not found in the expected state
Read More
💡 Retrying Failing Jobs - Automatic retry configuration with backoff strategies
💡 Stop Retrying Jobs - How to prevent further retries
Last updated
Was this helpful?