-import cluster, { type ClusterSettings, type Worker } from 'node:cluster'
+import cluster, { type Worker } from 'node:cluster'
import type { MessageValue } from '../../utility-types'
import { AbstractPool } from '../abstract-pool'
-import {
- type PoolOptions,
- type PoolType,
- PoolTypes,
- type WorkerType,
- WorkerTypes
-} from '../pool'
-
-/**
- * Options for a poolifier cluster pool.
- */
-export interface ClusterPoolOptions extends PoolOptions<Worker> {
- /**
- * Key/value pairs to add to worker process environment.
- *
- * @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env
- */
- env?: Record<string, unknown>
- /**
- * Cluster settings.
- *
- * @see https://nodejs.org/api/cluster.html#cluster_cluster_settings
- */
- settings?: ClusterSettings
-}
+import { type PoolOptions, type PoolType, PoolTypes } from '../pool'
+import { type WorkerType, WorkerTypes } from '../worker'
/**
* A cluster pool with a fixed number of workers.
*
- * It is possible to perform tasks in sync or asynchronous mode as you prefer.
- *
- * @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.
+ * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
+ * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
* @author [Christopher Quadflieg](https://github.com/Shinigami92)
* @since 2.0.0
*/
public constructor (
numberOfWorkers: number,
filePath: string,
- protected readonly opts: ClusterPoolOptions = {}
+ opts: PoolOptions<Worker> = {},
+ maximumNumberOfWorkers?: number
) {
- super(numberOfWorkers, filePath, opts)
+ super(numberOfWorkers, filePath, opts, maximumNumberOfWorkers)
}
/** @inheritDoc */
}
/** @inheritDoc */
- protected destroyWorker (worker: Worker): void {
- this.sendToWorker(worker, { kill: 1 })
- worker.kill()
+ protected sendToWorker (
+ workerNodeKey: number,
+ message: MessageValue<Data>
+ ): void {
+ this.workerNodes[workerNodeKey].worker.send({
+ ...message,
+ workerId: this.getWorkerInfo(workerNodeKey).id as number
+ })
}
/** @inheritDoc */
- protected sendToWorker (worker: Worker, message: MessageValue<Data>): void {
- worker.send(message)
+ protected sendStartupMessageToWorker (workerNodeKey: number): void {
+ this.sendToWorker(workerNodeKey, {
+ ready: false
+ })
}
/** @inheritDoc */
protected registerWorkerMessageListener<Message extends Data | Response>(
- worker: Worker,
+ workerNodeKey: number,
listener: (message: MessageValue<Message>) => void
): void {
- worker.on('message', listener)
+ this.workerNodes[workerNodeKey].worker.on('message', listener)
}
/** @inheritDoc */
- protected createWorker (): Worker {
- return cluster.fork(this.opts.env)
+ protected registerOnceWorkerMessageListener<Message extends Data | Response>(
+ workerNodeKey: number,
+ listener: (message: MessageValue<Message>) => void
+ ): void {
+ this.workerNodes[workerNodeKey].worker.once('message', listener)
}
/** @inheritDoc */
- protected afterWorkerSetup (worker: Worker): void {
- // Listen to worker messages.
- this.registerWorkerMessageListener(worker, super.workerListener())
+ protected deregisterWorkerMessageListener<Message extends Data | Response>(
+ workerNodeKey: number,
+ listener: (message: MessageValue<Message>) => void
+ ): void {
+ this.workerNodes[workerNodeKey].worker.off('message', listener)
}
/** @inheritDoc */
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()