X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fcluster%2Ffixed.ts;h=9470cccd857321388d70e0bf7f09de1a20505cfe;hb=037af3175d6121a6c08a631c992aa0d7fe853a9f;hp=6e9b4b07113bac62000a84ae0a2d8aa05b333532;hpb=21f710aa73abbb5d90328cfb199adfc0f7a70406;p=poolifier.git diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index 6e9b4b07..9470cccd 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -60,42 +60,63 @@ export class FixedClusterPool< } /** @inheritDoc */ - protected destroyWorker (worker: Worker): void { - this.sendToWorker(worker, { kill: true, workerId: worker.id }) + 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 (worker: Worker, message: MessageValue): void { - worker.send(message) + protected sendToWorker ( + workerNodeKey: number, + message: MessageValue + ): void { + this.workerNodes[workerNodeKey].worker.send({ + ...message, + workerId: this.workerNodes[workerNodeKey].info.id as number + }) } /** @inheritDoc */ - protected createWorker (): Worker { - return cluster.fork(this.opts.env) + protected sendStartupMessageToWorker (workerNodeKey: number): void { + this.sendToWorker(workerNodeKey, { + ready: false + }) } /** @inheritDoc */ - protected get type (): PoolType { - return PoolTypes.fixed + protected registerWorkerMessageListener( + workerNodeKey: number, + listener: (message: MessageValue) => void + ): void { + this.workerNodes[workerNodeKey].worker.on('message', listener) } /** @inheritDoc */ - protected get worker (): WorkerType { - return WorkerTypes.cluster + protected createWorker (): Worker { + return cluster.fork(this.opts.env) } /** @inheritDoc */ - protected get minSize (): number { - return this.numberOfWorkers + protected get type (): PoolType { + return PoolTypes.fixed } /** @inheritDoc */ - protected get maxSize (): number { - return this.numberOfWorkers + protected get worker (): WorkerType { + return WorkerTypes.cluster } /** @inheritDoc */