* @param worker - The worker.
* @returns The worker node key if found in the pool worker nodes, `-1` otherwise.
*/
- private getWorkerNodeKey (worker: Worker): number {
+ protected getWorkerNodeKey (worker: Worker): number {
return this.workerNodes.findIndex(
workerNode => workerNode.worker === worker
)
const workerNodeKey = this.getWorkerNodeKey(worker)
const workerInfo = this.getWorkerInfo(workerNodeKey)
workerInfo.ready = false
+ this.workerNodes[workerNodeKey].closeChannel()
this.emitter?.emit(PoolEvents.error, error)
if (this.opts.restartWorkerOnError === true && !this.starting) {
if (workerInfo.dynamic) {
worker.on('online', this.opts.onlineHandler ?? EMPTY_FUNCTION)
worker.on('exit', this.opts.exitHandler ?? EMPTY_FUNCTION)
worker.once('exit', () => {
- const workerInfo = this.getWorkerInfoByWorker(worker)
- if (workerInfo.messageChannel != null) {
- workerInfo.messageChannel?.port1.close()
- workerInfo.messageChannel?.port1.close()
- }
this.removeWorkerNode(worker)
})
* Gets the worker information from the given worker node key.
*
* @param workerNodeKey - The worker node key.
+ * @returns The worker information.
*/
private getWorkerInfo (workerNodeKey: number): WorkerInfo {
return this.workerNodes[workerNodeKey].info
* Gets the worker information from the given worker.
*
* @param worker - The worker.
+ * @returns The worker information.
+ * @throws {@link https://nodejs.org/api/errors.html#class-error} If the worker is not found.
*/
protected getWorkerInfoByWorker (worker: Worker): WorkerInfo {
- return this.workerNodes[this.getWorkerNodeKey(worker)].info
+ const workerNodeKey = this.getWorkerNodeKey(worker)
+ if (workerNodeKey === -1) {
+ throw new Error('Worker not found')
+ }
+ return this.workerNodes[workerNodeKey].info
}
/**