X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fcluster%2Ffixed.ts;h=f22906dfc00f89ba32430f63732492eb986dcfd3;hb=d46660cd0cccf38566337876c3fad0c3795aeb94;hp=ccffc6bba9cbb32888170cba58ca59b0fab8a8dd;hpb=91bfd799ee1722e0181fe158697523b575f28de3;p=poolifier.git diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index ccffc6bb..f22906df 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -1,8 +1,13 @@ -import { fork, isMaster, setupMaster, Worker } from 'cluster' +import cluster, { type ClusterSettings, type Worker } from 'node:cluster' import type { MessageValue } from '../../utility-types' -import type { PoolOptions } from '../abstract-pool' import { AbstractPool } from '../abstract-pool' -import { PoolType } from '../pool-internal' +import { + type PoolOptions, + type PoolType, + PoolTypes, + type WorkerType, + WorkerTypes +} from '../pool' /** * Options for a poolifier cluster pool. @@ -15,6 +20,12 @@ export interface ClusterPoolOptions extends PoolOptions { */ // 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 } /** @@ -24,8 +35,8 @@ export interface ClusterPoolOptions extends PoolOptions { * * This pool selects the workers in a round robin fashion. * - * @template DataType of data sent to the worker. This can only be serializable data. - * @template ResponseType of response of execution. This can only be serializable data. + * @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. * @author [Christopher Quadflieg](https://github.com/Shinigami92) * @since 2.0.0 */ @@ -36,67 +47,80 @@ export class FixedClusterPool< /** * Constructs a new poolifier fixed cluster pool. * - * @param numberOfWorkers Number of workers for this pool. - * @param filePath Path to an implementation of a `ClusterWorker` file, which can be relative or absolute. - * @param opts Options for this fixed cluster pool. Default: `{}` + * @param numberOfWorkers - Number of workers for this pool. + * @param filePath - Path to an implementation of a `ClusterWorker` file, which can be relative or absolute. + * @param opts - Options for this fixed cluster pool. */ public constructor ( numberOfWorkers: number, filePath: string, - public readonly opts: ClusterPoolOptions = {} + protected readonly opts: ClusterPoolOptions = {} ) { super(numberOfWorkers, filePath, opts) } - /** @inheritdoc */ + /** @inheritDoc */ protected setupHook (): void { - setupMaster({ - exec: this.filePath - }) + cluster.setupPrimary({ ...this.opts.settings, exec: this.filePath }) } - /** @inheritdoc */ + /** @inheritDoc */ protected isMain (): boolean { - return isMaster + return cluster.isPrimary } - /** @inheritdoc */ - public destroyWorker (worker: Worker): void { + /** @inheritDoc */ + protected destroyWorker (worker: Worker): void { this.sendToWorker(worker, { kill: 1 }) worker.kill() } - /** @inheritdoc */ + /** @inheritDoc */ protected sendToWorker (worker: Worker, message: MessageValue): void { worker.send(message) } - /** @inheritdoc */ - public registerWorkerMessageListener ( + /** @inheritDoc */ + protected registerWorkerMessageListener( worker: Worker, listener: (message: MessageValue) => void ): void { worker.on('message', listener) } - /** @inheritdoc */ + /** @inheritDoc */ protected createWorker (): Worker { - return fork(this.opts.env) + return cluster.fork(this.opts.env) } - /** @inheritdoc */ + /** @inheritDoc */ protected afterWorkerSetup (worker: Worker): void { - // Listen worker messages. + // Listen to worker messages. this.registerWorkerMessageListener(worker, super.workerListener()) } - /** @inheritdoc */ - public get type (): PoolType { - return PoolType.FIXED + /** @inheritDoc */ + protected get type (): PoolType { + return PoolTypes.fixed + } + + /** @inheritDoc */ + protected get worker (): WorkerType { + return WorkerTypes.cluster + } + + /** @inheritDoc */ + protected get minSize (): number { + return this.numberOfWorkers + } + + /** @inheritDoc */ + protected get maxSize (): number { + return this.numberOfWorkers } - /** @inheritdoc */ - public get busy (): boolean { - return this.internalGetBusyStatus() + /** @inheritDoc */ + protected get busy (): boolean { + return this.internalBusy() } }