@jjavery/worker-pool
Version:
A worker pool for Node.js applications
93 lines (92 loc) • 3.11 kB
TypeScript
interface NotReadyErrorOptions {
pid?: number;
}
export declare class NotReadyError extends Error {
constructor(options: NotReadyErrorOptions);
}
export declare class WorkerError extends Error {
constructor(err: Error);
}
export declare class UnexpectedExitError extends Error {
constructor();
}
interface WorkerOptions {
args?: string[];
cwd?: string;
env?: any;
idleTimeout?: number;
stopTimeout?: number;
stopSignal?: 'SIGTERM' | 'SIGINT' | 'SIGHUP' | 'SIGKILL';
stopWhenIdle?: () => boolean;
}
/**
* Responsible for starting and stopping worker processes and handling requests
* and replies
* @private
*/
export default class Worker {
private _id;
private _args?;
private _cwd?;
private _env?;
private _idleTimeout;
private _stopTimeout;
private _stopSignal;
private _stopWhenIdle?;
private _waiting;
private _childProcess?;
private _creatingChildProcess?;
private _destroyingChildProcess?;
private _messageListener?;
private _exitListener?;
private _requests;
private _createTimestamp?;
private _idleTimestamp?;
private _serialization;
get id(): number;
get waiting(): number;
get pid(): number | null;
get isStarted(): boolean;
/**
* @param {object} options={} - Optional parameters
* @param {string} options.cwd - The current working directory for worker processes
* @param {string[]} options.args - Arguments to pass to worker processes
* @param {Object} options.env - Environmental variables to set for worker processes
* @param {number} options.idleTimeout=10000 - Milliseconds before an idle worker process will be asked to stop via options.stopSignal
* @param {number} options.stopTimeout=10000 - Milliseconds before an idle worker process will receive SIGKILL after it has been asked to stop
* @param {'SIGTERM'|'SIGINT'|'SIGHUP'|'SIGKILL'} options.stopSignal='SIGTERM' - Initial signal to send when stopping worker processes
* @param {Function} options.stopWhenIdle - The worker will call this function to determine whether to stop an idle worker process
* @private
*/
constructor({ args, cwd, env, idleTimeout, stopTimeout, stopSignal, stopWhenIdle }?: WorkerOptions);
/**
* Start the worker process
* @returns {Promise}
* @throws {Worker.NotReadyError}
* @protected
*/
start(): Promise<void>;
/**
* Stop the worker process
* @returns {Promise}
* @protected
*/
stop(): Promise<void>;
/**
* Send a request to the worker process and wait for and return the result
* @param {any} message
* @returns {Promise}
* @resolves {any} - The result of the function invocation
* @rejects {Worker.UnexpectedExitError|Error}
* @protected
*/
request(message?: {}): Promise<unknown>;
private _handleResponse;
private _housekeeping;
private _createChildProcess;
private _destroyChildProcess;
private _addListeners;
private _removeListeners;
private _handleUnexpectedExit;
}
export {};