X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fcluster-worker.ts;h=26964aa489f83fff90f8e4306c3dc1bbafc36f23;hb=6934964fbec3005ecb0846f97b97e4a01399a2d3;hp=4c1e118789d53f7228e51a5b974b80575985705f;hpb=7e627c2a5c5ffa392ad9bfd3750ed63d017a1782;p=poolifier.git diff --git a/src/worker/cluster-worker.ts b/src/worker/cluster-worker.ts index 4c1e1187..26964aa4 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,9 +29,7 @@ export class ClusterWorker< * @param opts - Options for the worker. */ public constructor ( - taskFunctions: - | WorkerFunction - | TaskFunctions, + taskFunctions: TaskFunction | TaskFunctions, opts: WorkerOptions = {} ) { super( @@ -45,9 +43,21 @@ export class ClusterWorker< /** @inheritDoc */ protected handleReadyMessage (message: MessageValue): void { - if (!this.isMain && message.workerId === this.id && message.ready != null) { - this.getMainWorker()?.on('message', this.messageListener.bind(this)) - 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, + taskFunctions: this.listTaskFunctions(), + workerId: this.id + }) + } catch { + this.sendToMainWorker({ + ready: false, + taskFunctions: this.listTaskFunctions(), + workerId: this.id + }) + } } }