Add a pool option to register a message listener on pool workers (#487)
[poolifier.git] / src / pools / cluster / fixed.ts
1 import { fork, isMaster, setupMaster, Worker } from 'cluster'
2 import type { MessageValue } from '../../utility-types'
3 import type { PoolOptions } from '../abstract-pool'
4 import { AbstractPool } from '../abstract-pool'
5 import { PoolType } from '../pool-internal'
6
7 /**
8 * Options for a poolifier cluster pool.
9 */
10 export interface ClusterPoolOptions extends PoolOptions<Worker> {
11 /**
12 * Key/value pairs to add to worker process environment.
13 *
14 * @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env
15 */
16 // eslint-disable-next-line @typescript-eslint/no-explicit-any
17 env?: any
18 }
19
20 /**
21 * A cluster pool with a fixed number of workers.
22 *
23 * It is possible to perform tasks in sync or asynchronous mode as you prefer.
24 *
25 * This pool selects the workers in a round robin fashion.
26 *
27 * @template DataType of data sent to the worker. This can only be serializable data.
28 * @template ResponseType of response of execution. This can only be serializable data.
29 * @author [Christopher Quadflieg](https://github.com/Shinigami92)
30 * @since 2.0.0
31 */
32 export class FixedClusterPool<
33 Data = unknown,
34 Response = unknown
35 > extends AbstractPool<Worker, Data, Response> {
36 /**
37 * Constructs a new poolifier fixed cluster pool.
38 *
39 * @param numberOfWorkers Number of workers for this pool.
40 * @param filePath Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
41 * @param [opts={}] Options for this fixed cluster pool.
42 */
43 public constructor (
44 numberOfWorkers: number,
45 filePath: string,
46 public readonly opts: ClusterPoolOptions = {}
47 ) {
48 super(numberOfWorkers, filePath, opts)
49 }
50
51 /** @inheritdoc */
52 protected setupHook (): void {
53 setupMaster({
54 exec: this.filePath
55 })
56 }
57
58 /** @inheritdoc */
59 protected isMain (): boolean {
60 return isMaster
61 }
62
63 /** @inheritdoc */
64 public destroyWorker (worker: Worker): void {
65 this.sendToWorker(worker, { kill: 1 })
66 worker.kill()
67 }
68
69 /** @inheritdoc */
70 protected sendToWorker (worker: Worker, message: MessageValue<Data>): void {
71 worker.send(message)
72 }
73
74 /** @inheritdoc */
75 public registerWorkerMessageListener<Message extends Data | Response> (
76 worker: Worker,
77 listener: (message: MessageValue<Message>) => void
78 ): void {
79 worker.on('message', listener)
80 }
81
82 /** @inheritdoc */
83 protected createWorker (): Worker {
84 return fork(this.opts.env)
85 }
86
87 /** @inheritdoc */
88 protected afterWorkerSetup (worker: Worker): void {
89 // Listen worker messages.
90 this.registerWorkerMessageListener(worker, super.workerListener())
91 }
92
93 /** @inheritdoc */
94 public get type (): PoolType {
95 return PoolType.FIXED
96 }
97
98 /** @inheritdoc */
99 public get busy (): boolean {
100 return this.internalGetBusyStatus()
101 }
102 }