import { GetNextJobOptions, JobJsonRaw, Processor, RedisClient, WorkerOptions } from '../interfaces'; import { QueueBase } from './queue-base'; import { Repeat } from './repeat'; import { Job } from './job'; import { RedisConnection } from './redis-connection'; import { TimerManager } from './timer-manager'; export interface WorkerListener { /** * Listen to 'active' event. * * This event is triggered when a job enters the 'active' state. */ active: (job: Job, prev: string) => void; /** * Listen to 'closing' event. * * This event is triggered when the worker is closed. */ closed: () => void; /** * Listen to 'closing' event. * * This event is triggered when the worker is closing. */ closing: (msg: string) => void; /** * Listen to 'completed' event. * * This event is triggered when a job has successfully completed. */ completed: (job: Job, result: ResultType, prev: string) => void; /** * Listen to 'drained' event. * * This event is triggered when the queue has drained the waiting list. * Note that there could still be delayed jobs waiting their timers to expire * and this event will still be triggered as long as the waiting list has emptied. */ drained: () => void; /** * Listen to 'error' event. * * This event is triggered when an error is throw. */ error: (failedReason: Error) => void; /** * Listen to 'failed' event. * * This event is triggered when a job has thrown an exception. */ failed: (job: Job, error: Error, prev: string) => void; /** * Listen to 'paused' event. * * This event is triggered when the queue is paused. */ paused: () => void; /** * Listen to 'progress' event. * * This event is triggered when a job updates it progress, i.e. the * Job##updateProgress() method is called. This is useful to notify * progress or any other data from within a processor to the rest of the * world. */ progress: (job: Job, progress: number | object) => void; /** * Listen to 'resumed' event. * * This event is triggered when the queue is resumed. */ resumed: () => void; } /** * * This class represents a worker that is able to process jobs from the queue. * As soon as the class is instantiated it will start processing jobs. * */ export declare class Worker extends QueueBase { opts: WorkerOptions; private drained; private waiting; private running; private blockTimeout; protected processFn: Processor; private resumeWorker; protected paused: Promise; private _repeat; private childPool; protected timerManager: TimerManager; private blockingConnection; private processing; constructor(name: string, processor?: string | Processor, opts?: WorkerOptions, Connection?: typeof RedisConnection); emit>(event: U, ...args: Parameters[U]>): boolean; off>(eventName: U, listener: WorkerListener[U]): this; on>(event: U, listener: WorkerListener[U]): this; once>(event: U, listener: WorkerListener[U]): this; protected callProcessJob(job: Job, token: string): Promise; protected createJob(data: JobJsonRaw, jobId: string): Job; /** * * Waits until the worker is ready to start processing jobs. * In general only useful when writing tests. * */ waitUntilReady(): Promise; get repeat(): Promise; run(): Promise; /** * Returns a promise that resolves to the next job in queue. * @param token - worker token to be assigned to retrieved job * @returns a Job or undefined if no job was available in the queue. */ getNextJob(token: string, { block }?: GetNextJobOptions): Promise>; protected moveToActive(token: string, jobId?: string): Promise>; private waitForJob; /** * * This function is exposed only for testing purposes. */ delay(): Promise; protected nextJobFromJobData(jobData?: JobJsonRaw | number, jobId?: string): Promise>; processJob(job: Job, token: string): Promise>; /** * * Pauses the processing of this queue only for this worker. */ pause(doNotWaitActive?: boolean): Promise; /** * * Resumes processing of this worker (if paused). */ resume(): void; /** * * Checks if worker is paused. * * @returns true if worker is paused, false otherwise. */ isPaused(): boolean; /** * * Checks if worker is currently running. * * @returns true if worker is running, false otherwise. */ isRunning(): boolean; /** * * Closes the worker and related redis connections. * * This method waits for current jobs to finalize before returning. * * @param force - Use force boolean parameter if you do not want to wait for * current jobs to be processed. * * @returns Promise that resolves when the worker has been closed. */ close(force?: boolean): Promise; /** * Returns a promise that resolves when active jobs are cleared * * @returns */ private whenCurrentJobsFinished; private retryIfFailed; }