queuex-sdk
Version:
A TypeScript-based queue management SDK with Redis support
119 lines (118 loc) • 4.15 kB
TypeScript
import { Job, JobOptions } from '../models/job';
import { QueueOptions } from '../models/queue';
import { QueueXEvent } from '../lib';
/**
* Manages queues and job scheduling with dependency resolution, cron support, and job chaining.
*
* Features:
* - Queue management and job scheduling
* - Dependency resolution between jobs
* - Cron-based job scheduling
* - Job chaining with context passing
* - Concurrent job processing
*
* Example:
* ```typescript
* // Create a queue manager
* const queueManager = new QueueManager(redisUrl, emitEvent);
*
* // Create a queue
* await queueManager.createQueue('myQueue', { maxConcurrency: 5 });
*
* // Enqueue a job with chaining
* await queueManager.enqueue('myQueue', initialData, {
* chain: [
* {
* data: { step: 1 },
* options: { priority: 'high' }
* },
* {
* data: { step: 2 },
* options: { retries: 5 }
* }
* ]
* });
* ```
*/
export declare class QueueManager {
private storage;
private queues;
private emitEvent;
private dependencyGraph;
private schedulerInterval;
constructor(redisConnectionString: string, emitEvent: (event: QueueXEvent, job: Job) => void);
/**
* Creates a new queue with the specified options.
* @param name - Name of the queue
* @param options - Queue configuration options
* @throws Error if queue already exists
*/
createQueue(name: string, options?: QueueOptions): Promise<void>;
/**
* Enqueues a job with optional chaining and dependencies.
*
* Job Chaining:
* - The chain option allows defining subsequent jobs that will execute after the current job
* - Each job in the chain receives the result of the previous job as context
* - Chain jobs inherit the queue of the parent job
*
* Dependencies:
* - Jobs can depend on other jobs using dependsOn
* - A job will only start when all its dependencies are completed
*
* @param queueName - Name of the queue to enqueue the job in
* @param data - Job data payload
* @param options - Job configuration including chain and dependencies
* @returns The created job
* @throws Error if queue doesn't exist or if dependency jobs don't exist
*/
enqueue(queueName: string, data: any, options?: JobOptions): Promise<Job>;
/**
* Gets the next available job from the queue.
* Handles dependency resolution and job state transitions.
*
* @param queueName - Name of the queue to get job from
* @returns The next available job or null if no jobs are available
*/
getJob(queueName: string): Promise<Job | null>;
/**
* Retrieves a job by its ID from the dependency graph.
* @param jobId - ID of the job to retrieve
* @returns The job if found, undefined otherwise
*/
getJobById(jobId: string): Job | undefined;
/**
* Unschedules a delayed job and marks it as failed.
* @param jobId - ID of the job to unschedule
* @throws Error if job doesn't exist or is not in DELAYED state
*/
unscheduleJob(jobId: string): Promise<void>;
/**
* Updates the delay of a delayed or pending job.
* @param jobId - ID of the job to update
* @param newDelay - New delay in milliseconds
* @returns The updated job
* @throws Error if job doesn't exist or is not in DELAYED/PENDING state
*/
updateJobDelay(jobId: string, newDelay: number): Promise<Job>;
/**
* Determines the initial state of a job based on its options.
* @param options - Job options
* @returns The initial job state
*/
private getInitialState;
/**
* Calculates the scheduled execution time for a job based on its options.
* @param options - Job options
* @returns The scheduled timestamp or undefined if not scheduled
* @throws Error if cron expression is invalid
*/
private getScheduledAt;
/**
* Processes overdue jobs from all queues.
* Handles both delayed and cron jobs.
*/
private processOverdueJobs;
private startScheduler;
stopScheduler(): void;
}