X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fcluster%2Ffixed.ts;h=b5b32527cf595b8b4ec8f52f3290a10e4647cbd4;hb=07588f306649b9df235aaf65de4842e99532ed6a;hp=3f56c858b0ed5b363fc19da06eb1b9a295a66dd5;hpb=5b59d53ed7dd50ad5d42c0d5c146813e2c38132a;p=poolifier.git diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index 3f56c858..b5b32527 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -1,13 +1,8 @@ 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, - type WorkerType, - WorkerTypes -} from '../pool' +import { type PoolOptions, type PoolType, PoolTypes } from '../pool' +import { type WorkerType, WorkerTypes } from '../worker' /** * Options for a poolifier cluster pool. @@ -30,10 +25,8 @@ export interface ClusterPoolOptions extends PoolOptions { /** * 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 */ @@ -67,25 +60,45 @@ export class FixedClusterPool< } /** @inheritDoc */ - protected destroyWorker (worker: Worker): void { - this.sendToWorker(worker, { kill: 1 }) + 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() }) + this.sendToWorker(workerNodeKey, { kill: true, workerId: worker.id }) worker.disconnect() + await waitWorkerExit + } + + /** @inheritDoc */ + protected sendToWorker ( + workerNodeKey: number, + message: MessageValue + ): void { + this.workerNodes[workerNodeKey].worker.send(message) } /** @inheritDoc */ - protected sendToWorker (worker: Worker, message: MessageValue): void { - worker.send(message) + protected sendStartupMessageToWorker (workerNodeKey: number): void { + this.sendToWorker(workerNodeKey, { + ready: false, + workerId: this.workerNodes[workerNodeKey].worker.id + }) } /** @inheritDoc */ protected registerWorkerMessageListener( - worker: Worker, + workerNodeKey: number, listener: (message: MessageValue) => void ): void { - worker.on('message', listener) + this.workerNodes[workerNodeKey].worker.on('message', listener) } /** @inheritDoc */ @@ -93,12 +106,6 @@ export class FixedClusterPool< return cluster.fork(this.opts.env) } - /** @inheritDoc */ - protected afterWorkerSetup (worker: Worker): void { - // Listen to worker messages. - this.registerWorkerMessageListener(worker, super.workerListener()) - } - /** @inheritDoc */ protected get type (): PoolType { return PoolTypes.fixed