UNPKG

@tomsons/queue-manager

Version:

A powerful and flexible queue manager for handling asynchronous tasks in TypeScript applications. It provides features like concurrency control, task prioritization, automatic retries, cancellation, and progress tracking.

145 lines 4.38 kB
/** * Interface representing a task that can be executed by the queue manager * @template T The return type of the task execution */ export interface Task<T = unknown> { /** Unique identifier for the task */ id: string; /** Function that executes the task and returns a promise */ execute: (progress: (progress: TaskProgress) => void) => Promise<T>; /** Optional priority level for the task */ priority?: number; /** Maximum number of retry attempts */ maxRetries?: number; /** Policy for calculating retry delays */ retryPolicy?: RetryPolicy; /** Function to cancel the task execution */ cancel?: () => void; /** Optional timeout for the task in milliseconds */ timeout?: number; } /** * Enum representing the different states a task can be in */ export declare enum TaskStatus { /** Task is waiting to be processed */ PENDING = "PENDING", /** Task is currently being executed */ RUNNING = "RUNNING", /** Task has finished successfully */ COMPLETED = "COMPLETED", /** Task execution failed */ FAILED = "FAILED", /** Task was cancelled before completion */ CANCELLED = "CANCELLED" } /** * Interface for tracking task progress and status */ export interface TaskProgress { /** ID of the task being tracked */ taskId: string; /** Progress value between 0-100 */ progress: number; /** Current status of the task */ status: TaskStatus; /** Timestamp of the progress update */ date: Date; /** Error details if task failed */ error?: Error; } /** * Interface defining how retry delays should be calculated */ export interface RetryPolicy { /** Function to calculate delay in ms before next retry attempt */ calculateDelay: (attempt: number) => number; } /** * Default retry policy using exponential backoff strategy */ export declare const ExponentialBackoff: RetryPolicy; /** * Configuration options for the queue manager */ export interface QueueOptions { /** Maximum number of concurrent tasks */ concurrency: number; /** Default retry policy for tasks */ defaultRetryPolicy?: RetryPolicy; /** Default maximum retry attempts */ maxRetries?: number; } /** * Manages execution of tasks in a FIFO queue with retry capabilities * @template T The return type of the tasks */ export declare class QueueManager<T = unknown> { private queue; private failedQueue; private running; private options; private listeners; /** * Creates a new queue manager instance * @param options Configuration options for the queue */ constructor(options: QueueOptions); /** * Returns a copy of the tasks in the failed queue. */ getFailedTasks(): Task<T>[]; /** * Adds a new task to the queue * @param task Task to be queued */ enqueue(task: Task<T>): void; /** * Re-queues all tasks from the failed queue for processing. */ reprocessFailedTasks(): void; /** * Registers a callback for task progress updates * @param callback Function to be called with progress updates * @returns Function to unregister the callback */ onProgress(callback: (progress: TaskProgress) => void): () => void; /** * Clears all pending tasks from the queue * @param cancelRunning If true, also cancels currently running tasks */ clearQueue(cancelRunning?: boolean): void; /** * Waits for all tasks in the queue to complete * @returns Promise that resolves when all tasks are finished */ waitForCompletion(): Promise<void>; /** * Cancels all queued and running tasks */ cancelAll(): Promise<void>; /** * Processes the next task in the queue if possible */ private processQueue; /** * Executes a single task with retry logic * @param task Task to execute */ private executeTask; /** * Executes a task with a timeout * @param task * @private */ private executeTaskWithTimeout; /** * Notifies all registered listeners of task progress * @param taskId ID of the task * @param progress Current progress value * @param status Current task status * @param error Error if task failed */ private notifyProgress; } //# sourceMappingURL=queue-manager.d.ts.map