X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fcluster%2Ffixed.ts;h=592b3fe7d4234fe3df267074963b10301ee23707;hb=5df69fabd77b3ec4137a6382e2d84791bf0fe85d;hp=2e89b3232dccbc321907b02a73b583d650ee1661;hpb=38e795c12f0e9daeff7b025147f36f85f486366e;p=poolifier.git diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index 2e89b323..592b3fe7 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -1,9 +1,13 @@ -import type { ClusterSettings, Worker } from 'cluster' -import cluster from 'cluster' +import cluster, { type ClusterSettings, 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' +import { + type PoolOptions, + type PoolType, + PoolTypes, + type WorkerType, + WorkerTypes +} from '../pool' /** * Options for a poolifier cluster pool. @@ -14,8 +18,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. * @@ -32,7 +35,7 @@ export interface ClusterPoolOptions extends PoolOptions { * 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 Response - Type of execution response. This can only be serializable data. * @author [Christopher Quadflieg](https://github.com/Shinigami92) * @since 2.0.0 */ @@ -50,58 +53,73 @@ export class FixedClusterPool< public constructor ( numberOfWorkers: number, filePath: string, - public readonly opts: ClusterPoolOptions = {} + protected readonly opts: ClusterPoolOptions = {} ) { super(numberOfWorkers, filePath, opts) } - /** {@inheritDoc} */ + /** @inheritDoc */ protected setupHook (): void { cluster.setupPrimary({ ...this.opts.settings, exec: this.filePath }) } - /** {@inheritDoc} */ + /** @inheritDoc */ protected isMain (): boolean { 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 cluster.fork(this.opts.env) } - /** {@inheritDoc} */ + /** @inheritDoc */ protected afterWorkerSetup (worker: Worker): void { // 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} */ - public get busy (): boolean { - return this.internalGetBusyStatus() + /** @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() } }