UNPKG

torotask

Version:

Task queue processing in NodeJS based on BullMQ and Redis

72 lines 3.16 kB
import { Queue, QueueEvents, Worker, WorkerOptions, Job } from 'bullmq'; import { EventEmitter } from 'events'; import { Logger } from 'pino'; import type { ToroTask } from './client.js'; import { BulkJob, TaskJobOptions } from './types/index.js'; /** * Base class for managing a dedicated BullMQ Queue, its events, and an optional Worker. * Handles resource creation, lifecycle (startWorker, close), and provides an abstract * method for job processing logic. * Also provides a static registry (`instances`) of all created queues. * Extends EventEmitter to forward worker events. */ export declare abstract class BaseQueue extends EventEmitter { static readonly instances: Map<string, BaseQueue>; readonly client: ToroTask; readonly queueName: string; readonly prefix: string; readonly queue: Queue; readonly queueEvents: QueueEvents; readonly logger: Logger; protected worker?: Worker; private workerEventHandlers; constructor(client: ToroTask, queueName: string, parentLogger: Logger, prefix?: string); /** * Get the Redis client instance used by the queue. * @returns The Redis client instance. */ getRedisClient(): Promise<import("bullmq").RedisClient>; /** * Abstract method to process a job. * Subclasses must implement this method to define the job handling logic. * * @param job The BullMQ job object. * @returns A promise that resolves with the result of the job. */ abstract process(job: Job, token?: string): Promise<any>; getWorkerOptions(): Partial<WorkerOptions>; /** * Starts a dedicated BullMQ Worker for this queue, if one is not already running. * Forwards worker events to this BaseQueue instance. */ startWorker(options?: WorkerOptions): Promise<Worker>; /** Helper to remove all attached worker listeners */ private removeAllWorkerListeners; /** * Stops the worker instance associated with this queue, if it is running. * Removes event listeners before closing. */ stopWorker(): Promise<void>; /** * Closes the underlying BullMQ Worker (if started), Queue, and QueueEvents instances, * and removes the instance from the static registry. * Should be called during application shutdown. */ close(): Promise<void>; /** * Core logic to add a job to the queue. * Public method intended for use by subclasses or related classes (e.g., SubTask). */ _runJob<JobData, JobReturn>(jobName: string, data: JobData, options: TaskJobOptions): Promise<Job<JobData, JobReturn>>; /** * Core logic to add multiple jobs to the queue. * Public method intended for use by subclasses or related classes (e.g., SubTask). */ _runBulk<JobData, JobReturn>(jobs: BulkJob<JobData>[]): Promise<Job<JobData, JobReturn>[]>; /** * Core logic to add a job and wait for its completion. * Public method intended for use by subclasses or related classes. */ _runJobAndWait<JobData, JobReturn>(jobName: string, data: JobData, options: TaskJobOptions): Promise<JobReturn>; } //# sourceMappingURL=base-queue.d.ts.map