UNPKG

torotask

Version:

Task queue processing in NodeJS based on BullMQ and Redis

82 lines 3.77 kB
import type { JobsOptions, WorkerOptions } from 'bullmq'; import type { Logger } from 'pino'; import type { ToroTask } from './client.js'; import type { TaskJob } from './job.js'; import type { Task } from './task.js'; import type { EventQueueOptions, TaskJobOptions } from './types/index.js'; import { EventManager } from './event-manager.js'; import { TaskWorkerQueue } from './worker-queue.js'; type PayloadType = any; type ReturnType = any; /** * Options for configuring the EventDispatcher */ export interface EventDispatcherOptions { /** Options for the dispatcher queue itself */ dispatcherOptions?: EventQueueOptions; /** Options passed to the underlying EventManager */ managerOptions?: EventQueueOptions; } /** * Manages the registration and dispatching of events to subscribed Tasks. * It uses its own BullMQ queue to process published events. * Maintains a local cache (`activeEvents`) of events presumed to have subscribers. */ export declare class EventDispatcher extends TaskWorkerQueue<PayloadType, ReturnType> { manager: EventManager; activeEvents: Set<string>; subscribedToActiveEvents: boolean; disableSubscription: boolean; /** Default job options for dispatch jobs */ readonly eventDefaultJobOptions: Partial<JobsOptions>; /** Worker options for this queue */ readonly eventWorkerOptions: Partial<WorkerOptions>; private setActiveEventsDebounced; private setActiveEventsTimeout; constructor(taskClient: ToroTask, parentLogger: Logger, name?: string, eventOptions?: EventDispatcherOptions); subscribeToActiveEvents(): Promise<void>; /** * Returns worker options, merging any provided eventWorkerOptions */ getWorkerOptions(): Partial<WorkerOptions>; /** * Starts the event manager before the event dispatcher */ startWorker(options?: WorkerOptions): Promise<import("./worker.js").TaskWorker<any, any, string, TaskJob<any, any, string, import("./types/job.js").TaskJobData<any, import("./types/job.js").TaskJobState>, import("./types/job.js").TaskJobState>>>; /** * Fetches the list of event keys from Redis and updates the local activeEvents cache. * Note: This scans keys, but doesn't verify HLEN > 0 for each. It assumes * the existence of the key implies active subscribers for performance. */ setActiveEvents(): Promise<void>; /** * Removes all event registrations associated with a specific task. * Useful when a task is updated or removed. * @param task The Task instance whose registrations should be cleared. */ clearTaskEvents(task: Task<any, any>): Promise<void>; /** * Removes all task registrations for a specific event name. * Also removes the event name from the sets of associated tasks. * @param eventName The name of the event to clear. */ clearEvent(eventName: string): Promise<void>; /** * Sends an event by adding a job to the event queue. * The job name will be the event name. * @param eventName The name of the event to send. * @param data The data payload for the event. * @param options Optional BullMQ job options. */ send<E = unknown>(eventName: string, data: E, options?: TaskJobOptions): Promise<TaskJob<PayloadType, any> | undefined>; /** * Processes jobs from the event queue. * Looks up event subscription JSON in Redis Hash using the EventManager * and triggers associated tasks by adding jobs to their respective queues. * This is intended to be used as the processor function for the BullMQ Worker. * @param job The job received from the event queue. */ process(job: TaskJob): Promise<void>; } export {}; //# sourceMappingURL=event-dispatcher.d.ts.map