fix: fix formatting issue.
[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 { type PoolOptions, type PoolType, PoolTypes } from '../pool'
5 import { type WorkerType, WorkerTypes } from '../worker'
6
7 /**
8 * Options for a poolifier cluster pool.
9 */
10 export interface ClusterPoolOptions extends PoolOptions<Worker> {
11 /**
12 * Key/value pairs to add to worker process environment.
13 *
14 * @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env
15 */
16 env?: Record<string, unknown>
17 /**
18 * Cluster settings.
19 *
20 * @see https://nodejs.org/api/cluster.html#cluster_cluster_settings
21 */
22 settings?: ClusterSettings
23 }
24
25 /**
26 * A cluster pool with a fixed number of workers.
27 *
28 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
29 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
30 * @author [Christopher Quadflieg](https://github.com/Shinigami92)
31 * @since 2.0.0
32 */
33 export class FixedClusterPool<
34 Data = unknown,
35 Response = unknown
36 > extends AbstractPool<Worker, Data, Response> {
37 /**
38 * Constructs a new poolifier fixed cluster pool.
39 *
40 * @param numberOfWorkers - Number of workers for this pool.
41 * @param filePath - Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
42 * @param opts - Options for this fixed cluster pool.
43 */
44 public constructor (
45 numberOfWorkers: number,
46 filePath: string,
47 protected readonly opts: ClusterPoolOptions = {}
48 ) {
49 super(numberOfWorkers, filePath, opts)
50 }
51
52 /** @inheritDoc */
53 protected setupHook (): void {
54 cluster.setupPrimary({ ...this.opts.settings, exec: this.filePath })
55 }
56
57 /** @inheritDoc */
58 protected isMain (): boolean {
59 return cluster.isPrimary
60 }
61
62 /** @inheritDoc */
63 protected async destroyWorkerNode (workerNodeKey: number): Promise<void> {
64 this.flushTasksQueue(workerNodeKey)
65 // FIXME: wait for tasks to be finished
66 const workerNode = this.workerNodes[workerNodeKey]
67 const worker = workerNode.worker
68 const waitWorkerExit = new Promise<void>(resolve => {
69 worker.on('exit', () => {
70 resolve()
71 })
72 })
73 worker.on('disconnect', () => {
74 worker.kill()
75 })
76 await this.sendKillMessageToWorker(
77 workerNodeKey,
78 workerNode.info.id as number
79 )
80 worker.disconnect()
81 await waitWorkerExit
82 }
83
84 /** @inheritDoc */
85 protected sendToWorker (
86 workerNodeKey: number,
87 message: MessageValue<Data>
88 ): void {
89 this.workerNodes[workerNodeKey].worker.send(message)
90 }
91
92 /** @inheritDoc */
93 protected sendStartupMessageToWorker (workerNodeKey: number): void {
94 this.sendToWorker(workerNodeKey, {
95 ready: false,
96 workerId: this.workerNodes[workerNodeKey].info.id as number
97 })
98 }
99
100 /** @inheritDoc */
101 protected registerWorkerMessageListener<Message extends Data | Response>(
102 workerNodeKey: number,
103 listener: (message: MessageValue<Message>) => void
104 ): void {
105 this.workerNodes[workerNodeKey].worker.on('message', listener)
106 }
107
108 /** @inheritDoc */
109 protected createWorker (): Worker {
110 return cluster.fork(this.opts.env)
111 }
112
113 /** @inheritDoc */
114 protected get type (): PoolType {
115 return PoolTypes.fixed
116 }
117
118 /** @inheritDoc */
119 protected get worker (): WorkerType {
120 return WorkerTypes.cluster
121 }
122
123 /** @inheritDoc */
124 protected get busy (): boolean {
125 return this.internalBusy()
126 }
127 }