Commit | Line | Data |
---|---|---|
059438ff | 1 | import cluster, { type ClusterSettings, type Worker } from 'node:cluster' |
deb85c12 | 2 | import type { MessageValue } from '../../utility-types' |
c97c7edb | 3 | import { AbstractPool } from '../abstract-pool' |
184855e6 JB |
4 | import { |
5 | type PoolOptions, | |
6 | type PoolType, | |
7 | PoolTypes, | |
8 | type WorkerType, | |
9 | WorkerTypes | |
10 | } from '../pool' | |
4ade5f1f | 11 | |
729c563d S |
12 | /** |
13 | * Options for a poolifier cluster pool. | |
14 | */ | |
c97c7edb | 15 | export interface ClusterPoolOptions extends PoolOptions<Worker> { |
325f50bc S |
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 | */ | |
82f36766 | 21 | env?: Record<string, unknown> |
1a76932b JB |
22 | /** |
23 | * Cluster settings. | |
24 | * | |
25 | * @see https://nodejs.org/api/cluster.html#cluster_cluster_settings | |
26 | */ | |
27 | settings?: ClusterSettings | |
4ade5f1f S |
28 | } |
29 | ||
30 | /** | |
729c563d S |
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. | |
4ade5f1f | 34 | * |
729c563d S |
35 | * This pool selects the workers in a round robin fashion. |
36 | * | |
38e795c1 | 37 | * @typeParam Data - Type of data sent to the worker. This can only be serializable data. |
02706357 | 38 | * @typeParam Response - Type of execution response. This can only be serializable data. |
325f50bc S |
39 | * @author [Christopher Quadflieg](https://github.com/Shinigami92) |
40 | * @since 2.0.0 | |
4ade5f1f | 41 | */ |
d3c8a1a8 | 42 | export class FixedClusterPool< |
deb85c12 JB |
43 | Data = unknown, |
44 | Response = unknown | |
d3c8a1a8 | 45 | > extends AbstractPool<Worker, Data, Response> { |
4ade5f1f | 46 | /** |
729c563d S |
47 | * Constructs a new poolifier fixed cluster pool. |
48 | * | |
38e795c1 JB |
49 | * @param numberOfWorkers - Number of workers for this pool. |
50 | * @param filePath - Path to an implementation of a `ClusterWorker` file, which can be relative or absolute. | |
51 | * @param opts - Options for this fixed cluster pool. | |
4ade5f1f S |
52 | */ |
53 | public constructor ( | |
5c5a1fb7 | 54 | numberOfWorkers: number, |
c97c7edb | 55 | filePath: string, |
dea903a8 | 56 | protected readonly opts: ClusterPoolOptions = {} |
4ade5f1f | 57 | ) { |
5c5a1fb7 | 58 | super(numberOfWorkers, filePath, opts) |
c97c7edb | 59 | } |
4ade5f1f | 60 | |
afc003b2 | 61 | /** @inheritDoc */ |
c97c7edb | 62 | protected setupHook (): void { |
1a76932b | 63 | cluster.setupPrimary({ ...this.opts.settings, exec: this.filePath }) |
c97c7edb | 64 | } |
325f50bc | 65 | |
afc003b2 | 66 | /** @inheritDoc */ |
c97c7edb | 67 | protected isMain (): boolean { |
7e0d447f | 68 | return cluster.isPrimary |
4ade5f1f S |
69 | } |
70 | ||
afc003b2 | 71 | /** @inheritDoc */ |
14a2e530 | 72 | protected destroyWorker (worker: Worker): void { |
cefac5ba | 73 | this.sendToWorker(worker, { kill: 1 }) |
c97c7edb | 74 | worker.kill() |
4ade5f1f S |
75 | } |
76 | ||
afc003b2 | 77 | /** @inheritDoc */ |
c97c7edb S |
78 | protected sendToWorker (worker: Worker, message: MessageValue<Data>): void { |
79 | worker.send(message) | |
4ade5f1f S |
80 | } |
81 | ||
afc003b2 | 82 | /** @inheritDoc */ |
c319c66b | 83 | protected registerWorkerMessageListener<Message extends Data | Response>( |
4f7fa42a S |
84 | worker: Worker, |
85 | listener: (message: MessageValue<Message>) => void | |
c97c7edb | 86 | ): void { |
4f7fa42a | 87 | worker.on('message', listener) |
c97c7edb S |
88 | } |
89 | ||
afc003b2 | 90 | /** @inheritDoc */ |
280c2a77 | 91 | protected createWorker (): Worker { |
7e0d447f | 92 | return cluster.fork(this.opts.env) |
4ade5f1f S |
93 | } |
94 | ||
afc003b2 | 95 | /** @inheritDoc */ |
280c2a77 | 96 | protected afterWorkerSetup (worker: Worker): void { |
a05c10de | 97 | // Listen to worker messages. |
be0676b3 | 98 | this.registerWorkerMessageListener(worker, super.workerListener()) |
4ade5f1f | 99 | } |
7c0ba920 | 100 | |
afc003b2 | 101 | /** @inheritDoc */ |
8881ae32 | 102 | protected get type (): PoolType { |
6b27d407 | 103 | return PoolTypes.fixed |
7c0ba920 JB |
104 | } |
105 | ||
184855e6 JB |
106 | /** @inheritDoc */ |
107 | protected get worker (): WorkerType { | |
108 | return WorkerTypes.cluster | |
109 | } | |
110 | ||
08f3f44c | 111 | /** @inheritDoc */ |
6b27d407 JB |
112 | protected get minSize (): number { |
113 | return this.numberOfWorkers | |
114 | } | |
115 | ||
116 | /** @inheritDoc */ | |
117 | protected get maxSize (): number { | |
08f3f44c JB |
118 | return this.numberOfWorkers |
119 | } | |
120 | ||
afc003b2 | 121 | /** @inheritDoc */ |
c319c66b | 122 | protected get busy (): boolean { |
c2ade475 | 123 | return this.internalBusy() |
7c0ba920 | 124 | } |
4ade5f1f | 125 | } |