A job includes all data needed to perform its execution, as well as metadata about its current state and execution history.
job.data
The data object that was passed to Queue#add() when creating the job. This is the primary payload used to perform the job.
job.id
The unique identifier for the job. By default, this is an auto-incrementing integer, but can be customized using the jobId option when adding a job.
const job = await queue . add ( 'email' , data , { jobId: 'unique-email-123' });
console . log ( job . id ); // 'unique-email-123'
job.opts
The job options that were specified when the job was created.
Configuration options for the job Priority value. Ranges from 1 (highest priority) to MAX_INT (lowest priority)
Milliseconds to wait until this job can be processed
Total number of attempts to try the job until it completes
Backoff setting for automatic retries if the job fails
If true, adds the job to the right of the queue instead of the left
Number of milliseconds after which the job should fail with a timeout error
removeOnComplete
boolean | number | KeepJobs
If true, removes the job when it successfully completes
removeOnFail
boolean | number | KeepJobs
If true, removes the job when it fails after all attempts
const job = await queue . add ( 'email' , data , {
attempts: 3 ,
backoff: { type: 'exponential' , delay: 1000 },
removeOnComplete: true
});
console . log ( job . opts . attempts ); // 3
console . log ( job . opts . backoff ); // { type: 'exponential', delay: 1000 }
job.attemptsMade
The number of failed attempts that have been made for this job.
Number of failed attempts
queue . process ( async ( job ) => {
console . log ( `Attempt ${ job . attemptsMade + 1 } of ${ job . opts . attempts } ` );
if ( job . attemptsMade > 0 ) {
console . log ( 'This is a retry' );
}
// Process the job
});
job.finishedOn
Unix timestamp (in milliseconds) when the job was completed or finally failed after all attempts.
Unix timestamp in milliseconds
const job = await queue . getJob ( jobId );
if ( job . finishedOn ) {
const finishedDate = new Date ( job . finishedOn );
console . log ( `Job finished at: ${ finishedDate . toISOString () } ` );
}
Additional Properties
Jobs also contain other useful properties for tracking state:
job.timestamp: When the job was created
job.processedOn: When the job was last processed
job.failedReason: Error message if the job failed
job.stacktrace: Array of stack traces from failed attempts
job.returnvalue: The return value from a completed job
job.name: The job type name (if provided to Queue#add())
toJSON()
Serializes the job to a plain JavaScript object. This is useful for storing or transmitting job data.
Object containing all job properties: Number of failed attempts
Stack traces from failures
Return value if completed
Last processing timestamp
Serializing Jobs
const job = await queue . add ({ user: 'john' , action: 'send-email' });
const jobJSON = job . toJSON ();
console . log ( jobJSON );
// {
// id: '123',
// name: '__default__',
// data: { user: 'john', action: 'send-email' },
// opts: { attempts: 1, delay: 0, ... },
// progress: 0,
// delay: 0,
// timestamp: 1234567890,
// attemptsMade: 0,
// failedReason: null,
// stacktrace: null,
// returnvalue: null,
// finishedOn: null,
// processedOn: null
// }
Storing Job State
// Save job state to database
const job = await queue . getJob ( jobId );
const jobSnapshot = job . toJSON ();
await database . save ( 'job_snapshots' , {
jobId: job . id ,
snapshot: jobSnapshot ,
savedAt: new Date ()
});
API Response
app . get ( '/api/jobs/:id' , async ( req , res ) => {
const job = await queue . getJob ( req . params . id );
if ( ! job ) {
return res . status ( 404 ). json ({ error: 'Job not found' });
}
// Return job as JSON
res . json ( job . toJSON ());
});