X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fcluster-worker.ts;h=26964aa489f83fff90f8e4306c3dc1bbafc36f23;hb=7d1fc6d3a88ab9820a2e703c33b94de82f532c25;hp=180a5273f464cfee65f5a4b0d6d4f1a8032200fc;hpb=e8a4c3ea4d5123ff7c158d139ca142144ccc1117;p=poolifier.git diff --git a/src/worker/cluster-worker.ts b/src/worker/cluster-worker.ts index 180a5273..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,20 +29,38 @@ 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, - taskFunctions, cluster.worker as Worker, + taskFunctions, opts ) } + /** @inheritDoc */ + protected handleReadyMessage (message: MessageValue): void { + 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 + }) + } + } + } + /** @inheritDoc */ protected get id (): number { return this.getMainWorker().id @@ -52,9 +70,4 @@ export class ClusterWorker< protected sendToMainWorker (message: MessageValue): void { this.getMainWorker().send(message) } - - /** @inheritDoc */ - protected handleError (e: Error | string): string { - return e instanceof Error ? e.message : e - } }