X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fcluster-worker.ts;h=d972692ec6eb4f182479cac008c47a1f20b75d8d;hb=449cd15417158c0d1675a8aec8bcc514ff33b5a3;hp=c820641ddbb10319b454bf5b25154312703cc446;hpb=85aeb3f356d749b96361e74cf17d403a697e3dd7;p=poolifier.git diff --git a/src/worker/cluster-worker.ts b/src/worker/cluster-worker.ts index c820641d..d972692e 100644 --- a/src/worker/cluster-worker.ts +++ b/src/worker/cluster-worker.ts @@ -2,7 +2,7 @@ import cluster, { type Worker } from 'node:cluster' import type { MessageValue } from '../utility-types' import { AbstractWorker } from './abstract-worker' import type { WorkerOptions } from './worker-options' -import type { TaskFunctions, WorkerFunction } from './worker-functions' +import type { TaskFunction, TaskFunctions } from './task-functions' /** * A cluster worker used by a poolifier `ClusterPool`. @@ -29,27 +29,27 @@ export class ClusterWorker< * @param opts - Options for the worker. */ public constructor ( - taskFunctions: - | WorkerFunction - | TaskFunctions, + taskFunctions: TaskFunction | TaskFunctions, opts: WorkerOptions = {} ) { - super( - 'worker-cluster-pool:poolifier', - cluster.isPrimary, - cluster.worker as Worker, - taskFunctions, - opts - ) - if (!this.isMain) { - this.getMainWorker()?.on('message', this.messageListener.bind(this)) - } + super(cluster.isPrimary, cluster.worker as Worker, taskFunctions, opts) } /** @inheritDoc */ protected handleReadyMessage (message: MessageValue): void { - if (message.workerId === this.id && message.ready != null) { - !this.isMain && this.sendToMainWorker({ ready: true, workerId: this.id }) + if (message.workerId === this.id && message.ready === false) { + try { + this.getMainWorker().on('message', this.messageListener.bind(this)) + this.sendToMainWorker({ + ready: true, + taskFunctionNames: this.listTaskFunctionNames() + }) + } catch { + this.sendToMainWorker({ + ready: false, + taskFunctionNames: this.listTaskFunctionNames() + }) + } } } @@ -59,7 +59,9 @@ export class ClusterWorker< } /** @inheritDoc */ - protected sendToMainWorker (message: MessageValue): void { - this.getMainWorker().send(message) + protected readonly sendToMainWorker = ( + message: MessageValue + ): void => { + this.getMainWorker().send({ ...message, workerId: this.id }) } }