fix: prepare code to fix pool internal IPC for cluster worker
[poolifier.git] / src / pools / cluster / fixed.ts
1 import cluster, { type ClusterSettings, type Worker } from 'node:cluster'
2 import type { MessageValue } from '../../utility-types'
3 import { AbstractPool } from '../abstract-pool'
4 import {
5 type PoolOptions,
6 type PoolType,
7 PoolTypes,
8 type WorkerType,
9 WorkerTypes
10 } from '../pool'
11
12 /**
13 * Options for a poolifier cluster pool.
14 */
15 export interface ClusterPoolOptions extends PoolOptions<Worker> {
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 env?: Record<string, unknown>
22 /**
23 * Cluster settings.
24 *
25 * @see https://nodejs.org/api/cluster.html#cluster_cluster_settings
26 */
27 settings?: ClusterSettings
28 }
29
30 /**
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.
34 *
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.
37 * @author [Christopher Quadflieg](https://github.com/Shinigami92)
38 * @since 2.0.0
39 */
40 export class FixedClusterPool<
41 Data = unknown,
42 Response = unknown
43 > extends AbstractPool<Worker, Data, Response> {
44 /**
45 * Constructs a new poolifier fixed cluster pool.
46 *
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.
50 */
51 public constructor (
52 numberOfWorkers: number,
53 filePath: string,
54 protected readonly opts: ClusterPoolOptions = {}
55 ) {
56 super(numberOfWorkers, filePath, opts)
57 }
58
59 /** @inheritDoc */
60 protected setupHook (): void {
61 cluster.setupPrimary({ ...this.opts.settings, exec: this.filePath })
62 }
63
64 /** @inheritDoc */
65 protected isMain (): boolean {
66 return cluster.isPrimary
67 }
68
69 /** @inheritDoc */
70 protected destroyWorker (worker: Worker): void {
71 this.sendToWorker(worker, { kill: 1 })
72 worker.on('disconnect', () => {
73 worker.kill()
74 })
75 worker.disconnect()
76 }
77
78 /** @inheritDoc */
79 protected sendToWorker (worker: Worker, message: MessageValue<Data>): void {
80 worker.send(message)
81 }
82
83 /** @inheritDoc */
84 protected createWorker (): Worker {
85 return cluster.fork(this.opts.env)
86 }
87
88 /** @inheritDoc */
89 protected get type (): PoolType {
90 return PoolTypes.fixed
91 }
92
93 /** @inheritDoc */
94 protected get worker (): WorkerType {
95 return WorkerTypes.cluster
96 }
97
98 /** @inheritDoc */
99 protected get minSize (): number {
100 return this.numberOfWorkers
101 }
102
103 /** @inheritDoc */
104 protected get maxSize (): number {
105 return this.numberOfWorkers
106 }
107
108 /** @inheritDoc */
109 protected get busy (): boolean {
110 return this.internalBusy()
111 }
112 }