Skip to main content

Overview

The Worker class represents a worker that processes jobs from the queue. As soon as the class is instantiated and a connection to Redis is established, it will start processing jobs.

Constructor

name
string
required
The name of the queue to process
processor
Processor | string | URL
The processor function or path to processor file
opts
WorkerOptions
Configuration options for the worker

Example

Properties

id

Unique identifier for this worker instance.

concurrency

The maximum number of jobs this worker will process concurrently.

opts

The worker configuration options.

Methods

run

Starts processing jobs from the queue.

pause

Pauses the processing of jobs for this worker.
doNotWaitActive
boolean
default:"false"
If true, does not wait for active jobs to finish

resume

Resumes processing jobs after being paused.

isPaused

Checks if the worker is currently paused.

isRunning

Checks if the worker is currently running.

getNextJob

Manually fetches the next job from the queue.
token
string
required
Worker token to assign to retrieved job
opts.block
boolean
default:"true"
Whether to block waiting for a job

cancelJob

Cancels a specific job currently being processed.
jobId
string
required
The ID of the job to cancel
reason
string
Optional reason for cancellation
cancelled
boolean
True if the job was found and cancelled

cancelAllJobs

Cancels all jobs currently being processed by this worker.
reason
string
Optional reason for cancellation

close

Closes the worker and related Redis connections.
force
boolean
default:"false"
If true, does not wait for current jobs to be processed

startStalledCheckTimer

Manually starts the stalled job checker.

Static Methods

RateLimitError

Creates a rate limit error to throw from a processor.

Events

The Worker class extends EventEmitter and emits the following events:

active

Emitted when a job enters the active state.

completed

Emitted when a job has successfully completed.

failed

Emitted when a job has failed.

progress

Emitted when a job updates its progress.

drained

Emitted when the queue has drained the waiting list.

paused

Emitted when the worker is paused.

resumed

Emitted when the worker is resumed.

stalled

Emitted when a job has stalled.

error

Emitted when an error occurs.

closing

Emitted when the worker is closing.

closed

Emitted when the worker has closed.