X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fcluster%2Ffixed.ts;h=f22906dfc00f89ba32430f63732492eb986dcfd3;hb=d46660cd0cccf38566337876c3fad0c3795aeb94;hp=4ec81c07bab66b0ebad1cb3eed371ea568989ef0;hpb=be0676b3936d75f22ce55b0f71a1fb03d008a01c;p=poolifier.git diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index 4ec81c07..f22906df 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -1,7 +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 { + type PoolOptions, + type PoolType, + PoolTypes, + type WorkerType, + WorkerTypes +} from '../pool' /** * Options for a poolifier cluster pool. @@ -14,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 } /** @@ -23,9 +35,8 @@ export interface ClusterPoolOptions extends PoolOptions { * * This pool selects the workers in a round robin fashion. * - * @template Data Type of data sent to the worker. This can only be serializable data. - * @template 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 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,54 +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: `{ maxTasks: 1000 }` + * @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 = { maxTasks: 1000 } + protected readonly opts: ClusterPoolOptions = {} ) { super(numberOfWorkers, filePath, opts) } + /** @inheritDoc */ protected setupHook (): void { - setupMaster({ - exec: this.filePath - }) + cluster.setupPrimary({ ...this.opts.settings, exec: this.filePath }) } + /** @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 */ 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 */ protected createWorker (): Worker { - return fork(this.opts.env) + return cluster.fork(this.opts.env) } + /** @inheritDoc */ protected afterWorkerSetup (worker: Worker): void { - // We will attach a listener for every task, - // when task is completed the listener will be removed but to avoid warnings we are increasing the max listeners size - worker.setMaxListeners(this.opts.maxTasks ?? 1000) + // Listen to worker messages. this.registerWorkerMessageListener(worker, super.workerListener()) } + + /** @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 */ + protected get busy (): boolean { + return this.internalBusy() + } }