refactor: freeze empty function type
[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'
4f3c3d89 4import { EMPTY_OBJECT_LITERAL } from '../../utils'
c97c7edb 5import { AbstractPool } from '../abstract-pool'
bdaf31cd 6import type { PoolOptions } from '../pool'
7c0ba920 7import { PoolType } from '../pool-internal'
4ade5f1f 8
729c563d
S
9/**
10 * Options for a poolifier cluster pool.
11 */
c97c7edb 12export interface ClusterPoolOptions extends PoolOptions<Worker> {
325f50bc
S
13 /**
14 * Key/value pairs to add to worker process environment.
15 *
16 * @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env
17 */
18 // eslint-disable-next-line @typescript-eslint/no-explicit-any
19 env?: any
1a76932b
JB
20 /**
21 * Cluster settings.
22 *
23 * @see https://nodejs.org/api/cluster.html#cluster_cluster_settings
24 */
25 settings?: ClusterSettings
4ade5f1f
S
26}
27
28/**
729c563d
S
29 * A cluster pool with a fixed number of workers.
30 *
31 * It is possible to perform tasks in sync or asynchronous mode as you prefer.
4ade5f1f 32 *
729c563d
S
33 * This pool selects the workers in a round robin fashion.
34 *
38e795c1
JB
35 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
36 * @typeParam Response - Type of response of execution. This can only be serializable 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,
4f3c3d89 54 public readonly opts: ClusterPoolOptions = EMPTY_OBJECT_LITERAL
4ade5f1f 55 ) {
5c5a1fb7 56 super(numberOfWorkers, filePath, opts)
c97c7edb 57 }
4ade5f1f 58
38e795c1 59 /** {@inheritDoc} */
c97c7edb 60 protected setupHook (): void {
1a76932b 61 cluster.setupPrimary({ ...this.opts.settings, exec: this.filePath })
c97c7edb 62 }
325f50bc 63
38e795c1 64 /** {@inheritDoc} */
c97c7edb 65 protected isMain (): boolean {
7e0d447f 66 return cluster.isPrimary
4ade5f1f
S
67 }
68
38e795c1 69 /** {@inheritDoc} */
a35560ba 70 public destroyWorker (worker: Worker): void {
cefac5ba 71 this.sendToWorker(worker, { kill: 1 })
c97c7edb 72 worker.kill()
4ade5f1f
S
73 }
74
38e795c1 75 /** {@inheritDoc} */
c97c7edb
S
76 protected sendToWorker (worker: Worker, message: MessageValue<Data>): void {
77 worker.send(message)
4ade5f1f
S
78 }
79
38e795c1 80 /** {@inheritDoc} */
78cea37e 81 public registerWorkerMessageListener<Message extends Data | Response>(
4f7fa42a
S
82 worker: Worker,
83 listener: (message: MessageValue<Message>) => void
c97c7edb 84 ): void {
4f7fa42a 85 worker.on('message', listener)
c97c7edb
S
86 }
87
38e795c1 88 /** {@inheritDoc} */
280c2a77 89 protected createWorker (): Worker {
7e0d447f 90 return cluster.fork(this.opts.env)
4ade5f1f
S
91 }
92
38e795c1 93 /** {@inheritDoc} */
280c2a77 94 protected afterWorkerSetup (worker: Worker): void {
a05c10de 95 // Listen to worker messages.
be0676b3 96 this.registerWorkerMessageListener(worker, super.workerListener())
4ade5f1f 97 }
7c0ba920 98
38e795c1 99 /** {@inheritDoc} */
7c0ba920
JB
100 public get type (): PoolType {
101 return PoolType.FIXED
102 }
103
38e795c1 104 /** {@inheritDoc} */
7c0ba920
JB
105 public get busy (): boolean {
106 return this.internalGetBusyStatus()
107 }
4ade5f1f 108}