X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fcluster%2Ffixed.ts;h=4a8cb4f1eeeafeb59adf1e30c5d678328cb19b70;hb=ecde6ea8c439bbdd9dc2ca118731b5006a1a9884;hp=c7c95c4b3dad537b39dfd44d37481178035fcb4c;hpb=c38a523624d95c0329fac3d0748883c361db9d93;p=poolifier.git diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index c7c95c4b..4a8cb4f1 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -1,26 +1,14 @@ -import cluster, { type ClusterSettings, 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' +import cluster, { type Worker } from 'node:cluster' + +import type { MessageValue } from '../../utility-types.js' +import { AbstractPool } from '../abstract-pool.js' +import { type PoolOptions, type PoolType, PoolTypes } from '../pool.js' +import { type WorkerType, WorkerTypes } from '../worker.js' /** * 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 +32,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,37 +48,21 @@ export class FixedClusterPool< return cluster.isPrimary } - /** @inheritDoc */ - protected async destroyWorkerNode (workerNodeKey: number): Promise { - this.flushTasksQueue(workerNodeKey) - // FIXME: wait for tasks to be finished - const worker = this.workerNodes[workerNodeKey].worker - const waitWorkerExit = new Promise((resolve) => { - worker.on('exit', () => { - resolve() - }) - }) - worker.on('disconnect', () => { - worker.kill() - }) - await this.sendKillMessageToWorker(workerNodeKey, worker.id) - worker.disconnect() - await waitWorkerExit - } - /** @inheritDoc */ protected sendToWorker ( workerNodeKey: number, message: MessageValue ): void { - this.workerNodes[workerNodeKey].worker.send(message) + this.workerNodes[workerNodeKey]?.worker.send({ + ...message, + workerId: this.getWorkerInfo(workerNodeKey)?.id + } satisfies MessageValue) } /** @inheritDoc */ protected sendStartupMessageToWorker (workerNodeKey: number): void { this.sendToWorker(workerNodeKey, { - ready: false, - workerId: this.workerNodes[workerNodeKey].worker.id + ready: false }) } @@ -102,8 +75,29 @@ 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 */ + protected checkAndEmitDynamicWorkerCreationEvents (): void { + /* noop */ } /** @inheritDoc */