X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fcluster%2Ffixed.ts;h=092e97781911ec89ab1e16c5e2af0ca485fe1174;hb=1fbcaa7ca91d26b311689a5e698402bd157a9152;hp=95ea82b9697a12758a8118dc3ec0c308d8893427;hpb=35cf1c03f8379b58e410cad57189912379ee7b36;p=poolifier.git diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index 95ea82b9..092e9778 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -1,7 +1,9 @@ -import { fork, isMaster, setupMaster, Worker } from 'cluster' +import type { ClusterSettings, Worker } from 'cluster' +import cluster from 'cluster' import type { MessageValue } from '../../utility-types' -import type { PoolOptions } from '../abstract-pool' +import { EMPTY_OBJECT_LITERAL } from '../../utils' import { AbstractPool } from '../abstract-pool' +import type { PoolOptions } from '../pool' import { PoolType } from '../pool-internal' /** @@ -15,6 +17,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 +32,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 response of execution. This can only be serializable data. * @author [Christopher Quadflieg](https://github.com/Shinigami92) * @since 2.0.0 */ @@ -36,66 +44,64 @@ 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. + * @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 = {} + public readonly opts: ClusterPoolOptions = EMPTY_OBJECT_LITERAL ) { 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 */ + /** {@inheritDoc} */ public 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} */ + public 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 */ + /** {@inheritDoc} */ public get type (): PoolType { return PoolType.FIXED } - /** @inheritdoc */ + /** {@inheritDoc} */ public get busy (): boolean { return this.internalGetBusyStatus() }