X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fcluster%2Ffixed.ts;h=75c0853586c21739ff217eb3be4e63c64e7bb271;hb=9fe8fd698590c2494dc6793cfd8c08026fe88a31;hp=fe920f8a48126529a31dafc2d0c9f31bb4c0e4cf;hpb=658b9aa08266ed9a8ae3c0fc947d237fa2674f09;p=poolifier.git diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index fe920f8a..75c08535 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -60,42 +60,64 @@ 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 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, + workerNode.info.id as number + ) 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) } /** @inheritDoc */ - protected createWorker (): Worker { - return cluster.fork(this.opts.env) + protected sendStartupMessageToWorker (workerNodeKey: number): void { + this.sendToWorker(workerNodeKey, { + ready: false, + workerId: this.workerNodes[workerNodeKey].info.id as number + }) } /** @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 */