-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 } from '../pool'
import { type WorkerType, WorkerTypes } from '../worker'
-/**
- * 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
-}
-
/**
* A cluster pool with a fixed number of workers.
*
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 */
return cluster.isPrimary
}
- /** @inheritDoc */
- protected async destroyWorkerNode (workerNodeKey: number): Promise<void> {
- this.flushTasksQueue(workerNodeKey)
- // FIXME: wait for tasks to be finished
- const workerNode = this.workerNodes[workerNodeKey]
- const worker = workerNode.worker
- const waitWorkerExit = new Promise<void>(resolve => {
- worker.once('exit', () => {
- resolve()
- })
- })
- worker.once('disconnect', () => {
- worker.kill()
- })
- await this.sendKillMessageToWorker(workerNodeKey)
- worker.disconnect()
- await waitWorkerExit
- }
-
/** @inheritDoc */
protected sendToWorker (
workerNodeKey: number,
this.workerNodes[workerNodeKey].worker.off('message', listener)
}
- /** @inheritDoc */
- protected createWorker (): Worker {
- return cluster.fork(this.opts.env)
- }
-
/** @inheritDoc */
protected get type (): PoolType {
return PoolTypes.fixed