X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fcluster%2Ffixed.ts;h=9d162d8983a7bdce2a68b19e040392be40e83cd4;hb=4f4ae1cb3c33228a023ea599c2648268d15e6c0f;hp=6246320e3072d18371646d431692abda06b85ea1;hpb=a35560bac09e829e1e19f88f8fd1d71a64c9d50b;p=poolifier.git diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index 6246320e..9d162d89 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 { Worker } from 'cluster' +import cluster from 'cluster' import type { MessageValue } from '../../utility-types' import type { PoolOptions } from '../abstract-pool' import { AbstractPool } from '../abstract-pool' +import { PoolType } from '../pool-internal' /** * Options for a poolifier cluster pool. @@ -23,9 +25,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. - * + * @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. * @author [Christopher Quadflieg](https://github.com/Shinigami92) * @since 2.0.0 */ @@ -38,24 +39,26 @@ export class FixedClusterPool< * * @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 [opts={}] Options for this fixed cluster pool. */ public constructor ( numberOfWorkers: number, filePath: string, - public readonly opts: ClusterPoolOptions = { maxTasks: 1000 } + public readonly opts: ClusterPoolOptions = {} ) { super(numberOfWorkers, filePath, opts) } + /** @inheritdoc */ protected setupHook (): void { - setupMaster({ + cluster.setupPrimary({ exec: this.filePath }) } + /** @inheritdoc */ protected isMain (): boolean { - return isMaster + return cluster.isPrimary } /** @inheritdoc */ @@ -64,6 +67,7 @@ export class FixedClusterPool< worker.kill() } + /** @inheritdoc */ protected sendToWorker (worker: Worker, message: MessageValue): void { worker.send(message) } @@ -76,20 +80,24 @@ export class FixedClusterPool< worker.on('message', listener) } - protected unregisterWorkerMessageListener ( - worker: Worker, - listener: (message: MessageValue) => void - ): void { - worker.removeListener('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 worker messages. + this.registerWorkerMessageListener(worker, super.workerListener()) + } + + /** @inheritdoc */ + public get type (): PoolType { + return PoolType.FIXED + } + + /** @inheritdoc */ + public get busy (): boolean { + return this.internalGetBusyStatus() } }