X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fcluster%2Ffixed.ts;h=3c69ec2f74318188d1a2b8ac1dcd40b59369aa80;hb=48487131ad37630e3021c3e4feee1b311d8bcd11;hp=0ca0250cecdf4d02f712c5fee30ce699701ba58f;hpb=8881ae32256c9a9ebe6d78c95672f610a3f46719;p=poolifier.git diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index 0ca0250c..3c69ec2f 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -1,13 +1,8 @@ import cluster, { type ClusterSettings, type Worker } from 'node:cluster' import type { MessageValue } from '../../utility-types' import { AbstractPool } from '../abstract-pool' -import { - type PoolOptions, - type PoolType, - PoolTypes, - type WorkerType, - WorkerTypes -} from '../pool' +import { type PoolOptions, type PoolType, PoolTypes } from '../pool' +import { type WorkerType, WorkerTypes } from '../worker' /** * Options for a poolifier cluster pool. @@ -18,8 +13,7 @@ export interface ClusterPoolOptions extends PoolOptions { * * @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env */ - // eslint-disable-next-line @typescript-eslint/no-explicit-any - env?: any + env?: Record /** * Cluster settings. * @@ -31,12 +25,8 @@ export interface ClusterPoolOptions extends PoolOptions { /** * A cluster pool with a fixed number of workers. * - * It is possible to perform tasks in sync or asynchronous mode as you prefer. - * - * This pool selects the workers in a round robin fashion. - * - * @typeParam Data - Type of data sent to the worker. This can only be serializable data. - * @typeParam Response - Type of execution response. This can only be serializable data. + * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. + * @typeParam Response - Type of execution response. This can only be structured-cloneable data. * @author [Christopher Quadflieg](https://github.com/Shinigami92) * @since 2.0.0 */ @@ -54,7 +44,7 @@ export class FixedClusterPool< public constructor ( numberOfWorkers: number, filePath: string, - public readonly opts: ClusterPoolOptions = {} + protected readonly opts: ClusterPoolOptions = {} ) { super(numberOfWorkers, filePath, opts) } @@ -71,8 +61,11 @@ export class FixedClusterPool< /** @inheritDoc */ protected destroyWorker (worker: Worker): void { - this.sendToWorker(worker, { kill: 1 }) - worker.kill() + this.sendToWorker(worker, { kill: true }) + worker.on('disconnect', () => { + worker.kill() + }) + worker.disconnect() } /** @inheritDoc */ @@ -80,25 +73,11 @@ export class FixedClusterPool< worker.send(message) } - /** @inheritDoc */ - protected registerWorkerMessageListener( - worker: Worker, - listener: (message: MessageValue) => void - ): void { - worker.on('message', listener) - } - /** @inheritDoc */ protected createWorker (): Worker { return cluster.fork(this.opts.env) } - /** @inheritDoc */ - protected afterWorkerSetup (worker: Worker): void { - // Listen to worker messages. - this.registerWorkerMessageListener(worker, super.workerListener()) - } - /** @inheritDoc */ protected get type (): PoolType { return PoolTypes.fixed @@ -119,11 +98,6 @@ export class FixedClusterPool< return this.numberOfWorkers } - /** @inheritDoc */ - protected get full (): boolean { - return this.workerNodes.length >= this.numberOfWorkers - } - /** @inheritDoc */ protected get busy (): boolean { return this.internalBusy()