refactor: cleanup exports
[poolifier.git] / src / pools / cluster / fixed.ts
CommitLineData
c3719753 1import cluster, { type Worker } from 'node:cluster'
deb85c12 2import type { MessageValue } from '../../utility-types'
c97c7edb 3import { AbstractPool } from '../abstract-pool'
4b628b48
JB
4import { type PoolOptions, type PoolType, PoolTypes } from '../pool'
5import { type WorkerType, WorkerTypes } from '../worker'
4ade5f1f 6
4ade5f1f 7/**
729c563d
S
8 * A cluster pool with a fixed number of workers.
9 *
e102732c
JB
10 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
11 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
325f50bc
S
12 * @author [Christopher Quadflieg](https://github.com/Shinigami92)
13 * @since 2.0.0
4ade5f1f 14 */
d3c8a1a8 15export class FixedClusterPool<
deb85c12
JB
16 Data = unknown,
17 Response = unknown
d3c8a1a8 18> extends AbstractPool<Worker, Data, Response> {
4ade5f1f 19 /**
729c563d
S
20 * Constructs a new poolifier fixed cluster pool.
21 *
38e795c1
JB
22 * @param numberOfWorkers - Number of workers for this pool.
23 * @param filePath - Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
24 * @param opts - Options for this fixed cluster pool.
4ade5f1f
S
25 */
26 public constructor (
5c5a1fb7 27 numberOfWorkers: number,
c97c7edb 28 filePath: string,
c3719753 29 protected readonly opts: PoolOptions<Worker> = {}
4ade5f1f 30 ) {
5c5a1fb7 31 super(numberOfWorkers, filePath, opts)
c97c7edb 32 }
4ade5f1f 33
afc003b2 34 /** @inheritDoc */
c97c7edb 35 protected setupHook (): void {
1a76932b 36 cluster.setupPrimary({ ...this.opts.settings, exec: this.filePath })
c97c7edb 37 }
325f50bc 38
afc003b2 39 /** @inheritDoc */
c97c7edb 40 protected isMain (): boolean {
7e0d447f 41 return cluster.isPrimary
4ade5f1f
S
42 }
43
afc003b2 44 /** @inheritDoc */
aa9eede8
JB
45 protected sendToWorker (
46 workerNodeKey: number,
47 message: MessageValue<Data>
48 ): void {
72ae84a2
JB
49 this.workerNodes[workerNodeKey].worker.send({
50 ...message,
dbfa7948 51 workerId: this.getWorkerInfo(workerNodeKey).id as number
72ae84a2 52 })
4ade5f1f
S
53 }
54
85aeb3f3 55 /** @inheritDoc */
aa9eede8
JB
56 protected sendStartupMessageToWorker (workerNodeKey: number): void {
57 this.sendToWorker(workerNodeKey, {
e9dd5b66 58 ready: false
85aeb3f3
JB
59 })
60 }
61
62 /** @inheritDoc */
63 protected registerWorkerMessageListener<Message extends Data | Response>(
aa9eede8 64 workerNodeKey: number,
85aeb3f3
JB
65 listener: (message: MessageValue<Message>) => void
66 ): void {
aa9eede8 67 this.workerNodes[workerNodeKey].worker.on('message', listener)
85aeb3f3
JB
68 }
69
ae036c3e
JB
70 /** @inheritDoc */
71 protected registerOnceWorkerMessageListener<Message extends Data | Response>(
72 workerNodeKey: number,
73 listener: (message: MessageValue<Message>) => void
74 ): void {
75 this.workerNodes[workerNodeKey].worker.once('message', listener)
76 }
77
78 /** @inheritDoc */
79 protected deregisterWorkerMessageListener<Message extends Data | Response>(
80 workerNodeKey: number,
81 listener: (message: MessageValue<Message>) => void
82 ): void {
83 this.workerNodes[workerNodeKey].worker.off('message', listener)
84 }
85
afc003b2 86 /** @inheritDoc */
8881ae32 87 protected get type (): PoolType {
6b27d407 88 return PoolTypes.fixed
7c0ba920
JB
89 }
90
184855e6
JB
91 /** @inheritDoc */
92 protected get worker (): WorkerType {
93 return WorkerTypes.cluster
94 }
95
afc003b2 96 /** @inheritDoc */
c319c66b 97 protected get busy (): boolean {
c2ade475 98 return this.internalBusy()
7c0ba920 99 }
4ade5f1f 100}