X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fcluster%2Ffixed.ts;h=b522947f25cb6f9497c7935fdcd131d1747e8b34;hb=a7dc540979d13bee0dd320858214b776fa556e4d;hp=0d8021ebf08f0011142a442cbc21175def17db87;hpb=280c2a7728fbeb53612d8bc115a295d0255dd991;p=poolifier.git diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index 0d8021eb..b522947f 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -1,7 +1,8 @@ import { fork, isMaster, setupMaster, Worker } from 'cluster' -import type { JSONValue, MessageValue } from '../../utility-types' +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,27 +24,27 @@ export interface ClusterPoolOptions extends PoolOptions { * * This pool selects the workers in a round robin fashion. * - * @template Data Type of data sent to the worker. - * @template Response Type of response of execution. + * @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. * * @author [Christopher Quadflieg](https://github.com/Shinigami92) * @since 2.0.0 */ export class FixedClusterPool< - Data extends JSONValue = JSONValue, - Response extends JSONValue = JSONValue + Data = unknown, + Response = unknown > extends AbstractPool { /** * 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 opts Options for this fixed cluster pool. Default: `{}` */ public constructor ( numberOfWorkers: number, filePath: string, - public readonly opts: ClusterPoolOptions = { maxTasks: 1000 } + public readonly opts: ClusterPoolOptions = {} ) { super(numberOfWorkers, filePath, opts) } @@ -58,27 +59,22 @@ export class FixedClusterPool< return isMaster } - protected destroyWorker (worker: Worker): void { + /** @inheritdoc */ + public destroyWorker (worker: Worker): void { + this.sendToWorker(worker, { kill: 1 }) worker.kill() - // FIXME: The tests are currently failing, so these must be changed first } protected sendToWorker (worker: Worker, message: MessageValue): void { worker.send(message) } - protected registerWorkerMessageListener ( - port: Worker, - listener: (message: MessageValue) => void + /** @inheritdoc */ + public registerWorkerMessageListener ( + worker: Worker, + listener: (message: MessageValue) => void ): void { - port.on('message', listener) - } - - protected unregisterWorkerMessageListener ( - port: Worker, - listener: (message: MessageValue) => void - ): void { - port.removeListener('message', listener) + worker.on('message', listener) } protected createWorker (): Worker { @@ -86,8 +82,17 @@ export class FixedClusterPool< } 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() } }