Skip to main content

Job

Job

A Job represents a piece of work to be run in the background, i.e. outside the request-response cycle. It is intended to be used for long-running work triggered by API requests. Jobs should not generally be directly instantiated. Rather, the JobQueue add() method should be used to create and add a new Job to a queue.

Signature
class Job<T extends JobData<T> = any> {
readonly id: number | string | null;
readonly queueName: string;
readonly retries: number;
readonly createdAt: Date;
name: string
data: T
state: JobState
progress: number
result: any
error: any
isSettled: boolean
startedAt: Date | undefined
settledAt: Date | undefined
duration: number
attempts: number
constructor(config: JobConfig<T>)
start() => ;
setProgress(percent: number) => ;
complete(result?: any) => ;
fail(err?: any) => ;
cancel() => ;
defer() => ;
on(eventType: JobEventType, listener: JobEventListener<T>) => ;
off(eventType: JobEventType, listener: JobEventListener<T>) => ;
}

id

property
number | string | null

queueName

property
string

retries

property
number

createdAt

property
Date

name

property
string

data

property
T

state

property

progress

property
number

result

property
any

error

property
any

isSettled

property
boolean

startedAt

property
Date | undefined

settledAt

property
Date | undefined

duration

property
number

attempts

property
number

constructor

method
(config: JobConfig<T>) => Job

start

method
() =>

Calling this signifies that the job work has started. This method should be called in the JobQueueStrategy next() method.

setProgress

method
(percent: number) =>

Sets the progress (0 - 100) of the job.

complete

method
(result?: any) =>

Calling this method signifies that the job succeeded. The result will be stored in the Job.result property.

fail

method
(err?: any) =>

Calling this method signifies that the job failed.

cancel

method
() =>

defer

method
() =>

Sets a RUNNING job back to PENDING. Should be used when the JobQueue is being destroyed before the job has been completed.

on

method
(eventType: JobEventType, listener: JobEventListener<T>) =>

Used to register event handler for job events

off

method
(eventType: JobEventType, listener: JobEventListener<T>) =>

JobEventType

An event raised by a Job.

Signature
type JobEventType = 'progress'

JobEventListener

The signature of the event handler expected by the Job.on() method.

Signature
type JobEventListener<T extends JobData<T>> = (job: Job<T>) => void