/** * Fixed size worker pool with per task timeouts. * * Adapted from the pool in @postalsys/email-text-tools (lib/worker-pool.js), simplified for batch * work: the corpus scanner knows how many workers it wants and drives its own backpressure, so the * elastic sizing and idle reaping of the original are not needed. What is kept is the important * part, a task timeout that terminates and replaces the worker, so that one pathological message * cannot wedge a scan of 320k files. */ import { EventEmitter } from 'node:events'; export interface WorkerPoolOptions { workerPath: string; size: number; /** Per task timeout in milliseconds. */ taskTimeoutMs: number; /** Optional data passed to every worker at startup. */ workerData?: unknown; /** * Old generation heap cap per worker, in MiB. * * Worker threads share the parent process, so their heaps all count towards its RSS. Without a * cap, sixteen workers each growing to a comfortable default is more than a gigabyte of * resident memory for a job whose working set is one message at a time. Exceeding the cap * terminates that worker, which the pool treats like any other worker failure. */ maxOldGenerationSizeMb?: number; /** * Worker respawns allowed before the pool gives up. Default 50 plus ten per worker. * * Respawning without a budget turns a worker that dies during startup into an infinite loop: * spawn, crash, spawn, forever, producing no results and no error. The budget converts that * into a bounded failure that names the tasks in flight when it tripped. */ maxRestarts?: number; } /** The pool gave up replacing workers. Carries the ids of the tasks that were in flight. */ export declare class WorkerRestartBudgetError extends Error { readonly restarts: number; constructor(restarts: number); } export declare class WorkerTimeoutError extends Error { constructor(ms: number); } /** * Emits 'worker-error' with the underlying error whenever a worker dies unexpectedly. Callers may * log it; the pool itself recovers by spawning a replacement. */ export declare class WorkerPool extends EventEmitter { private readonly options; private readonly slots; private readonly queue; private nextId; private closing; private restarts; private readonly maxRestarts; /** Set once the restart budget is spent. Every queued and future task fails with it. */ private brokenBy; constructor(options: WorkerPoolOptions); private spawn; private replace; /** Reject whatever a slot was running, if anything. */ private fail; /** Reject everything in flight and waiting, so a broken pool stops a run instead of stalling it. */ private failQueue; private dispatch; private drainQueue; /** Number of tasks accepted but not yet completed. */ get inFlight(): number; submit(task: TTask): Promise; close(): Promise; } //# sourceMappingURL=worker-pool.d.ts.map