X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fcluster%2Ffixed.ts;h=46cabdcb75787700bab966bed3ce3312056659ad;hb=de868be6cc9bcfb6d341ffb14e6407c24a1a0e17;hp=eef394ece07192bcb7b4bad60a6d1c534d0b2e96;hpb=f06e48d8e14dcfe3277bd16b1bd2463136af13e6;p=poolifier.git diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index eef394ec..46cabdcb 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -1,38 +1,14 @@ -import type { ClusterSettings, Worker } from 'node:cluster' -import cluster from 'node:cluster' +import cluster, { type Worker } from 'node:cluster' import type { MessageValue } from '../../utility-types' import { AbstractPool } from '../abstract-pool' -import type { PoolOptions } from '../pool' -import { PoolType } from '../pool-internal' - -/** - * Options for a poolifier cluster pool. - */ -export interface ClusterPoolOptions extends PoolOptions { - /** - * Key/value pairs to add to worker process environment. - * - * @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env - */ - // eslint-disable-next-line @typescript-eslint/no-explicit-any - env?: any - /** - * Cluster settings. - * - * @see https://nodejs.org/api/cluster.html#cluster_cluster_settings - */ - settings?: ClusterSettings -} +import { type PoolOptions, type PoolType, PoolTypes } from '../pool' +import { type WorkerType, WorkerTypes } from '../worker' /** * 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 response of execution. 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 */ @@ -50,7 +26,7 @@ export class FixedClusterPool< public constructor ( numberOfWorkers: number, filePath: string, - public readonly opts: ClusterPoolOptions = {} + protected readonly opts: PoolOptions = {} ) { super(numberOfWorkers, filePath, opts) } @@ -66,47 +42,59 @@ export class FixedClusterPool< } /** @inheritDoc */ - public destroyWorker (worker: Worker): void { - this.sendToWorker(worker, { kill: 1 }) - worker.kill() + protected sendToWorker ( + workerNodeKey: number, + message: MessageValue + ): void { + this.workerNodes[workerNodeKey].worker.send({ + ...message, + workerId: this.getWorkerInfo(workerNodeKey).id as number + }) } /** @inheritDoc */ - protected sendToWorker (worker: Worker, message: MessageValue): void { - worker.send(message) + protected sendStartupMessageToWorker (workerNodeKey: number): void { + this.sendToWorker(workerNodeKey, { + ready: false + }) } /** @inheritDoc */ - public registerWorkerMessageListener( - worker: Worker, + protected registerWorkerMessageListener( + workerNodeKey: number, listener: (message: MessageValue) => void ): void { - worker.on('message', listener) + this.workerNodes[workerNodeKey].worker.on('message', listener) } /** @inheritDoc */ - protected createWorker (): Worker { - return cluster.fork(this.opts.env) + protected registerOnceWorkerMessageListener( + workerNodeKey: number, + listener: (message: MessageValue) => void + ): void { + this.workerNodes[workerNodeKey].worker.once('message', listener) } /** @inheritDoc */ - protected afterWorkerSetup (worker: Worker): void { - // Listen to worker messages. - this.registerWorkerMessageListener(worker, super.workerListener()) + protected deregisterWorkerMessageListener( + workerNodeKey: number, + listener: (message: MessageValue) => void + ): void { + this.workerNodes[workerNodeKey].worker.off('message', listener) } /** @inheritDoc */ - public get type (): PoolType { - return PoolType.FIXED + protected get type (): PoolType { + return PoolTypes.fixed } /** @inheritDoc */ - public get full (): boolean { - return this.workerNodes.length === this.numberOfWorkers + protected get worker (): WorkerType { + return WorkerTypes.cluster } /** @inheritDoc */ - public get busy (): boolean { + protected get busy (): boolean { return this.internalBusy() } }