fix: prepare code to fix pool internal IPC for cluster worker
[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 */
82f36766 21 env?: Record<string, unknown>
1a76932b
JB
22 /**
23 * Cluster settings.
24 *
25 * @see https://nodejs.org/api/cluster.html#cluster_cluster_settings
26 */
27 settings?: ClusterSettings
4ade5f1f
S
28}
29
30/**
729c563d
S
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.
4ade5f1f 34 *
e102732c
JB
35 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
36 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
325f50bc
S
37 * @author [Christopher Quadflieg](https://github.com/Shinigami92)
38 * @since 2.0.0
4ade5f1f 39 */
d3c8a1a8 40export class FixedClusterPool<
deb85c12
JB
41 Data = unknown,
42 Response = unknown
d3c8a1a8 43> extends AbstractPool<Worker, Data, Response> {
4ade5f1f 44 /**
729c563d
S
45 * Constructs a new poolifier fixed cluster pool.
46 *
38e795c1
JB
47 * @param numberOfWorkers - Number of workers for this pool.
48 * @param filePath - Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
49 * @param opts - Options for this fixed cluster pool.
4ade5f1f
S
50 */
51 public constructor (
5c5a1fb7 52 numberOfWorkers: number,
c97c7edb 53 filePath: string,
dea903a8 54 protected readonly opts: ClusterPoolOptions = {}
4ade5f1f 55 ) {
5c5a1fb7 56 super(numberOfWorkers, filePath, opts)
c97c7edb 57 }
4ade5f1f 58
afc003b2 59 /** @inheritDoc */
c97c7edb 60 protected setupHook (): void {
1a76932b 61 cluster.setupPrimary({ ...this.opts.settings, exec: this.filePath })
c97c7edb 62 }
325f50bc 63
afc003b2 64 /** @inheritDoc */
c97c7edb 65 protected isMain (): boolean {
7e0d447f 66 return cluster.isPrimary
4ade5f1f
S
67 }
68
afc003b2 69 /** @inheritDoc */
14a2e530 70 protected destroyWorker (worker: Worker): void {
cefac5ba 71 this.sendToWorker(worker, { kill: 1 })
5b59d53e
JB
72 worker.on('disconnect', () => {
73 worker.kill()
74 })
75 worker.disconnect()
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 */
280c2a77 84 protected createWorker (): Worker {
7e0d447f 85 return cluster.fork(this.opts.env)
4ade5f1f
S
86 }
87
afc003b2 88 /** @inheritDoc */
8881ae32 89 protected get type (): PoolType {
6b27d407 90 return PoolTypes.fixed
7c0ba920
JB
91 }
92
184855e6
JB
93 /** @inheritDoc */
94 protected get worker (): WorkerType {
95 return WorkerTypes.cluster
96 }
97
08f3f44c 98 /** @inheritDoc */
6b27d407
JB
99 protected get minSize (): number {
100 return this.numberOfWorkers
101 }
102
103 /** @inheritDoc */
104 protected get maxSize (): number {
08f3f44c
JB
105 return this.numberOfWorkers
106 }
107
afc003b2 108 /** @inheritDoc */
c319c66b 109 protected get busy (): boolean {
c2ade475 110 return this.internalBusy()
7c0ba920 111 }
4ade5f1f 112}