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 | * | |
e102732c JB |
33 | * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. |
34 | * @typeParam Response - Type of execution response. This can only be structured-cloneable data. | |
325f50bc S |
35 | * @author [Christopher Quadflieg](https://github.com/Shinigami92) |
36 | * @since 2.0.0 | |
4ade5f1f | 37 | */ |
d3c8a1a8 | 38 | export class FixedClusterPool< |
deb85c12 JB |
39 | Data = unknown, |
40 | Response = unknown | |
d3c8a1a8 | 41 | > extends AbstractPool<Worker, Data, Response> { |
4ade5f1f | 42 | /** |
729c563d S |
43 | * Constructs a new poolifier fixed cluster pool. |
44 | * | |
38e795c1 JB |
45 | * @param numberOfWorkers - Number of workers for this pool. |
46 | * @param filePath - Path to an implementation of a `ClusterWorker` file, which can be relative or absolute. | |
47 | * @param opts - Options for this fixed cluster pool. | |
4ade5f1f S |
48 | */ |
49 | public constructor ( | |
5c5a1fb7 | 50 | numberOfWorkers: number, |
c97c7edb | 51 | filePath: string, |
dea903a8 | 52 | protected readonly opts: ClusterPoolOptions = {} |
4ade5f1f | 53 | ) { |
5c5a1fb7 | 54 | super(numberOfWorkers, filePath, opts) |
c97c7edb | 55 | } |
4ade5f1f | 56 | |
afc003b2 | 57 | /** @inheritDoc */ |
c97c7edb | 58 | protected setupHook (): void { |
1a76932b | 59 | cluster.setupPrimary({ ...this.opts.settings, exec: this.filePath }) |
c97c7edb | 60 | } |
325f50bc | 61 | |
afc003b2 | 62 | /** @inheritDoc */ |
c97c7edb | 63 | protected isMain (): boolean { |
7e0d447f | 64 | return cluster.isPrimary |
4ade5f1f S |
65 | } |
66 | ||
afc003b2 | 67 | /** @inheritDoc */ |
14a2e530 | 68 | protected destroyWorker (worker: Worker): void { |
cefac5ba | 69 | this.sendToWorker(worker, { kill: 1 }) |
5b59d53e JB |
70 | worker.on('disconnect', () => { |
71 | worker.kill() | |
72 | }) | |
73 | worker.disconnect() | |
4ade5f1f S |
74 | } |
75 | ||
afc003b2 | 76 | /** @inheritDoc */ |
c97c7edb S |
77 | protected sendToWorker (worker: Worker, message: MessageValue<Data>): void { |
78 | worker.send(message) | |
4ade5f1f S |
79 | } |
80 | ||
afc003b2 | 81 | /** @inheritDoc */ |
280c2a77 | 82 | protected createWorker (): Worker { |
7e0d447f | 83 | return cluster.fork(this.opts.env) |
4ade5f1f S |
84 | } |
85 | ||
afc003b2 | 86 | /** @inheritDoc */ |
8881ae32 | 87 | protected get type (): PoolType { |
6b27d407 | 88 | return PoolTypes.fixed |
7c0ba920 JB |
89 | } |
90 | ||
184855e6 JB |
91 | /** @inheritDoc */ |
92 | protected get worker (): WorkerType { | |
93 | return WorkerTypes.cluster | |
94 | } | |
95 | ||
08f3f44c | 96 | /** @inheritDoc */ |
6b27d407 JB |
97 | protected get minSize (): number { |
98 | return this.numberOfWorkers | |
99 | } | |
100 | ||
101 | /** @inheritDoc */ | |
102 | protected get maxSize (): number { | |
08f3f44c JB |
103 | return this.numberOfWorkers |
104 | } | |
105 | ||
afc003b2 | 106 | /** @inheritDoc */ |
c319c66b | 107 | protected get busy (): boolean { |
c2ade475 | 108 | return this.internalBusy() |
7c0ba920 | 109 | } |
4ade5f1f | 110 | } |