spawn-workers
Version:
A high-performance worker pool library for Node.js that spawns worker processes to handle tasks in parallel
79 lines • 2.9 kB
TypeScript
import type { ErrorLike, WorkerStatus } from "./types";
export interface SpawnWorkersConfig<CustomStatus extends Record<string, number>> {
/** Path to the worker file to spawn */
workerFilePath: string;
/** Path to the data file containing entries to process */
dataFilePath: string;
/** Path to the output file where worker results will be written */
outputFilePath?: string;
/** Path to the failure output file where worker errors will be written */
failureOutputFilePath?: string;
/** Whether to overwrite the output file if it exists @default false */
overwriteOutputFile?: boolean;
/** Number of worker processes to spawn */
processCount: number;
/** Maximum number of concurrent entries per worker */
maxConcurrency: number;
/**
* The amount of entries sent to each worker in one batch.
* @default data.length / processCount
*/
batchSize?: number;
/**
* Maximum number of pending jobs per worker
* @default batchSize * 2
*/
maxPendingJobs?: number;
/**
* Each tick (duration in ms) will distribute work to workers
* @default 500
*/
tickDuration?: number;
/** Initial index to start processing from */
initialIndex?: number;
/** Total number of entries to process (from initialIndex) */
totalEntries?: number;
/** Path to log file */
logFilePath?: string;
/** Called when all workers complete */
onComplete?: (statuses: readonly WorkerStatus<CustomStatus>[]) => void;
/** Called on each status update with all worker statuses */
onStatusUpdate?: (statuses: readonly WorkerStatus<CustomStatus>[]) => void;
/** Called when an error occurs in a worker */
onError?: (error: ErrorLike, worker: {
index: number;
pid?: number;
}) => void;
/** Additional environment variables to pass to workers */
env?: Record<string, string>;
}
export declare class WorkerManager<CustomStatus extends Record<string, number>> {
private workers;
private config;
private optionalConfig;
private logFile?;
private outputFile?;
private failureOutputFile?;
private startedAt;
private currentIndex;
private dataEntries;
private workerIntervalId?;
private isShuttingDown;
constructor(config: SpawnWorkersConfig<CustomStatus>);
private validateConfig;
initialize(): Promise<void>;
private handleShutdown;
private cleanup;
private handleIpcMessage;
private logError;
private startChildProcesses;
start(): Promise<void>;
private distributeWork;
private checkForCompletion;
private sendToWorker;
}
/**
* Spawns worker processes to process data entries
*/
export declare function spawnWorkers<CustomStatus extends Record<string, number>>(config: SpawnWorkersConfig<CustomStatus>): Promise<void>;
//# sourceMappingURL=spawnWorkers.d.ts.map