Basic Worker Creation
Create a worker by instantiating theWorker class with a queue name and a processor function:
Processor Function Signature
The processor function receives up to three parameters:- job: The job instance containing data and methods
- token: Optional lock token for the job
- signal: Optional AbortSignal for job cancellation
Example with All Parameters
Worker Options
Configure worker behavior with options:Key Options
Number of jobs that can be processed in parallel. See Concurrency.
Whether to start processing jobs immediately upon worker creation.
Duration in milliseconds that a worker holds a lock on a job.
Maximum times a job can be recovered from stalled state before moving to failed.
Interval in milliseconds between stalled job checks.
Automatically remove completed jobs. See Auto-removal.
Automatically remove failed jobs. See Auto-removal.
Manual Worker Control
Control when the worker starts processing:Worker Events
Listen to worker events to monitor job processing:Progress Reporting
Report job progress from within the processor:TypeScript Generics
Define types for job data and return values:Waiting Until Ready
Wait for the worker’s Redis connection to be ready:Graceful Shutdown
Properly close the worker:Global Job Events
Monitor jobs across all workers usingQueueEvents:
Best Practices
1
Always handle errors
Attach an error listener to prevent crashes:
2
Keep processors async
Use
async/await or return promises to avoid blocking the event loop.3
Report progress for long jobs
Help monitor long-running jobs by calling
job.updateProgress().4
Handle shutdown gracefully
Call
worker.close() on process termination:5
Use sandboxing for CPU-intensive work
See Sandboxed Processors for CPU-heavy tasks.
Related Topics
Concurrency
Process multiple jobs in parallel
Sandboxed Processors
Isolate CPU-intensive processors
Graceful Shutdown
Properly close workers
Cancelling Jobs
Cancel jobs in progress
