Skip to main content

Overview

The Job class represents a job in the queue. Jobs are normally created implicitly when you add a job to the queue with Queue.add(), but a Job instance is also passed to the Worker’s process function.

Constructor

new Job(
  queue: MinimalQueue,
  name: string,
  data: any,
  opts?: JobsOptions,
  id?: string
)
Jobs are typically created using Queue.add() rather than instantiating directly.

Properties

id

id?: string
The unique identifier of the job.

name

name: string
The name of the job.

data

data: any
The payload data for this job.

opts

opts: JobsOptions
The options object for this job.

progress

progress: number | object = 0
The progress a job has performed so far.

returnvalue

returnvalue: any = null
The value returned by the processor when processing this job.

stacktrace

stacktrace: string[] = null
Stacktrace for the error (for failed jobs).

timestamp

timestamp: number
Timestamp when the job was created.

attemptsMade

attemptsMade: number = 0
Number of attempts after the job has failed.

attemptsStarted

attemptsStarted: number = 0
Number of times job has been moved to active state.

delay

delay: number = 0
An amount of milliseconds to wait until this job can be processed.

priority

priority: number = 0
Ranges from 0 (highest priority) to 2,097,152 (lowest priority).

processedOn

processedOn?: number
Timestamp for when the job was processed.

finishedOn

finishedOn?: number
Timestamp for when the job finished (completed or failed).

failedReason

failedReason: string
Reason for failing.

Static Methods

create

Creates a new job and adds it to the queue.
static create<T, R, N>(
  queue: MinimalQueue,
  name: N,
  data: T,
  opts?: JobsOptions
): Promise<Job<T, R, N>>

createBulk

Creates a bulk of jobs and adds them atomically to the queue.
static createBulk<T, R, N>(
  queue: MinimalQueue,
  jobs: Array<{ name: N; data: T; opts?: JobsOptions }>
): Promise<Job<T, R, N>[]>

fromId

Fetches a Job from the queue by ID.
static fromId<T, R, N>(
  queue: MinimalQueue,
  jobId: string
): Promise<Job<T, R, N> | undefined>

Methods

updateProgress

Updates a job’s progress.
updateProgress(progress: number | object): Promise<void>
progress
number | object
required
Number or object to be saved as progress

updateData

Updates a job’s data.
updateData(data: any): Promise<void>
data
any
required
The data that will replace the current job’s data

log

Logs one row of log data.
log(logRow: string): Promise<number>
logRow
string
required
String with log data to be logged
count
number
The total number of log entries for this job

remove

Completely removes the job from the queue.
remove(opts?: { removeChildren?: boolean }): Promise<void>
opts.removeChildren
boolean
default:"true"
Whether to remove child jobs

retry

Attempts to retry the job.
retry(state?: 'completed' | 'failed', opts?: RetryOptions): Promise<void>
state
string
default:"'failed'"
The state from which to retry (completed or failed)
opts
RetryOptions
Retry options

promote

Promotes a delayed job so that it starts to be processed as soon as possible.
promote(): Promise<void>

changeDelay

Changes the delay of a delayed job.
changeDelay(delay: number): Promise<void>
delay
number
required
Milliseconds from now when the job should be processed

changePriority

Changes the priority of a job.
changePriority(opts: { priority?: number; lifo?: boolean }): Promise<void>
opts.priority
number
New priority value
opts.lifo
boolean
Whether to add to the left (LIFO) or right (FIFO)

getState

Gets the current state of the job.
getState(): Promise<JobState | 'unknown'>
state
string
One of: ‘completed’, ‘failed’, ‘delayed’, ‘active’, ‘waiting’, ‘waiting-children’, ‘unknown’

isCompleted

Checks if the job has completed.
isCompleted(): Promise<boolean>

isFailed

Checks if the job has failed.
isFailed(): Promise<boolean>

isDelayed

Checks if the job is delayed.
isDelayed(): Promise<boolean>

isActive

Checks if the job is active.
isActive(): Promise<boolean>

isWaiting

Checks if the job is waiting.
isWaiting(): Promise<boolean>

isWaitingChildren

Checks if the job is waiting for children.
isWaitingChildren(): Promise<boolean>

getChildrenValues

Gets the result values of child jobs.
getChildrenValues<T>(): Promise<{ [jobKey: string]: T }>

getDependencies

Gets children job keys if this job is a parent.
getDependencies(opts?: DependenciesOpts): Promise<{
  processed?: Record<string, any>;
  unprocessed?: string[];
  failed?: string[];
  ignored?: Record<string, any>;
}>

getDependenciesCount

Gets children job counts if this job is a parent.
getDependenciesCount(opts?: {
  processed?: boolean;
  unprocessed?: boolean;
  failed?: boolean;
  ignored?: boolean;
}): Promise<{
  processed?: number;
  unprocessed?: number;
  failed?: number;
  ignored?: number;
}>

waitUntilFinished

Returns a promise that resolves when the job has completed or rejects when failed.
waitUntilFinished(queueEvents: QueueEvents, ttl?: number): Promise<any>
queueEvents
QueueEvents
required
Instance of QueueEvents to listen for completion
ttl
number
Time in milliseconds to wait before timing out

moveToCompleted

Moves a job to the completed state.
moveToCompleted(
  returnValue: any,
  token: string,
  fetchNext?: boolean
): Promise<void | any[]>

moveToFailed

Moves a job to the failed state.
moveToFailed(
  err: Error,
  token: string,
  fetchNext?: boolean
): Promise<void | any[]>

extendLock

Extends the lock for this job.
extendLock(token: string, duration: number): Promise<number>
token
string
required
Unique token for the lock
duration
number
required
Lock duration in milliseconds

Build docs developers (and LLMs) love