queuex-sdk
Version:
A TypeScript-based queue management SDK with Redis support
82 lines (81 loc) • 2.7 kB
TypeScript
import { QueueManager } from './queue-manager';
import { Job } from '../models/job';
import { RedisStorage } from '../storage/redis';
import { QueueXEvent } from '../lib';
/**
* Processes jobs from a queue with concurrency control.
* Handles job execution, retries, job chaining, timeouts, and TTL.
*
* Job Chaining:
* When a job completes successfully and has a chain defined in its options,
* the worker will automatically create and enqueue the next job in the chain.
* The result of the completed job is passed as context to the next job.
*
* Example:
* ```typescript
* // Create a chain of jobs
* await queueX.enqueue('myQueue', initialData, {
* chain: [
* {
* data: { step: 1 },
* options: { priority: 'high' }
* },
* {
* data: { step: 2 },
* options: { retries: 5 }
* }
* ]
* });
*
* // In your job processor, you can access the previous job's result
* const processor = async (job: Job) => {
* if (job.context) {
* // This is a chained job, job.context contains the previous job's result
* console.log('Previous job result:', job.context);
* }
* // Process the job...
* };
* ```
*/
export declare class Worker {
private queueManager;
private storage;
private running;
private concurrency;
private emitEvent;
private activeTimeouts;
constructor(queueManager: QueueManager, storage: RedisStorage, concurrency: number | undefined, emitEvent: (event: QueueXEvent, job: Job) => void);
/**
* Starts processing jobs from the specified queue.
* Handles job execution, retries, job chaining, timeouts, and TTL.
*
* Job Chaining Process:
* 1. When a job completes successfully, check if it has a chain defined
* 2. If chain exists, create the next job with:
* - New unique ID
* - Data and options from the chain definition
* - Context set to the result of the completed job
* 3. Enqueue the new job and emit a 'jobReady' event
*
* @param queueName - Name of the queue to process jobs from
* @param processor - Function to process each job
*/
start(queueName: string, processor: (job: Job) => Promise<any>): Promise<void>;
/**
* Calculates the delay for the next retry attempt based on the backoff strategy.
*/
private calculateBackoffDelay;
/**
* Default exponential backoff calculation.
*/
private calculateDefaultBackoff;
/**
* Handles job timeout by failing the job and cleaning up.
*/
private handleJobTimeout;
/**
* Stops the worker from processing new jobs.
* Cleans up any active timeouts.
*/
stop(): void;
}