import { JobJson, JobJsonRaw, JobsOptions, ParentKeys, RedisClient } from '../interfaces'; import { FinishedStatus, JobState, JobJsonSandbox } from '../types'; import { QueueEvents } from './queue-events'; import { MinimalQueue, ParentOpts, JobData } from './scripts'; export declare type BulkJobOptions = Omit; export interface MoveToChildrenOpts { timestamp?: number; child?: { id: string; queue: string; }; } export interface DependenciesOpts { processed?: { cursor?: number; count?: number; }; unprocessed?: { cursor?: number; count?: number; }; } export declare class Job { protected queue: MinimalQueue; /** * The name of the Job */ name: NameType; /** * The payload for this job. */ data: DataType; /** * The options object for this job. */ opts: JobsOptions; id?: string; /** * The progress a job has performed so far. * @defaultValue 0 */ progress: number | object; /** * The value returned by the processor when processing this job. * @defaultValue null */ returnvalue: ReturnType; /** * Stacktrace for the error (for failed jobs). * @defaultValue null */ stacktrace: string[]; /** * Timestamp when the job was created (unless overridden with job options). */ timestamp: number; /** * Number of attempts after the job has failed. * @defaultValue 0 */ attemptsMade: number; /** * Reason for failing. */ failedReason: string; /** * Timestamp for when the job finished (completed or failed). */ finishedOn?: number; /** * Timestamp for when the job was processed. */ processedOn?: number; /** * Fully qualified key (including the queue prefix) pointing to the parent of this job. */ parentKey?: string; /** * Object that contains parentId (id) and parent queueKey. */ parent?: ParentKeys; protected toKey: (type: string) => string; private discarded; constructor(queue: MinimalQueue, /** * The name of the Job */ name: NameType, /** * The payload for this job. */ data: DataType, /** * The options object for this job. */ opts?: JobsOptions, id?: string); /** * 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 create(queue: MinimalQueue, name: N, data: T, opts?: JobsOptions): Promise>; /** * 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 createBulk(queue: MinimalQueue, jobs: { name: N; data: T; opts?: BulkJobOptions; }[]): Promise[]>; /** * 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): 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 fromId(queue: MinimalQueue, jobId: string): Promise | undefined>; toJSON(): Omit; /** * Prepares a job to be serialized for storage in Redis. * @returns */ asJSON(): JobJson; /** * Prepares a job to be passed to Sandbox. * @returns */ asJSONSandbox(): JobJsonSandbox; /** * Updates a job's data * * @param data - the data that will replace the current jobs data. */ update(data: DataType): Promise; /** * Updates a job's progress * * @param progress - number or object to be saved as progress. */ updateProgress(progress: number | object): Promise; /** * Logs one row of log data. * * @param logRow - string with log data to be logged. */ log(logRow: string): Promise; /** * 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. */ remove(): Promise; /** * Extend the lock for this job. * * @param token - unique token for the lock * @param duration - lock duration in milliseconds */ extendLock(token: string, duration: number): Promise; /** * Moves a job to the completed queue. * Returned job to be used with Queue.prototype.nextJobFromJobData. * * @param returnValue - The jobs success message. * @param token - Worker token used to acquire completed job. * @param fetchNext - True when wanting to fetch the next job. * @returns Returns the jobData of the next job in the waiting queue. */ moveToCompleted(returnValue: ReturnType, token: string, fetchNext?: boolean): Promise; /** * Moves a job to the failed queue. * * @param err - the jobs error message. * @param token - token to check job is locked by current worker * @param fetchNext - true when wanting to fetch the next job * @returns void */ moveToFailed(err: E, token: string, fetchNext?: boolean): Promise; /** * @returns true if the job has completed. */ isCompleted(): Promise; /** * @returns true if the job has failed. */ isFailed(): Promise; /** * @returns true if the job is delayed. */ isDelayed(): Promise; /** * @returns true if the job is waiting for children. */ isWaitingChildren(): Promise; /** * @returns true of the job is active. */ isActive(): Promise; /** * @returns true if the job is waiting. */ isWaiting(): Promise; /** * @returns the queue name this job belongs to. */ get queueName(): string; get prefix(): string; /** * Get current state. * * @returns Returns one of these values: * 'completed', 'failed', 'delayed', 'active', 'waiting', 'waiting-children', 'unknown'. */ getState(): Promise; /** * Change delay of a delayed job. * * @param delay - milliseconds to be added to current time. * @returns void */ changeDelay(delay: number): Promise; /** * Get this jobs children result values if any. * * @returns Object mapping children job keys with their values. */ getChildrenValues(): Promise<{ [jobKey: string]: CT; }>; /** * Get children job keys if this job is a parent and has children. * * @returns dependencies separated by processed and unprocessed. */ getDependencies(opts?: DependenciesOpts): Promise<{ nextProcessedCursor?: number; processed?: Record; nextUnprocessedCursor?: number; unprocessed?: string[]; }>; /** * Get children job counts if this job is a parent and has children. * * @returns dependencies count separated by processed and unprocessed. */ getDependenciesCount(opts?: { processed?: boolean; unprocessed?: boolean; }): Promise<{ processed?: number; unprocessed?: number; }>; /** * Returns a promise the resolves when the job has completed (containing the return value of the job), * or rejects when the job has failed (containing the failedReason). * * @param queueEvents - Instance of QueueEvents. * @param ttl - Time in milliseconds to wait for job to finish before timing out. */ waitUntilFinished(queueEvents: QueueEvents, ttl?: number): Promise; /** * Moves the job to the delay set. * * @param timestamp - timestamp where the job should be moved back to "wait" * @param token - token to check job is locked by current worker * @returns */ moveToDelayed(timestamp: number, token?: string): Promise; /** * Moves the job to the waiting-children set. * * @param token - Token to check job is locked by current worker * @param opts - The options bag for moving a job to waiting-children. * @returns true if the job was moved */ moveToWaitingChildren(token: string, opts?: MoveToChildrenOpts): Promise; /** * Promotes a delayed job so that it starts to be processed as soon as possible. */ promote(): Promise; /** * Attempts to retry the job. Only a job that has failed or completed can be retried. * * @param state - completed / failed * @returns If resolved and return code is 1, then the queue emits a waiting event * otherwise the operation was not a success and throw the corresponding error. If the promise * rejects, it indicates that the script failed to execute */ retry(state?: FinishedStatus): Promise; /** * Marks a job to not be retried if it fails (even if attempts has been configured) */ discard(): void; private isInZSet; private isInList; /** * Adds the job to Redis. * * @param client - * @param parentOpts - * @returns */ addJob(client: RedisClient, parentOpts?: ParentOpts): Promise; private saveStacktrace; }