refactor: imports cleanup
[poolifier.git] / src / pools / cluster / fixed.ts
CommitLineData
059438ff 1import cluster, { type ClusterSettings, type Worker } from 'node:cluster'
deb85c12 2import type { MessageValue } from '../../utility-types'
c97c7edb 3import { AbstractPool } from '../abstract-pool'
184855e6
JB
4import {
5 type PoolOptions,
6 type PoolType,
7 PoolTypes,
8 type WorkerType,
9 WorkerTypes
10} from '../pool'
4ade5f1f 11
729c563d
S
12/**
13 * Options for a poolifier cluster pool.
14 */
c97c7edb 15export interface ClusterPoolOptions extends PoolOptions<Worker> {
325f50bc
S
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 // eslint-disable-next-line @typescript-eslint/no-explicit-any
22 env?: any
1a76932b
JB
23 /**
24 * Cluster settings.
25 *
26 * @see https://nodejs.org/api/cluster.html#cluster_cluster_settings
27 */
28 settings?: ClusterSettings
4ade5f1f
S
29}
30
31/**
729c563d
S
32 * A cluster pool with a fixed number of workers.
33 *
34 * It is possible to perform tasks in sync or asynchronous mode as you prefer.
4ade5f1f 35 *
729c563d
S
36 * This pool selects the workers in a round robin fashion.
37 *
38e795c1 38 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
02706357 39 * @typeParam Response - Type of execution response. This can only be serializable data.
325f50bc
S
40 * @author [Christopher Quadflieg](https://github.com/Shinigami92)
41 * @since 2.0.0
4ade5f1f 42 */
d3c8a1a8 43export class FixedClusterPool<
deb85c12
JB
44 Data = unknown,
45 Response = unknown
d3c8a1a8 46> extends AbstractPool<Worker, Data, Response> {
4ade5f1f 47 /**
729c563d
S
48 * Constructs a new poolifier fixed cluster pool.
49 *
38e795c1
JB
50 * @param numberOfWorkers - Number of workers for this pool.
51 * @param filePath - Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
52 * @param opts - Options for this fixed cluster pool.
4ade5f1f
S
53 */
54 public constructor (
5c5a1fb7 55 numberOfWorkers: number,
c97c7edb 56 filePath: string,
ed6dd37f 57 public readonly opts: ClusterPoolOptions = {}
4ade5f1f 58 ) {
5c5a1fb7 59 super(numberOfWorkers, filePath, opts)
c97c7edb 60 }
4ade5f1f 61
afc003b2 62 /** @inheritDoc */
c97c7edb 63 protected setupHook (): void {
1a76932b 64 cluster.setupPrimary({ ...this.opts.settings, exec: this.filePath })
c97c7edb 65 }
325f50bc 66
afc003b2 67 /** @inheritDoc */
c97c7edb 68 protected isMain (): boolean {
7e0d447f 69 return cluster.isPrimary
4ade5f1f
S
70 }
71
afc003b2 72 /** @inheritDoc */
14a2e530 73 protected destroyWorker (worker: Worker): void {
cefac5ba 74 this.sendToWorker(worker, { kill: 1 })
c97c7edb 75 worker.kill()
4ade5f1f
S
76 }
77
afc003b2 78 /** @inheritDoc */
c97c7edb
S
79 protected sendToWorker (worker: Worker, message: MessageValue<Data>): void {
80 worker.send(message)
4ade5f1f
S
81 }
82
afc003b2 83 /** @inheritDoc */
c319c66b 84 protected registerWorkerMessageListener<Message extends Data | Response>(
4f7fa42a
S
85 worker: Worker,
86 listener: (message: MessageValue<Message>) => void
c97c7edb 87 ): void {
4f7fa42a 88 worker.on('message', listener)
c97c7edb
S
89 }
90
afc003b2 91 /** @inheritDoc */
280c2a77 92 protected createWorker (): Worker {
7e0d447f 93 return cluster.fork(this.opts.env)
4ade5f1f
S
94 }
95
afc003b2 96 /** @inheritDoc */
280c2a77 97 protected afterWorkerSetup (worker: Worker): void {
a05c10de 98 // Listen to worker messages.
be0676b3 99 this.registerWorkerMessageListener(worker, super.workerListener())
4ade5f1f 100 }
7c0ba920 101
afc003b2 102 /** @inheritDoc */
7c0ba920 103 public get type (): PoolType {
6b27d407 104 return PoolTypes.fixed
7c0ba920
JB
105 }
106
184855e6
JB
107 /** @inheritDoc */
108 protected get worker (): WorkerType {
109 return WorkerTypes.cluster
110 }
111
08f3f44c 112 /** @inheritDoc */
6b27d407
JB
113 protected get minSize (): number {
114 return this.numberOfWorkers
115 }
116
117 /** @inheritDoc */
118 protected get maxSize (): number {
08f3f44c
JB
119 return this.numberOfWorkers
120 }
121
afc003b2 122 /** @inheritDoc */
c319c66b 123 protected get full (): boolean {
1f68cede 124 return this.workerNodes.length >= this.numberOfWorkers
c2ade475
JB
125 }
126
afc003b2 127 /** @inheritDoc */
c319c66b 128 protected get busy (): boolean {
c2ade475 129 return this.internalBusy()
7c0ba920 130 }
4ade5f1f 131}