X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fcluster%2Ffixed.ts;h=b7bf9c1a70bb150d98414571c0820752a8707c9a;hb=9d9fb7b64a28d150583fe1bcd895893f6736d9d4;hp=9470cccd857321388d70e0bf7f09de1a20505cfe;hpb=01bc762f43e8acaa2420246a4dfa0167c26d2e32;p=poolifier.git diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index 9470cccd..b7bf9c1a 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -1,4 +1,4 @@ -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' @@ -7,20 +7,7 @@ import { type WorkerType, WorkerTypes } from '../worker' /** * Options for a poolifier cluster pool. */ -export interface ClusterPoolOptions extends PoolOptions { - /** - * Key/value pairs to add to worker process environment. - * - * @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env - */ - env?: Record - /** - * Cluster settings. - * - * @see https://nodejs.org/api/cluster.html#cluster_cluster_settings - */ - settings?: ClusterSettings -} +export type ClusterPoolOptions = PoolOptions /** * A cluster pool with a fixed number of workers. @@ -44,9 +31,10 @@ export class FixedClusterPool< public constructor ( numberOfWorkers: number, filePath: string, - protected readonly opts: ClusterPoolOptions = {} + opts: ClusterPoolOptions = {}, + maximumNumberOfWorkers?: number ) { - super(numberOfWorkers, filePath, opts) + super(numberOfWorkers, filePath, opts, maximumNumberOfWorkers) } /** @inheritDoc */ @@ -59,25 +47,6 @@ export class FixedClusterPool< return cluster.isPrimary } - /** @inheritDoc */ - protected async destroyWorkerNode (workerNodeKey: number): Promise { - this.flushTasksQueue(workerNodeKey) - // FIXME: wait for tasks to be finished - const workerNode = this.workerNodes[workerNodeKey] - const worker = workerNode.worker - const waitWorkerExit = new Promise(resolve => { - worker.on('exit', () => { - resolve() - }) - }) - worker.on('disconnect', () => { - worker.kill() - }) - await this.sendKillMessageToWorker(workerNodeKey) - worker.disconnect() - await waitWorkerExit - } - /** @inheritDoc */ protected sendToWorker ( workerNodeKey: number, @@ -85,7 +54,7 @@ export class FixedClusterPool< ): void { this.workerNodes[workerNodeKey].worker.send({ ...message, - workerId: this.workerNodes[workerNodeKey].info.id as number + workerId: this.getWorkerInfo(workerNodeKey).id as number }) } @@ -105,8 +74,24 @@ export class FixedClusterPool< } /** @inheritDoc */ - protected createWorker (): Worker { - return cluster.fork(this.opts.env) + protected registerOnceWorkerMessageListener( + workerNodeKey: number, + listener: (message: MessageValue) => void + ): void { + this.workerNodes[workerNodeKey].worker.once('message', listener) + } + + /** @inheritDoc */ + protected deregisterWorkerMessageListener( + workerNodeKey: number, + listener: (message: MessageValue) => void + ): void { + this.workerNodes[workerNodeKey].worker.off('message', listener) + } + + /** @inheritDoc */ + protected shallCreateDynamicWorker (): boolean { + return false } /** @inheritDoc */