* @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
)
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.workerNodes[this.getWorkerNodeKey(worker)].closeChannel()
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 {
const workerNodeKey = this.getWorkerNodeKey(worker)
/** @inheritDoc */
protected async destroyWorker (worker: Worker): Promise<void> {
this.sendToWorker(worker, { kill: true, workerId: worker.threadId })
- const workerInfo = this.getWorkerInfoByWorker(worker)
- workerInfo.messageChannel?.port1.close()
- workerInfo.messageChannel?.port2.close()
+ this.workerNodes[this.getWorkerNodeKey(worker)].closeChannel()
await worker.terminate()
}
this.tasksUsage.clear()
}
+ /** @inheritdoc */
+ public closeChannel (): void {
+ if (this.info.messageChannel != null) {
+ this.info.messageChannel?.port1.close()
+ this.info.messageChannel?.port2.close()
+ delete this.info.messageChannel
+ }
+ }
+
/** @inheritdoc */
public getTaskWorkerUsage (name: string): WorkerUsage | undefined {
if (!this.tasksUsage.has(name)) {