Merge branch 'master' into elu-strategy
[poolifier.git] / src / pools / cluster / fixed.ts
1 import cluster, { type ClusterSettings, type Worker } from 'node:cluster'
2 import type { MessageValue } from '../../utility-types'
3 import { AbstractPool } from '../abstract-pool'
4 import {
5 type PoolOptions,
6 type PoolType,
7 PoolTypes,
8 type WorkerType,
9 WorkerTypes
10 } from '../pool'
11
12 /**
13 * Options for a poolifier cluster pool.
14 */
15 export interface ClusterPoolOptions extends PoolOptions<Worker> {
16 /**
17 * Key/value pairs to add to worker process environment.
18 *
19 * @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env
20 */
21 env?: Record<string, unknown>
22 /**
23 * Cluster settings.
24 *
25 * @see https://nodejs.org/api/cluster.html#cluster_cluster_settings
26 */
27 settings?: ClusterSettings
28 }
29
30 /**
31 * A cluster pool with a fixed number of workers.
32 *
33 * It is possible to perform tasks in sync or asynchronous mode as you prefer.
34 *
35 * This pool selects the workers in a round robin fashion.
36 *
37 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
38 * @typeParam Response - Type of execution response. This can only be serializable data.
39 * @author [Christopher Quadflieg](https://github.com/Shinigami92)
40 * @since 2.0.0
41 */
42 export class FixedClusterPool<
43 Data = unknown,
44 Response = unknown
45 > extends AbstractPool<Worker, Data, Response> {
46 /**
47 * Constructs a new poolifier fixed cluster pool.
48 *
49 * @param numberOfWorkers - Number of workers for this pool.
50 * @param filePath - Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
51 * @param opts - Options for this fixed cluster pool.
52 */
53 public constructor (
54 numberOfWorkers: number,
55 filePath: string,
56 protected readonly opts: ClusterPoolOptions = {}
57 ) {
58 super(numberOfWorkers, filePath, opts)
59 }
60
61 /** @inheritDoc */
62 protected setupHook (): void {
63 cluster.setupPrimary({ ...this.opts.settings, exec: this.filePath })
64 }
65
66 /** @inheritDoc */
67 protected isMain (): boolean {
68 return cluster.isPrimary
69 }
70
71 /** @inheritDoc */
72 protected destroyWorker (worker: Worker): void {
73 this.sendToWorker(worker, { kill: 1 })
74 worker.kill()
75 }
76
77 /** @inheritDoc */
78 protected sendToWorker (worker: Worker, message: MessageValue<Data>): void {
79 worker.send(message)
80 }
81
82 /** @inheritDoc */
83 protected registerWorkerMessageListener<Message extends Data | Response>(
84 worker: Worker,
85 listener: (message: MessageValue<Message>) => void
86 ): void {
87 worker.on('message', listener)
88 }
89
90 /** @inheritDoc */
91 protected createWorker (): Worker {
92 return cluster.fork(this.opts.env)
93 }
94
95 /** @inheritDoc */
96 protected afterWorkerSetup (worker: Worker): void {
97 // Listen to worker messages.
98 this.registerWorkerMessageListener(worker, super.workerListener())
99 }
100
101 /** @inheritDoc */
102 protected get type (): PoolType {
103 return PoolTypes.fixed
104 }
105
106 /** @inheritDoc */
107 protected get worker (): WorkerType {
108 return WorkerTypes.cluster
109 }
110
111 /** @inheritDoc */
112 protected get minSize (): number {
113 return this.numberOfWorkers
114 }
115
116 /** @inheritDoc */
117 protected get maxSize (): number {
118 return this.numberOfWorkers
119 }
120
121 /** @inheritDoc */
122 protected get busy (): boolean {
123 return this.internalBusy()
124 }
125 }