feat: restart worker in case of uncaught error
[poolifier.git] / src / pools / cluster / fixed.ts
CommitLineData
fc3e6586
JB
1import type { ClusterSettings, Worker } from 'node:cluster'
2import cluster from 'node:cluster'
deb85c12 3import type { MessageValue } from '../../utility-types'
c97c7edb 4import { AbstractPool } from '../abstract-pool'
bdaf31cd 5import type { PoolOptions } from '../pool'
c4855468 6import { PoolType } from '../pool'
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 34 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
02706357 35 * @typeParam Response - Type of execution response. 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
afc003b2 58 /** @inheritDoc */
c97c7edb 59 protected setupHook (): void {
1a76932b 60 cluster.setupPrimary({ ...this.opts.settings, exec: this.filePath })
c97c7edb 61 }
325f50bc 62
afc003b2 63 /** @inheritDoc */
c97c7edb 64 protected isMain (): boolean {
7e0d447f 65 return cluster.isPrimary
4ade5f1f
S
66 }
67
afc003b2 68 /** @inheritDoc */
14a2e530 69 protected destroyWorker (worker: Worker): void {
cefac5ba 70 this.sendToWorker(worker, { kill: 1 })
c97c7edb 71 worker.kill()
4ade5f1f
S
72 }
73
afc003b2 74 /** @inheritDoc */
c97c7edb
S
75 protected sendToWorker (worker: Worker, message: MessageValue<Data>): void {
76 worker.send(message)
4ade5f1f
S
77 }
78
afc003b2 79 /** @inheritDoc */
c319c66b 80 protected 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
afc003b2 87 /** @inheritDoc */
280c2a77 88 protected createWorker (): Worker {
7e0d447f 89 return cluster.fork(this.opts.env)
4ade5f1f
S
90 }
91
afc003b2 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
afc003b2 98 /** @inheritDoc */
7c0ba920
JB
99 public get type (): PoolType {
100 return PoolType.FIXED
101 }
102
08f3f44c
JB
103 /** @inheritDoc */
104 public get size (): number {
105 return this.numberOfWorkers
106 }
107
afc003b2 108 /** @inheritDoc */
c319c66b 109 protected get full (): boolean {
1f68cede 110 return this.workerNodes.length >= this.numberOfWorkers
c2ade475
JB
111 }
112
afc003b2 113 /** @inheritDoc */
c319c66b 114 protected get busy (): boolean {
c2ade475 115 return this.internalBusy()
7c0ba920 116 }
4ade5f1f 117}