fix: brown paper bag bug referencing the same object literal
[poolifier.git] / src / pools / cluster / fixed.ts
CommitLineData
1a76932b 1import type { ClusterSettings, Worker } from 'cluster'
1d43bc4c 2import cluster from 'cluster'
deb85c12 3import type { MessageValue } from '../../utility-types'
c97c7edb 4import { AbstractPool } from '../abstract-pool'
bdaf31cd 5import type { PoolOptions } from '../pool'
7c0ba920 6import { PoolType } from '../pool-internal'
4ade5f1f 7
729c563d
S
8/**
9 * Options for a poolifier cluster pool.
10 */
c97c7edb 11export interface ClusterPoolOptions extends PoolOptions<Worker> {
325f50bc
S
12 /**
13 * Key/value pairs to add to worker process environment.
14 *
15 * @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env
16 */
17 // eslint-disable-next-line @typescript-eslint/no-explicit-any
18 env?: any
1a76932b
JB
19 /**
20 * Cluster settings.
21 *
22 * @see https://nodejs.org/api/cluster.html#cluster_cluster_settings
23 */
24 settings?: ClusterSettings
4ade5f1f
S
25}
26
27/**
729c563d
S
28 * A cluster pool with a fixed number of workers.
29 *
30 * It is possible to perform tasks in sync or asynchronous mode as you prefer.
4ade5f1f 31 *
729c563d
S
32 * This pool selects the workers in a round robin fashion.
33 *
38e795c1
JB
34 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
35 * @typeParam Response - Type of response of execution. This can only be serializable data.
325f50bc
S
36 * @author [Christopher Quadflieg](https://github.com/Shinigami92)
37 * @since 2.0.0
4ade5f1f 38 */
d3c8a1a8 39export class FixedClusterPool<
deb85c12
JB
40 Data = unknown,
41 Response = unknown
d3c8a1a8 42> extends AbstractPool<Worker, Data, Response> {
4ade5f1f 43 /**
729c563d
S
44 * Constructs a new poolifier fixed cluster pool.
45 *
38e795c1
JB
46 * @param numberOfWorkers - Number of workers for this pool.
47 * @param filePath - Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
48 * @param opts - Options for this fixed cluster pool.
4ade5f1f
S
49 */
50 public constructor (
5c5a1fb7 51 numberOfWorkers: number,
c97c7edb 52 filePath: string,
ed6dd37f 53 public readonly opts: ClusterPoolOptions = {}
4ade5f1f 54 ) {
5c5a1fb7 55 super(numberOfWorkers, filePath, opts)
c97c7edb 56 }
4ade5f1f 57
38e795c1 58 /** {@inheritDoc} */
c97c7edb 59 protected setupHook (): void {
1a76932b 60 cluster.setupPrimary({ ...this.opts.settings, exec: this.filePath })
c97c7edb 61 }
325f50bc 62
38e795c1 63 /** {@inheritDoc} */
c97c7edb 64 protected isMain (): boolean {
7e0d447f 65 return cluster.isPrimary
4ade5f1f
S
66 }
67
38e795c1 68 /** {@inheritDoc} */
a35560ba 69 public destroyWorker (worker: Worker): void {
cefac5ba 70 this.sendToWorker(worker, { kill: 1 })
c97c7edb 71 worker.kill()
4ade5f1f
S
72 }
73
38e795c1 74 /** {@inheritDoc} */
c97c7edb
S
75 protected sendToWorker (worker: Worker, message: MessageValue<Data>): void {
76 worker.send(message)
4ade5f1f
S
77 }
78
38e795c1 79 /** {@inheritDoc} */
78cea37e 80 public registerWorkerMessageListener<Message extends Data | Response>(
4f7fa42a
S
81 worker: Worker,
82 listener: (message: MessageValue<Message>) => void
c97c7edb 83 ): void {
4f7fa42a 84 worker.on('message', listener)
c97c7edb
S
85 }
86
38e795c1 87 /** {@inheritDoc} */
280c2a77 88 protected createWorker (): Worker {
7e0d447f 89 return cluster.fork(this.opts.env)
4ade5f1f
S
90 }
91
38e795c1 92 /** {@inheritDoc} */
280c2a77 93 protected afterWorkerSetup (worker: Worker): void {
a05c10de 94 // Listen to worker messages.
be0676b3 95 this.registerWorkerMessageListener(worker, super.workerListener())
4ade5f1f 96 }
7c0ba920 97
38e795c1 98 /** {@inheritDoc} */
7c0ba920
JB
99 public get type (): PoolType {
100 return PoolType.FIXED
101 }
102
38e795c1 103 /** {@inheritDoc} */
7c0ba920
JB
104 public get busy (): boolean {
105 return this.internalGetBusyStatus()
106 }
4ade5f1f 107}