1 import type { Worker
} from
'node:cluster'
2 import cluster from
'node:cluster'
7 } from
'../utility-types'
8 import { AbstractWorker
} from
'./abstract-worker'
9 import type { WorkerOptions
} from
'./worker-options'
12 * A cluster worker used by a poolifier `ClusterPool`.
14 * When this worker is inactive for more than the given `maxInactiveTime`,
15 * it will send a termination request to its main worker.
17 * If you use a `DynamicClusterPool` the extra workers that were created will be terminated,
18 * but the minimum number of workers will be guaranteed.
20 * @typeParam Data - Type of data this worker receives from pool's execution. This can only be serializable data.
21 * @typeParam Response - Type of response the worker sends back to the main worker. This can only be serializable data.
22 * @author [Christopher Quadflieg](https://github.com/Shinigami92)
25 export class ClusterWorker
<
28 > extends AbstractWorker
<Worker
, Data
, Response
> {
30 * Constructs a new poolifier cluster worker.
32 * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked.
33 * @param opts - Options for the worker.
37 | WorkerFunction
<Data
, Response
>
38 | TaskFunctions
<Data
, Response
>,
39 opts
: WorkerOptions
= {}
42 'worker-cluster-pool:poolifier',
51 protected sendToMainWorker (message
: MessageValue
<Response
>): void {
52 this.getMainWorker().send(message
)
56 protected handleError (e
: Error | string): string {
57 return e
instanceof Error ? e
.message
: e