-import cluster from 'node:cluster'
+import cluster, { type Worker } from 'node:cluster'
import type { MessageValue } from '../utility-types'
import { AbstractWorker } from './abstract-worker'
import type { WorkerOptions } from './worker-options'
export class ClusterWorker<
Data = unknown,
Response = unknown
-> extends AbstractWorker<NodeJS.Process, Data, Response> {
+> extends AbstractWorker<Worker, Data, Response> {
/**
* Constructs a new poolifier cluster worker.
*
'worker-cluster-pool:poolifier',
cluster.isPrimary,
taskFunctions,
- process,
+ cluster.worker as Worker,
opts
)
+ if (!this.isMain) {
+ this.sendToMainWorker({ workerId: this.id, started: true })
+ }
+ }
+
+ /** @inheritDoc */
+ protected get id (): number {
+ return this.getMainWorker().id
}
/** @inheritDoc */
protected sendToMainWorker (message: MessageValue<Response>): void {
- const mainWorker = this.getMainWorker()
- if (mainWorker.send == null) {
- throw new Error('Main worker does not support IPC communication')
- }
- mainWorker.send(message)
+ console.log('sending message to main worker(cluster)', message)
+ this.getMainWorker().send(message)
}
/** @inheritDoc */