1 import type { Worker
} from
'cluster'
2 import { isMaster
, worker
} from
'cluster'
3 import type { MessageValue
} from
'../utility-types'
4 import { AbstractWorker
} from
'./abstract-worker'
5 import type { WorkerOptions
} from
'./worker-options'
8 * A cluster worker used by a poolifier `ClusterPool`.
10 * When this worker is inactive for more than the given `maxInactiveTime`,
11 * it will send a termination request to its main worker.
13 * If you use a `DynamicClusterPool` the extra workers that were created will be terminated,
14 * but the minimum number of workers will be guaranteed.
16 * @template Data Type of data this worker receives from pool's execution. This can only be serializable data.
17 * @template Response Type of response the worker sends back to the main worker. This can only be serializable data.
19 * @author [Christopher Quadflieg](https://github.com/Shinigami92)
22 export class ClusterWorker
<
25 > extends AbstractWorker
<Worker
, Data
, Response
> {
27 * Constructs a new poolifier cluster worker.
29 * @param fn Function processed by the worker when the pool's `execution` function is invoked.
30 * @param opts Options for the worker.
32 public constructor (fn
: (data
: Data
) => Response
, opts
: WorkerOptions
= {}) {
33 super('worker-cluster-pool:pioardi', isMaster
, fn
, worker
, opts
)
36 protected sendToMainWorker (message
: MessageValue
<Response
>): void {
37 this.getMainWorker().send(message
)
40 protected handleError (e
: Error | string): string {
41 return e
instanceof Error ? e
.message
: e