X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker-node.ts;h=4349ed72471b3614e227ab4d374df7f0aba17186;hb=71b2b6d8c0678320a3b6390c058f0d7338b10918;hp=2c39393b05eb416b16f7aba82cdba417ab5b4758;hpb=85aeb3f356d749b96361e74cf17d403a697e3dd7;p=poolifier.git diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index 2c39393b..4349ed72 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -2,6 +2,7 @@ import { MessageChannel } from 'node:worker_threads' import { CircularArray } from '../circular-array' import { Queue } from '../queue' import type { Task } from '../utility-types' +import { DEFAULT_TASK_NAME } from '../utils' import { type IWorker, type IWorkerNode, @@ -74,8 +75,31 @@ implements IWorkerNode { this.tasksUsage.clear() } + /** @inheritdoc */ + public closeChannel (): void { + if (this.info.messageChannel != null) { + this.info.messageChannel?.port1.unref() + this.info.messageChannel?.port2.unref() + this.info.messageChannel?.port1.close() + this.info.messageChannel?.port2.close() + delete this.info.messageChannel + } + } + /** @inheritdoc */ public getTaskWorkerUsage (name: string): WorkerUsage | undefined { + if (name === DEFAULT_TASK_NAME && !Array.isArray(this.info.taskFunctions)) { + throw new Error( + 'Cannot get task worker usage for default task function name when task function names list is not yet defined' + ) + } + if ( + name === DEFAULT_TASK_NAME && + Array.isArray(this.info.taskFunctions) && + this.info.taskFunctions.length > 1 + ) { + name = this.info.taskFunctions[1] + } if (!this.tasksUsage.has(name)) { this.tasksUsage.set(name, this.initTaskWorkerUsage(name)) }