import child_process from "node:child_process"; import { InternalBatchProcessOptions } from "./InternalBatchProcessOptions"; import { ProcessHealthMonitor } from "./ProcessHealthMonitor"; import { Task } from "./Task"; import { WhyNotHealthy, WhyNotReady } from "./WhyNotHealthy"; /** How long end() waits for the child to exit after termination. */ export declare const ExitConfirmationMillis = 5000; /** * BatchProcess manages the care and feeding of a single child process. */ export declare class BatchProcess { #private; readonly proc: child_process.ChildProcess; readonly opts: InternalBatchProcessOptions; private readonly onIdle; readonly name: string; readonly pid: number; readonly start: number; /** * Exit code from the child process, or null if not yet exited. * Populated for both expected terminations (idle, old, worn) and * unexpected exits (crashes, errors). * Use {@link unexpectedExit} to determine if the exit was unexpected. */ get exitCode(): number | null; /** * Exit signal from the child process, or null if not terminated by signal. * Populated for both expected terminations and unexpected exits. * Use {@link unexpectedExit} to determine if the exit was unexpected. */ get exitSignal(): NodeJS.Signals | null; /** * Returns true if the process exited unexpectedly (crash, error, timeout, etc.) * rather than through proactive cluster management (worn, old, idle, etc.). * Useful for determining if a non-zero exit code indicates a problem. * @see {@link ExpectedTerminationReasons} for the list of expected termination reasons. */ get unexpectedExit(): boolean; readonly startupTaskId: number; failedTaskCount: number; /** * Getter for current task (required by StreamContext interface) */ get currentTask(): Task | undefined; /** * @param onIdle to be called when internal state changes (like the current * task is resolved, or the process exits) */ constructor(proc: child_process.ChildProcess, opts: InternalBatchProcessOptions, onIdle: () => void, healthMonitor?: ProcessHealthMonitor); get taskCount(): number; get starting(): boolean; /** * @return true if `this.end()` has been requested (which may be due to the * child process exiting) */ get ending(): boolean; /** * @return true if `this.end()` has completed running, which includes child * process cleanup. A failed termination also settles end(); use `exited` * to distinguish confirmed exit, and await end() to receive cleanup errors. */ get ended(): boolean; /** * @return true if the child process has exited (based on OS events). * This is now authoritative and inexpensive since it's driven by OS events * rather than polling. */ get exited(): boolean; /** * @return a string describing why this process should be recycled, or null if * the process passes all health checks. Note that this doesn't include if * we're already busy: see {@link BatchProcess.whyNotReady} if you need to * know if a process can handle a new task. */ get whyNotHealthy(): WhyNotHealthy | null; /** Whether this worker has been asked to retire after its current task. */ get retirementRequested(): boolean; /** * Permanently prevent further assignments while letting the active task * settle under its normal timeout. Idle workers are recycled immediately. * Repeated calls have no additional effect. Unlike `end()`, this preserves * the current task and its output streams until the task settles. */ requestRetirement(): void; /** * @return true if the process doesn't need to be recycled. */ get healthy(): boolean; /** * @return true iff no current task. Does not take into consideration if the * process has ended or should be recycled: see {@link BatchProcess.ready}. */ get idle(): boolean; /** * @return a string describing why this process cannot currently handle a new * task, or `undefined` if this process is idle and healthy. */ get whyNotReady(): WhyNotReady | null; /** * @return true iff this process is both healthy and idle, and ready for a * new task. */ get ready(): boolean; get idleMs(): number; /** * @return true if the child process is running. * Now event-driven first with polling fallback. */ running(): boolean; notRunning(): boolean; /** * Reject the task this process was running when it was told to end, if * termination hasn't settled it yet. * * `ProcessTerminator` gives that task up to 2 seconds to finish on its own, * which is longer than a bounded shutdown may be willing to wait. Without * this, `end()` can resolve -- and the caller `process.exit()` -- while that * promise is still pending, which abandons it entirely. * * @return true if a pending task was rejected. */ abandonTerminatingTask(reason: string): boolean; maybeRunHealthCheck(): Task | undefined; execTask(task: Task): boolean; /** * End this child process. * * @param gracefully Wait for any current task to be resolved or rejected * before shutting down the child process. * @param reason who called end() (used for logging) * @return Promise that resolves after the child exits, or rejects if it is * still running 5 seconds after termination. * Subsequent calls to end() will ignore the parameters and return the first * endPromise. */ end(gracefully: boolean | undefined, reason: WhyNotHealthy): Promise; }