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