import { Args } from "./Args"; import { BatchProcess } from "./BatchProcess"; import { Task } from "./Task"; import { WhyNotHealthy } from "./WhyNotHealthy"; export type ChildEndReason = WhyNotHealthy | "tooMany"; export interface TypedEventEmitter { once(eventName: E, listener: (...args: Args) => void): this; on(eventName: E, listener: (...args: Args) => void): this; off(eventName: E, listener: (...args: Args) => void): this; emit(eventName: E, ...args: Args): boolean; listeners(event: E): Function[]; removeAllListeners(eventName?: keyof T): this; } /** * This interface describes the BatchCluster's event names as fields. The type * of the field describes the event data payload. * * See {@link BatchClusterEmitter} for more details. */ export interface BatchClusterEvents { /** * Emitted when a child process has started */ childStart: (childProcess: BatchProcess) => void; /** * Emitted when a child process has ended, or when termination failed to * make it exit (see `endError`). Check `proc.exited` to tell them apart. * * @example * ```typescript * bc.on("childEnd", (proc, reason) => { * // Check if this was an unexpected exit (crash, error, timeout) * if (proc.unexpectedExit && proc.exitCode !== 0) { * logger.error(`Process crashed: ${reason}`, { * exitCode: proc.exitCode, * exitSignal: proc.exitSignal * }); * } * }); * ``` */ childEnd: (childProcess: BatchProcess, reason: ChildEndReason) => void; /** * Emitted when a child process fails to spin up and run the {@link BatchProcessOptions.versionCommand} successfully within {@link BatchClusterOptions.spawnTimeoutMillis}. * * @param childProcess will be undefined if the error is from {@link ChildProcessFactory.processFactory} */ startError: (error: Error, childProcess?: BatchProcess) => void; /** * Emitted when an internal consistency check fails */ internalError: (error: Error) => void; /** * Emitted when tasks receive data, which may be partial chunks from the task * stream. */ taskData: (data: Buffer | string, task: Task | undefined, proc: BatchProcess) => void; /** * Emitted when a task has been resolved */ taskResolved: (task: Task, proc: BatchProcess) => void; /** * Emitted when a task times out. Note that a `taskError` event always succeeds these events. * * By then the task has been rejected with a `TaskTimeoutError` and `proc` is * ending, so `proc.end()` returns that same termination. */ taskTimeout: (timeoutMs: number, task: Task, proc: BatchProcess) => void; /** * Emitted when a task has an error */ taskError: (error: Error, task: Task, proc: BatchProcess) => void; /** * Emitted when child processes write to stdout or stderr without a current * task */ noTaskData: (stdoutData: string | Buffer | null, stderrData: string | Buffer | null, proc: BatchProcess) => void; /** * Emitted when a process fails health checks */ healthCheckError: (error: Error, proc: BatchProcess) => void; /** * Emitted when a child process has an error during shutdown, including * when it is still running 5 seconds after termination. */ endError: (error: Error, proc?: BatchProcess) => void; /** * Emitted when this instance is in the process of ending. */ beforeEnd: () => void; /** * Emitted when this instance has ended. No child processes should remain at * this point. */ end: () => void; } /** * The BatchClusterEmitter signature is built up automatically by the * {@link BatchClusterEvents} interface, which ensures `.on`, `.off`, and * `.emit` signatures are all consistent, and include the correct data payloads * for all of BatchCluster's events. * * This approach has some benefits: * * - it ensures that on(), off(), and emit() signatures are all consistent, * - supports editor autocomplete, and * - offers strong typing, * * but has one drawback: * * - jsdocs don't list all signatures directly: you have to visit the event * source interface. * * See {@link BatchClusterEvents} for a the list of events and their payload * signatures */ export type BatchClusterEmitter = TypedEventEmitter;