Skip to content

Commit

Permalink
docs: document some job methods
Browse files Browse the repository at this point in the history
  • Loading branch information
manast committed May 24, 2021
1 parent 9bad41e commit 601810e
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions src/classes/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,19 @@ export class Job<T = any, R = any, N extends string = string> {

constructor(
private queue: MinimalQueue,
/**
* The name of the Job
*/
public name: N,

/**
* The payload for this job.
*/
public data: T,

/**
* The options object for this job.
*/
public opts: JobsOptions = {},
public id?: string,
) {
Expand All @@ -120,6 +131,15 @@ export class Job<T = any, R = any, N extends string = string> {
this.toKey = queue.toKey.bind(queue);
}

/**
* Creates a new job and adds it to the queue.
*
* @param queue the queue where to add the job.
* @param name the name of the job.
* @param data the payload of the job.
* @param opts the options bag for this job.
* @returns
*/
static async create<T = any, R = any, N extends string = string>(
queue: MinimalQueue,
name: N,
Expand All @@ -140,6 +160,13 @@ export class Job<T = any, R = any, N extends string = string> {
return job;
}

/**
* Creates a bulk of jobs and adds them atomically to the given queue.
*
* @param queue the queue were to add the jobs.
* @param jobs an array of jobs to be added to the queue.
* @returns
*/
static async createBulk<T = any, R = any, N extends string = string>(
queue: MinimalQueue,
jobs: {
Expand Down Expand Up @@ -175,6 +202,13 @@ export class Job<T = any, R = any, N extends string = string> {
return jobInstances;
}

/**
* Instantiates a Job from a JobJsonRaw object (coming from a deserialized JSON object)
* @param queue the queue where the job belongs to.
* @param json the plain object containing the job.
* @param jobId an optional job id (overrides the id coming from the JSON object)
* @returns
*/
static fromJSON(queue: MinimalQueue, json: JobJsonRaw, jobId?: string) {
const data = JSON.parse(json.data || '{}');
const opts = JSON.parse(json.opts || '{}');
Expand Down Expand Up @@ -210,6 +244,13 @@ export class Job<T = any, R = any, N extends string = string> {
return job;
}

/**
* Fetches a Job from the queue given the passed job id.
*
* @param queue the queue where the job belongs to.
* @param jobId the job id.
* @returns
*/
static async fromId(
queue: MinimalQueue,
jobId: string,
Expand All @@ -229,6 +270,10 @@ export class Job<T = any, R = any, N extends string = string> {
return withoutQueue;
}

/**
* Prepares a job to be serialized for storage in Redis.
* @returns
*/
asJSON(): JobJson {
return {
id: this.id,
Expand All @@ -246,6 +291,11 @@ export class Job<T = any, R = any, N extends string = string> {
};
}

/**
* Updates a job's data
*
* @param data the data that will replace the current jobs data.
*/
async update(data: T) {
const client = await this.queue.client;

Expand All @@ -270,6 +320,11 @@ export class Job<T = any, R = any, N extends string = string> {
return client.rpush(logsKey, logRow);
}

/**
* Completely remove the job from the queue.
* Note, this call will throw an exception if the job
* is being processed when the call is performed.
*/
async remove() {
await this.queue.waitUntilReady();

Expand Down Expand Up @@ -402,26 +457,50 @@ export class Job<T = any, R = any, N extends string = string> {
}
}

/**
*
* @returns true if the job has completed.
*/
isCompleted() {
return this.isInZSet('completed');
}

/**
*
* @returns true if the job has failed.
*/
isFailed() {
return this.isInZSet('failed');
}

/**
*
* @returns true if the job is delayed.
*/
isDelayed() {
return this.isInZSet('delayed');
}

/**
*
* @returns true if the job is waiting for children.
*/
isWaitingChildren() {
return this.isInZSet('waiting-children');
}

/**
*
* @returns true of the job is active.
*/
isActive() {
return this.isInList('active');
}

/**
*
* @returns true if the job is waiting.
*/
async isWaiting() {
return (await this.isInList('wait')) || (await this.isInList('paused'));
}
Expand Down Expand Up @@ -539,10 +618,19 @@ export class Job<T = any, R = any, N extends string = string> {
});
}

/**
* Moves the job to the delay set.
*
* @param timestamp timestmap where the job should be moved back to "wait"
* @returns
*/
moveToDelayed(timestamp: number) {
return Scripts.moveToDelayed(this.queue, this.id, timestamp);
}

/**
* Promotes a delayed job so that it starts to be processed as soon as possible.
*/
async promote() {
const queue = this.queue;
const jobId = this.id;
Expand Down Expand Up @@ -584,6 +672,9 @@ export class Job<T = any, R = any, N extends string = string> {
}
}

/**
* Marks a job to not be retried if it fails (even if attempts has been configured)
*/
discard() {
this.discarded = true;
}
Expand All @@ -599,6 +690,13 @@ export class Job<T = any, R = any, N extends string = string> {
return Scripts.isJobInList(this.queue, this.queue.toKey(list), this.id);
}

/**
* Adds the job to Redis.
*
* @param client
* @param parentOpts
* @returns
*/
addJob(client: RedisClient, parentOpts?: ParentOpts): string {
const queue = this.queue;

Expand Down

0 comments on commit 601810e

Please sign in to comment.