import { Log } from 'ts-tiny-log'; import { TaskPersistence, InMemoryTaskPersistence } from './task-persistence'; import { Task } from './task'; import { Worker, WorkerSpawnData } from './worker'; import { ParentThread } from './parent'; export declare type QueueCallback = (data: TIn) => Promise; export declare type ErrorHandler = (err: Error) => void | Promise; export interface QueueOptions { /** * Queue name. Must be unique */ name: string; /** * Worker entry file. Must be a relative/absolute path/file */ workerEntry?: string; /** * Number of workers. Default is 4 */ nWorkers?: number; /** * Polling rate in milliseconds. Default is 250 */ pollingRate?: number; /** * Maximum number of attempts for a task. Default is 1 */ maxAttempts?: number; /** * Delay in milliseconds before retrying a failed task. Default is 0 */ retryDelayMs?: number; /** * Function to run on worker startup */ startup?: (data: WorkerSpawnData) => Promise; /** * Function to run for the queue task */ callback: QueueCallback; /** * Function to run on error */ error?: ErrorHandler; /** * Function to run on fatal error */ fatal?: ErrorHandler; /** * Class for communicating Worker -> Parent */ parentType?: typeof ParentThread; /** * Class for communicating Parent -> Worker */ workerType?: typeof Worker; /** * Task persistence implementation. Default is InMemoryTaskPersistence */ persistenceType?: typeof InMemoryTaskPersistence; } /** * Queue class - main entry point for task queue * * @typeParam TIn Queue task input type * @typeParam TOut Queue task output type */ export declare class Queue { protected log: Log; defaultOptions: Partial>; protected tasks: TaskPersistence; protected options: QueueOptions; /** * Runs on: Main, Worker * * @param options Queue options */ constructor(options: QueueOptions); /** * Initialize main thread with dispatcher * * Runs on: Main */ protected initializeMainThread(): void; /** * Initialize worker thread * * Runs on: Worker */ protected initializeWorker(): void; /** * Check if this thread is the main thread * * @returns Returns true on the main thread, false on workers */ isMainThread(): boolean; /** * Push a new task to the queue to be run in parallel. This task will * not be joined back into the main thread on completion. * * Runs on: Main * * @param task Task object */ push(task: Task): void; /** * Push a new task to the queue. This method will return a promise that * will resolve when/if the task runs and completes * * Runs on: Main * * @param task Task object. The accept and reject callbacks will * be attached automatically. * @return Returns a promise resolving to the task result (type TOut) */ await(task: Omit, 'accept' | 'reject'>): Promise; }